我使用一些承諾撰寫了一些代碼,在我的情況下,這是一個轉換腳本,它將在轉換檔案和附件時上傳它們。轉換部分作業正常,我在 xwiki 門戶中看到了預期的結果。
我想要的是遍歷目錄的檔案,進行一些轉換并找到并收集檔案中的所有影像名稱。完成此操作后,我需要先上傳頁面,因為這是 xwiki 方面的要求,然后我想上傳附件。此時它上傳了錯誤的附件,而不是檔案的一個檔案,而是從另一個檔案上傳它們。uploadImage 和 uploapPage 中的檔案名是其上傳到的頁面。
function importDirToXWiki (filePath: string, imagePath: string){
fs.readdir(filePath, (err, files) => {
if (err){
console.log(err);
}
files.forEach(file => {
convertFile(space, filePath, imagePath, file);
});
});
};
function convertFile(directory: string, path: string, imagePath: string, filename: string){
fs.readFile(path "/" filename, async function(err: any, data: { toString: () => string; }) {
if(err) throw err;
attachments = []
//Split the lines
let lines = data.toString().replace(/\r\n/g,'\n').split('\n');
//Convert md to xwiki format
lines = convertHeaders(lines);
lines = convertLists(lines);
lines.map(function (line, i) {
let filename = extractUsingRegex(line, /([a-zA-Z0-9\s_\\.\-\(\):]) (.png|.jpg)/gm)
if(filename.length > 0) attachments.push(filename);
lines[i] = convertImageTags(line);
});
//Adding HTML support & keep XWiki format active
lines.unshift('{{html wiki="true"}}');
lines.push("{{/html}}");
await postNewPage(lines.join("\n"), directory, formatFileName(filename))
Promise.all(attachments.map(async attachment => {
await uploadImage(directory, formatFileName(filename), imagePath, attachment)
}))
});
};
async function uploadImage(space: string, page: string, imagePath: string, filename: string){
if(fs.existsSync(imagePath "/" filename)){
let file = fs.readFileSync(imagePath "/" filename);
await postNewAttachment(file, space, page, filename );
}
};
uj5u.com熱心網友回復:
您可以使用 Node 的fs/promisesAPI 使事情變得更簡單。
.forEach我還清理了vs的一些非慣用用法.map。您還無意中制作attachments了一個全域(否const或var);考慮使用諸如 ESLint 之類的 linter 來警告您類似的事情。
const fsp = require("fs/promises");
async function importDirToXWiki(filePath: string, imagePath: string) {
const files = await fsp.readdir(filePath);
const conversionPromises = files.map((file) => convertFile(space, filePath, imagePath, file));
return Promise.all(conversionPromises);
}
async function convertFile(directory: string, path: string, imagePath: string, filename: string) {
const data = await fsp.readFile(path filename, "utf8");
const attachments = [];
//Split the lines
let lines = data.toString().replace(/\r\n/g, "\n").split("\n");
//Convert md to xwiki format
lines = convertHeaders(lines);
lines = convertLists(lines);
lines = lines.map(function (line) {
let filename = extractUsingRegex(line, /([a-zA-Z0-9\s_\\.\-\(\):]) (.png|.jpg)/gm);
if (filename.length > 0) attachments.push(filename);
return convertImageTags(line);
});
//Adding HTML support & keep XWiki format active
lines.unshift('{{html wiki="true"}}');
lines.push("{{/html}}");
await postNewPage(lines.join("\n"), directory, formatFileName(filename));
const uploadPromises = attachments.map((attachment) =>
uploadImage(directory, formatFileName(filename), imagePath, attachment),
);
return Promise.all(uploadPromises);
}
async function uploadImage(space: string, page: string, imagePath: string, filename: string) {
let data;
try {
const data = await fsp.readFile(imagePath filename);
return await postNewAttachment(data, space, page, filename);
} catch (err) {
if (err.code === "ENOENT") {
return; // "Not found", that's likely benign
}
throw err;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/464951.html
標籤:javascript 节点.js
