44 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-06-27 22:06:11 +08:00
import config from '../config';
import * as path from 'path';
import * as fs from 'fs-extra';
2022-11-27 20:22:42 +08:00
import FileObj from '../entity/vo/FileObj';
import RuleObj from '../entity/vo/RuleObj';
import RuleInterface from '../entity/bo/rules/RuleInterface';
2021-06-28 18:07:02 +08:00
2021-06-27 22:06:11 +08:00
class RenamerService {
2021-06-28 18:07:02 +08:00
static async preview(fileList: Array<FileObj>, ruleList: Array<any>): Promise<Array<FileObj>> {
let ruleObjs = ruleList.map(item => new RuleObj(item));
let newNameSet: Set<string> = new Set<string>();
for (let i in fileList) {
let obj = fileList[i];
ruleObjs.forEach(item => (item.data as RuleInterface).deal(obj));
2023-05-24 22:04:24 +08:00
if (newNameSet.has(obj.path + obj.name)) {
2021-06-28 18:07:02 +08:00
obj.errorMessage = "重名";
}
2023-05-10 20:24:29 +08:00
newNameSet.add(obj.path + obj.name);
2021-06-28 18:07:02 +08:00
}
return fileList;
}
static async rename(fileList: Array<FileObj>, changedFileList: Array<FileObj>) {
for (let i in fileList) {
let old = fileList[i];
let oldPath = path.join(fileList[i].path, fileList[i].name);
let newPath = path.join(changedFileList[i].path, changedFileList[i].name);
2022-12-17 18:59:43 +08:00
if (oldPath === newPath) {
continue;
}
2021-06-28 18:07:02 +08:00
if ((await fs.pathExists(newPath))) {
throw new Error("此路径已存在:" + newPath);
}
await fs.rename(oldPath, newPath);
}
2021-06-27 22:06:11 +08:00
}
}
export default RenamerService;