Skip to content

Commit 51c6ec9

Browse files
author
fanxb
committed
Merge pull request 'dev' (#52) from dev into main
Reviewed-on: https://gitea.fleyx.com/fanxb/open-renamer/pulls/52
2 parents 67f542f + a0e0186 commit 51c6ec9

File tree

11 files changed

+5323
-4340
lines changed

11 files changed

+5323
-4340
lines changed

electron/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "electron",
3-
"version": "1.1.0",
2+
"name": "renamer",
3+
"version": "1.8.0",
44
"description": "",
55
"main": "main.js",
66
"scripts": {
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,99 @@
11
import RuleInterface from "./RuleInterface";
2+
import {dealFileName} from "./RuleInterface";
23
import FileObj from "../../vo/FileObj";
34
import path from 'path';
45

56
export default class DeleteRule implements RuleInterface {
6-
/**
7-
* 类别:deletePart:部分删除,deleteAll:全部删除
8-
*/
9-
type: string;
10-
/**
11-
* 部分删除时的开始信息
12-
*/
13-
start: DeleteRuleItem;
14-
/**
15-
* 部分删除时的结束信息
7+
/**
8+
* 类别:deletePart:部分删除,deleteAll:全部删除
9+
*/
10+
type: string;
11+
/**
12+
* 部分删除时的开始信息
13+
*/
14+
start: DeleteRuleItem;
15+
/**
16+
* 部分删除时的结束信息
1617
17-
*/
18-
end: DeleteRuleItem;
19-
/**
20-
* 忽略拓展名,true:忽略,false:不忽略
21-
*/
22-
ignorePostfix: boolean;
18+
*/
19+
end: DeleteRuleItem;
20+
/**
21+
* 忽略拓展名,true:忽略,false:不忽略
22+
*/
23+
ignorePostfix: boolean;
24+
/*
25+
* 是否区分大小写
26+
*/
27+
regI: boolean;
2328

24-
constructor(data: any) {
25-
this.type = data.type;
26-
this.start = new DeleteRuleItem(data.start);
27-
this.end = new DeleteRuleItem(data.end);
28-
this.ignorePostfix = data.ignorePostfix;
29-
}
29+
constructor(data: any) {
30+
this.type = data.type;
31+
this.regI = data.regI != undefined && data.regI;
32+
this.start = new DeleteRuleItem(data.start, this.regI);
33+
this.end = new DeleteRuleItem(data.end, this.regI);
34+
this.ignorePostfix = data.ignorePostfix;
35+
}
3036

3137

32-
33-
deal(file: FileObj): void {
34-
if (this.type === 'deleteAll') {
35-
file.realName = "";
36-
if (!this.ignorePostfix) {
37-
file.expandName = "";
38-
}
39-
} else {
40-
let str = file.realName + (this.ignorePostfix ? "" : file.expandName);
41-
let startIndex = this.start.calIndex(str);
42-
let endIndex = this.end.calIndex(str);
43-
if (startIndex < 0 || endIndex < 0) {
44-
return;
45-
}
46-
str = str.substring(0, startIndex) + str.substring(endIndex + 1);
47-
if (this.ignorePostfix) {
48-
file.realName = str;
49-
} else {
50-
file.expandName = path.extname(str);
51-
if (file.expandName.length > 0) {
52-
file.realName = str.substring(0, str.lastIndexOf("."));
53-
} else {
54-
file.realName = str;
55-
}
56-
}
57-
}
58-
59-
file.name = file.realName + file.expandName;
60-
}
38+
deal(file: FileObj): void {
39+
let target = "";
40+
if (this.type === 'deleteAll') {
41+
target = "";
42+
} else {
43+
let str = file.realName + (this.ignorePostfix ? "" : file.expandName);
44+
let startIndex = this.start.calIndex(str, false);
45+
let endIndex = this.end.calIndex(str, true);
46+
if (startIndex < 0 || endIndex < 0 || startIndex > endIndex) {
47+
return;
48+
}
49+
str = str.substring(0, startIndex) + str.substring(endIndex + 1);
50+
target = str;
51+
}
52+
dealFileName(file, target, this.ignorePostfix);
53+
}
6154

6255
}
6356

6457
class DeleteRuleItem {
65-
/**
66-
* location:位置,text:文本,end:直到末尾
67-
*/
68-
type: string;
69-
/**
70-
* 对应的值
71-
*/
72-
value: string;
58+
/**
59+
* location:位置,text:文本,end:直到末尾
60+
*/
61+
type: string;
62+
/**
63+
* 对应的值
64+
*/
65+
value: string;
66+
/**
67+
* 正则对象
68+
*/
69+
reg: RegExp;
7370

74-
constructor(data: any) {
75-
this.type = data.type;
76-
this.value = data.value;
77-
}
71+
constructor(data: any, regI: boolean) {
72+
this.type = data.type;
73+
this.value = data.value;
74+
if (this.type === 'reg') {
75+
this.reg = regI ? new RegExp(this.value) : new RegExp(this.value, 'i');
76+
}
77+
}
7878

79-
/**
80-
* 计算位置
81-
*/
82-
calIndex(str: string): number {
83-
if (this.type === 'location') {
84-
return parseInt(this.value) - 1;
85-
} else if (this.type === 'text') {
86-
return str.indexOf(this.value);
87-
} else if (this.type === 'end') {
88-
return str.length - 1;
89-
}
90-
return -1;
91-
}
79+
/**
80+
* 计算位置
81+
* @param str 字符串
82+
* @param end 是否末尾计算
83+
*/
84+
calIndex(str: string, end: boolean): number {
85+
if (this.type === 'location') {
86+
let val = parseInt(this.value);
87+
return val > 0 ? val - 1 : str.length + val;
88+
} else if (this.type === 'text') {
89+
let index = str.indexOf(this.value);
90+
return index + (end ? this.value.length - 1 : 0);
91+
} else if (this.type === 'end') {
92+
return str.length - 1;
93+
} else if (this.type === 'reg') {
94+
let res = this.reg.exec(str);
95+
return res == null ? -1 : (res.index + (end ? res[0].length - 1 : 0));
96+
}
97+
return -1;
98+
}
9299
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import RuleInterface from "./RuleInterface";
2+
import * as ValUtil from "../../../util/ValUtil";
23
import FileObj from "../../vo/FileObj";
4+
import {dealFileName} from './RuleInterface';
35
import path from 'path';
46

57

@@ -10,45 +12,101 @@ export default class ReplaceRule implements RuleInterface {
1012
*/
1113
type: number;
1214
/**
13-
* 前面追加
15+
*
1416
*/
1517
source: string;
1618
/**
17-
* 后面追加
19+
* 目标
1820
*/
1921
target: string;
22+
/**
23+
* 是否正则模式
24+
*/
25+
regFlag: boolean;
26+
/**
27+
* 是否区分大小写
28+
*/
29+
regI: boolean;
30+
/**
31+
* 是否护理拓展名
32+
*/
33+
ignorePostfix: boolean;
2034

2135
constructor(data: any) {
2236
this.type = data.type;
2337
this.source = data.source;
2438
this.target = data.target;
39+
this.regFlag = ValUtil.nullToDefault(data.regFlag, false);
40+
this.regI = ValUtil.nullToDefault(data.regI, false);
41+
this.ignorePostfix = ValUtil.nullToDefault(data.ignorePostfix, false);
2542
}
2643

2744

2845
deal(file: FileObj): void {
46+
let targetStr = this.ignorePostfix ? file.realName : file.name;
47+
let res = this.regFlag ? this.dealReg(targetStr) : this.dealNoReg(targetStr);
48+
dealFileName(file, res, this.ignorePostfix);
49+
}
50+
51+
private dealNoReg(targetStr: string): string {
2952
let start = 0;
30-
let changed = false;
31-
for (; ;) {
32-
let index = this.type == 1 || this.type == 3 ? file.name.indexOf(this.source, start) : file.name.lastIndexOf(this.source);
33-
if (index > -1) {
34-
file.name = file.name.substring(0, index) + this.target + file.name.substring(index + this.source.length);
35-
start = index + this.target.length;
36-
changed = true;
37-
if (this.type != 3) {
38-
break;
39-
}
40-
} else {
53+
let arr: number[] = [];
54+
for (let i = 0; i < (this.type == 1 ? 1 : 1000); i++) {
55+
let one = targetStr.indexOf(this.source, start);
56+
if (one == -1) {
4157
break;
4258
}
59+
arr.push(one);
60+
start = one + this.source.length;
61+
}
62+
if (arr.length == 0) {
63+
return targetStr;
64+
}
65+
let res = "";
66+
let needDealArr: number[] = this.type === 1 ? [arr[0]] : this.type === 2 ? [arr[arr.length - 1]] : arr;
67+
let lastIndex = 0;
68+
for (let i = 0; i < needDealArr.length; i++) {
69+
res += targetStr.substring(lastIndex, needDealArr[i]) + this.target;
70+
lastIndex = needDealArr[i] + this.source.length;
4371
}
44-
if (changed) {
45-
file.originName = file.name;
46-
file.expandName = path.extname(file.name);
47-
if (file.expandName && file.expandName.length > 0) {
48-
file.realName = file.name.substring(0, file.name.lastIndexOf("."));
49-
} else {
50-
file.realName = file.name;
72+
res += targetStr.substring(lastIndex);
73+
return res;
74+
}
75+
76+
private dealReg(targetStr: string): string {
77+
let templateReg = new RegExp("#\{group(\\d+\)}", "g");
78+
let templateArr: string[][] = [];
79+
while (true) {
80+
let one = templateReg.exec(this.target);
81+
if (one == null) {
82+
break;
5183
}
84+
templateArr.push([one[0], one[1]]);
85+
}
86+
87+
let reg = new RegExp(this.source, this.regI ? "g" : "ig");
88+
let arr: RegExpExecArray[] = [];
89+
for (let i = 0; i < (this.type == 1 ? 1 : 1000); i++) {
90+
let one = reg.exec(targetStr);
91+
if (one == null) {
92+
break;
93+
}
94+
arr.push(one);
95+
}
96+
if (arr.length == 0) {
97+
return targetStr;
98+
}
99+
let res = "";
100+
let needDealReg: RegExpExecArray[] = this.type === 1 ? [arr[0]] : this.type === 2 ? [arr[arr.length - 1]] : arr;
101+
let lastIndex = 0;
102+
for (let i = 0; i < needDealReg.length; i++) {
103+
let reg = needDealReg[i];
104+
let target = this.target;
105+
templateArr.forEach(item => target = target.replace(item[0], ValUtil.nullToDefault(reg[parseInt(item[1])], '')));
106+
res += targetStr.substring(lastIndex, reg.index) + target;
107+
lastIndex = reg.index + reg[0].length;
52108
}
109+
res += targetStr.substring(lastIndex);
110+
return res;
53111
}
54112
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import FileObj from "../../vo/FileObj";
2+
import * as path from 'path';
23

34
export default interface RuleInterface {
45

5-
deal(file: FileObj): void;
6+
deal(file: FileObj): void;
7+
}
8+
9+
/**
10+
* 重新处理文件名
11+
* @param file
12+
* @param newFileName
13+
* @param ignorePostfix
14+
*/
15+
export function dealFileName(file: FileObj, newFileName: string, ignorePostfix: boolean) {
16+
if (ignorePostfix) {
17+
file.realName = newFileName;
18+
} else {
19+
file.expandName = path.extname(newFileName);
20+
if (file.expandName.length > 0) {
21+
file.realName = newFileName.substring(0, newFileName.lastIndexOf("."));
22+
} else {
23+
file.realName = newFileName;
24+
}
25+
}
26+
file.name = file.realName + file.expandName;
627
}

openRenamerBackend/entity/vo/FileObj.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import {isVideo, isSub, isNfo} from "../../util/MediaUtil"
33

44
export default class FileObj {
55
/**
6-
* 文件名
6+
* 变更后的文件名(包含拓展名)
77
*/
88
name: string;
99
/**
10-
原始名字
10+
* 去掉拓展名后的名字(不包含拓展名)
1111
*/
12-
originName: string;
12+
realName: string;
1313
/**
14-
* 拓展名
14+
原始文件名(不变)
1515
*/
16-
expandName: string;
16+
originName: string;
1717
/**
18-
* 去掉拓展名后的名字
18+
* 拓展名(最新的拓展名,每次应用规则后重新计算)
1919
*/
20-
realName: string;
20+
expandName: string;
21+
2122
/**
2223
* 所属路径
2324
*/

openRenamerBackend/util/ValUtil.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* null to default
3+
* @param value
4+
* @param defaultVal
5+
*/
6+
export function nullToDefault(value: any, defaultVal: any): any {
7+
return value === undefined || value == null ? defaultVal : value;
8+
}

0 commit comments

Comments
 (0)