|
@@ -190,9 +190,11 @@ export function renamePreUploadJsonFile(dir) {
|
|
|
|
|
|
export async function moveFile(src, dest, count = 1) {
|
|
|
let result = true;
|
|
|
- await fs.promises.rename(src, dest).catch((e) => {
|
|
|
+ await fs.promises.rename(src, dest).catch((error) => {
|
|
|
logger.error(
|
|
|
- `loop-03-01 转存文件失败,rename:${count}:${e.message || "rename error"}`
|
|
|
+ `loop-03-01 转存文件失败,rename:${count}:${
|
|
|
+ error.message || "rename error"
|
|
|
+ }`
|
|
|
);
|
|
|
result = false;
|
|
|
});
|
|
@@ -205,11 +207,11 @@ export async function moveFile(src, dest, count = 1) {
|
|
|
try {
|
|
|
await fs.promises.copyFile(src, dest);
|
|
|
await fs.promises.unlink(src);
|
|
|
- } catch (e) {
|
|
|
+ } catch (error) {
|
|
|
logger.error(
|
|
|
- `loop-03-01 转存文件失败:copy:${e.message || "copy unlink error"}`
|
|
|
+ `loop-03-01 转存文件失败:copy:${error.message || "copy unlink error"}`
|
|
|
);
|
|
|
- return Promise.reject(e);
|
|
|
+ return Promise.reject(error);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -226,85 +228,39 @@ export async function getDirScanFile(dir) {
|
|
|
|
|
|
let files = fs
|
|
|
.readdirSync(ddir)
|
|
|
- .filter((fileName) =>
|
|
|
- /^\d{13}_\d{8}_(F|B)\.(jpg|png|jpeg)$/.test(fileName)
|
|
|
- );
|
|
|
-
|
|
|
- logger.info(`loop-02 目录结果:${files.join()}`);
|
|
|
-
|
|
|
- files = files
|
|
|
- .map((fileName) => {
|
|
|
- return {
|
|
|
- name: fileName,
|
|
|
- time: fs.statSync(path.join(ddir, fileName)).birthtimeMs,
|
|
|
- };
|
|
|
- })
|
|
|
- .sort((a, b) => a.time - b.time);
|
|
|
-
|
|
|
- logger.info(`loop-03 目录排序结果:$${JSON.stringify(files)}`);
|
|
|
+ .filter((fileName) => /^\d{13}_\d{8}_F\.(jpg|png|jpeg)$/.test(fileName));
|
|
|
+ files = files.sort((a, b) => (a <= b ? -1 : 1));
|
|
|
|
|
|
- const getbasename = (name) => {
|
|
|
- return path
|
|
|
- .basename(name, path.extname(name))
|
|
|
- .replace("_F", "")
|
|
|
- .replace("_B", "");
|
|
|
- };
|
|
|
+ logger.info(`loop-02 目录结果:${JSON.stringify(files)}`);
|
|
|
|
|
|
- // console.log(files);
|
|
|
- // 根据时间排序之后,结果一定是[正,反,正,反]结构
|
|
|
const nFiles = [];
|
|
|
- const len = Math.floor(files.length / 2);
|
|
|
+ const len = files.length;
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
- let frontName = files[2 * i].name;
|
|
|
- let versoName = files[2 * i + 1].name;
|
|
|
- // 严格控制文件顺序为F,B(即正,反)
|
|
|
- if (!frontName.includes("_F.")) {
|
|
|
- const midName = frontName;
|
|
|
- frontName = versoName;
|
|
|
- versoName = midName;
|
|
|
- }
|
|
|
-
|
|
|
- const frontBaseName = getbasename(frontName);
|
|
|
- const versoBaseName = getbasename(versoName);
|
|
|
- if (frontBaseName !== versoBaseName) {
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
+ const frontName = files[i];
|
|
|
+ logger.info(`loop-03-01 开始转存文件:${frontName}`);
|
|
|
const frontFile = path.join(stageDdir, frontName);
|
|
|
- const versoFile = path.join(stageDdir, versoName);
|
|
|
-
|
|
|
- logger.info(`loop-03-01 开始转存文件:${frontName},${versoName}`);
|
|
|
+ const versoFile = path.join(stageDdir, frontName.replace("_F", "_B"));
|
|
|
|
|
|
const inFrontFile = path.join(ddir, frontName);
|
|
|
- const inVersoFile = path.join(ddir, versoName);
|
|
|
- // if (!fs.existsSync(inFrontFile) || !fs.existsSync(inVersoFile)) {
|
|
|
- // logger.error(
|
|
|
- // `loop-03-01 转存文件失败:${frontName},${versoName}有文件不存在,退出!`
|
|
|
- // );
|
|
|
- // break;
|
|
|
- // }
|
|
|
|
|
|
let result = true;
|
|
|
- try {
|
|
|
- await moveFile(inFrontFile, frontFile);
|
|
|
- await moveFile(inVersoFile, versoFile);
|
|
|
- } catch (error) {
|
|
|
+ await moveFile(inFrontFile, frontFile).catch((error) => {
|
|
|
result = false;
|
|
|
logger.info(
|
|
|
- `loop-03-02 转存文件失败:${frontName},${versoName},原因:${
|
|
|
+ `loop-03-02 转存文件失败:${frontName},原因:${
|
|
|
error.message || "move file failed"
|
|
|
}`
|
|
|
);
|
|
|
- }
|
|
|
-
|
|
|
+ });
|
|
|
if (!result) break;
|
|
|
|
|
|
- logger.info(`loop-03-02 转存文件成功:${frontName},${versoName}`);
|
|
|
+ logger.info(`loop-03-02 转存文件成功:${frontName}`);
|
|
|
|
|
|
nFiles[i] = {
|
|
|
frontFile,
|
|
|
versoFile,
|
|
|
basename: getScanFileBasename(frontName),
|
|
|
+ isFull: false,
|
|
|
};
|
|
|
}
|
|
|
|
|
@@ -424,8 +380,8 @@ export async function batchSaveImages(imageList, courseCode) {
|
|
|
return async () => {
|
|
|
const outputOriginPath = path.join(outputDir, path.basename(item.url));
|
|
|
let result = true;
|
|
|
- await moveFile(item.url, outputOriginPath).catch((err) => {
|
|
|
- console.error(`Error moving file: ${item.url}, ${err.message}`);
|
|
|
+ await moveFile(item.url, outputOriginPath).catch((error) => {
|
|
|
+ console.error(`Error moving file: ${item.url}, ${error.message}`);
|
|
|
result = false;
|
|
|
});
|
|
|
if (!result) return;
|
|
@@ -438,3 +394,32 @@ export async function batchSaveImages(imageList, courseCode) {
|
|
|
|
|
|
return imageList;
|
|
|
}
|
|
|
+
|
|
|
+export async function moveInFileToStage(dir) {
|
|
|
+ const ddir = dir || getInputDir();
|
|
|
+ const stageDdir = getStageDir();
|
|
|
+ const files = fs.readdirSync(ddir);
|
|
|
+ const { buildQueue } = usePQueue({ concurrency: 6 });
|
|
|
+
|
|
|
+ const tasks = files.map((fileName) => {
|
|
|
+ return async () => {
|
|
|
+ const stageFilePath = path.join(stageDdir, fileName);
|
|
|
+ const inFilePath = path.join(ddir, fileName);
|
|
|
+
|
|
|
+ await moveFile(inFilePath, stageFilePath).catch((error) => {
|
|
|
+ logger.error(`Error moving file: ${fileName}, ${error.message}`);
|
|
|
+ });
|
|
|
+ };
|
|
|
+ });
|
|
|
+ await buildQueue(tasks);
|
|
|
+}
|
|
|
+
|
|
|
+export function checkStageFileValid(datas) {
|
|
|
+ for (let i = 0; i < datas.length; i++) {
|
|
|
+ const item = datas[i];
|
|
|
+ if (!fs.existsSync(item.frontFile) || !fs.existsSync(item.versoFile)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|