2021-06-27 22:06:11 +08:00
|
|
|
import config from '../config';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs-extra';
|
|
|
|
|
|
|
|
import FileObj from '../vo/FileObj';
|
2021-06-28 18:07:02 +08:00
|
|
|
import RuleObj from '../vo/RuleObj';
|
2021-06-27 22:06:11 +08:00
|
|
|
import DeleteRule from '../vo/rules/DeleteRule';
|
2021-06-28 18:07:02 +08:00
|
|
|
import RuleInterface from '../vo/rules/RuleInterface';
|
|
|
|
|
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));
|
|
|
|
if (newNameSet.has(obj.name)) {
|
|
|
|
obj.errorMessage = "重名";
|
|
|
|
}
|
|
|
|
newNameSet.add(obj.name);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
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;
|