Files
LaiTool/src/define/setting/imageSetting.js
T

245 lines
8.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Tools } from "../../main/tools";
import { define } from "../define";
import path from "path";
import { DEFINE_STRING } from "../define_string";
import { get, has } from "lodash";
let tools = new Tools();
const { v4: uuidv4 } = require('uuid'); // 引入UUID库来生成唯一标识符
export const ImageSetting = {
/**
* 获取自动保存图片的方式
* @returns
*/
async GetAutoSaveImageClassifyOptions() {
return {
code: 1,
data: [{
label: "不分类",
value: "one"
},
// {
// label: "按模型",
// value: "model"
// },
{
label: "按风格",
value: "style"
},
{
label: "风格+模型",
value: "theme"
}]
}
},
/**
* 获取自动保存图片的方式(sd,mj,d3 等)
*/
async GetImageGenerateCategory() {
return {
code: 1,
data: [{
label: "SD",
value: "sd"
},
{
label: "MJ",
value: "mj"
},
{
label: "D3",
value: "d3"
}]
}
},
/**
* 保存自动保存图片的设置
*/
async SaveImageAutoSaveSetting(value) {
try {
value = JSON.parse(value);
await tools.writeJsonFilePropertyValue(define.img_base, "auto_save_image", value);
return {
code: 1,
}
} catch (error) {
return {
code: 0,
message: error.toString()
}
}
},
/**
* 保存自动保存图片的设置
*/
async GetImageAutoSaveSetting() {
try {
let res = await tools.getJsonFilePropertyValue(define.img_base, "auto_save_image", {}, false);
return {
code: 1,
data: res
}
} catch (error) {
return {
code: 0,
message: error.toString()
}
}
},
/**
* 保存图片到指定文件夹,只是将任务添加到队列中
*/
async SaveImageToOtherFolder(output = [], value) {
try {
let show = false;
if (value) {
value = JSON.parse(value);
show = false;
} else {
let auto_save_image = await tools.getJsonFilePropertyValue(define.img_base, "auto_save_image", {}, false);
value = {
save_match_count: auto_save_image.save_match_count,
save_folder: auto_save_image.main_save_folder
}
if (!auto_save_image.auto_save) {
return {
code: 1
}
}
}
let batch = DEFINE_STRING.QUEUE_BATCH.IMAGE_SAVE_TO_OTHER_FOLDER;
// 获取当前的所有的output文件夹
if (output.length == 0) {
output = await tools.getSubFolderList(path.join(global.config.project_path, "tmp/bak/"), "start", "output_crop_");
}
for (let i = 0; i < output.length; i++) {
const element = path.join(global.config.project_path, 'tmp/bak/' + output[i]);
// 获取指定的文件夹里面的文件
let png_files = await tools.getFilesWithExtensions(element, ".png");
console.log(png_files);
// 判断当前的png_files中的数据是不是大于value.save_match_count,大于的话,删除数组里面的数据
if (value.save_match_count && png_files.length > value.save_match_count) {
// 删除数组
png_files.splice(value.save_match_count);
}
for (let j = 0; j < png_files.length; j++) {
const item = png_files[j];
global.fileQueue.enqueue(async () => {
// 复制文件到指定的文件夹
let dst = path.join(value.save_folder, `${Date.now().toString()}_${uuidv4().split('-')[0]}.png`);
await tools.copyFileOrDirectory(item, dst);
}, `${batch}_${item}`, batch)
}
}
global.fileQueue.setBatchCompletionCallback(batch, (failedTasks) => {
if (failedTasks.length > 0) {
let message = `
转存任务都已完成。
但是以下任务执行失败:
`
failedTasks.forEach(({ taskId, error }) => {
message += `${taskId}-, \n 错误信息: ${error}` + '\n';
});
global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, {
code: 0,
message: message
})
} else {
if (show) {
global.newWindow[0].win.webContents.send(DEFINE_STRING.SHOW_MESSAGE_DIALOG, {
code: 1,
message: "所有图片转存完成"
})
}
}
})
return {
code: 1
}
} catch (error) {
return {
code: 0,
message: error.toString()
}
}
},
/**
* 获取指定的配置文件里面指定的属性的数据
* @param {*} value 执行方法必要的信息
* 0 define中的指定属性(指定的配置文件)
* 1 要获取的什么属性信息 propertyproperty 为null,赶回当前配置文件的所有数据
* 2 是不是要校验属性不存在
* 3 属性没有找到的默认值
* @returns
*/
async GetDefineConfigJsonByProperty(value) {
try {
value = JSON.parse(value);
let defin_property = value[0];
let property = value[1];
let need_check = value[2];
let default_data = value[3];
// 判断define中的属性是不是存在
let hasProperty = has(define, defin_property);
if (!hasProperty) {
throw new Error(`${defin_property} 属性不存在。`);
}
let data = await tools.getJsonFilePropertyValue(define[defin_property], property, default_data, need_check);
return {
code: 1,
data: data
}
} catch (error) {
return {
code: 0,
message: "获取配置信息失败,错误信息入下 : " + error.message.toString()
}
}
},
/**
* 保存指定的配置文件里面指定的属性的数据
* @param {*} value
* 0 define中的指定属性(指定的配置文件)
* 1 要获取的什么属性信息 propertyproperty 为null,赶回当前配置文件的所有数据
* 2 是不是要校验属性不存在
* 3 属性没有找到的默认值
*/
async SaveDefineConfigJsonByProperty(value) {
try {
value = JSON.parse(value);
let defin_property = value[0];
let property = value[1];
let data = value[2];
let need_check = value[3] ? value[3] : false;
// 判断define中的属性是不是存在
let hasProperty = has(define, defin_property);
if (!hasProperty) {
throw new Error(`${defin_property} 属性不存在。`);
}
await tools.writeJsonFilePropertyValue(define[defin_property], property, data, need_check);
return {
code: 1,
message: "保存指定的配置成功"
}
} catch (error) {
return {
code: 0,
message: "保存配置信息失败,错误信息入下 : " + error.message.toString()
}
}
}
}