LaiTool/src/main/Public/Image.js
2024-06-01 15:08:22 +08:00

48 lines
1.9 KiB
JavaScript

import { errorMessage, successMessage } from "../generalTools";
import path from "path";
import { Tools } from "../tools";
export class Image {
constructor(global) {
this.global = global;
this.tools = new Tools();
}
// 将指定的文件夹复制到四个文件夹中
async OneSplitFour(value) {
try {
value = JSON.parse(value);
let count = value[1];
let data = value[0];
// 先创建输出文件
if (count <= 1) {
throw new Error("可选择的图片的数量必须大于1");
}
for (let i = 1; i < count; i++) {
let out_folder = path.join(this.global.config.project_path, `tmp/output_crop_0000${i + 1}`);
// 判断当前的文件夹是不是存在,存在删除
let isH = await this.tools.checkExists(out_folder);
if (isH) {
await this.tools.deleteFileOrDirectory(out_folder);
}
await this.tools.checkFolderExistsOrCreate(out_folder)
}
for (let i = 0; i < data.length; i++) {
const element = data[i];
let subImagePath = element.subImagePath;
for (let j = 1; j < count; j++) {
let out_file = path.join(this.global.config.project_path, `tmp/output_crop_0000${j + 1}/${element.name}`);
if (subImagePath[j] && subImagePath[j].startsWith("file")) {
subImagePath[j] = subImagePath[j].replace("file://", "");
subImagePath[j] = subImagePath[j].replace(/\?time=.*$/, '');
}
await this.tools.copyFileOrDirectory(subImagePath[j], out_file);
}
}
return successMessage("拆分成功");
} catch (error) {
return errorMessage(error.message);
}
}
}