-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-files.js
286 lines (256 loc) · 10.9 KB
/
rename-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
if (window.qBittorrent === undefined) {
window.qBittorrent = {};
}
window.qBittorrent.MultiRename = (function() {
const exports = function() {
return {
AppliesTo: AppliesTo,
RenameFiles: RenameFiles
};
};
const AppliesTo = {
"FilenameExtension": "FilenameExtension",
"Filename": "Filename",
"Extension": "Extension"
};
const RenameFiles = new Class({
hash: '',
selectedFiles: [],
matchedFiles: [],
// Search Options
_inner_search: "",
setSearch(val) {
this._inner_search = val;
this._inner_update();
this.onChanged(this.matchedFiles);
},
useRegex: false,
matchAllOccurrences: false,
caseSensitive: false,
// Replacement Options
_inner_replacement: "",
setReplacement(val) {
this._inner_replacement = val;
this._inner_update();
this.onChanged(this.matchedFiles);
},
appliesTo: AppliesTo.FilenameExtension,
includeFiles: true,
includeFolders: false,
replaceAll: false,
fileEnumerationStart: 0,
onChanged: function(rows) {},
onInvalidRegex: function(err) {},
onRenamed: function(rows) {},
onRenameError: function(err) {},
_inner_update: function() {
const findMatches = (regex, str) => {
let result;
let count = 0;
let lastIndex = 0;
regex.lastIndex = 0;
let matches = [];
do {
result = regex.exec(str);
if (result == null) { break; }
matches.push(result);
// regex assertions don't modify lastIndex,
// so we need to explicitly break out to prevent infinite loop
if (lastIndex == regex.lastIndex) {
break;
}
else {
lastIndex = regex.lastIndex;
}
// Maximum of 250 matches per file
++count;
} while (regex.global && count < 250);
return matches;
};
const replaceBetween = (input, start, end, replacement) => {
return input.substring(0, start) + replacement + input.substring(end);
};
const replaceGroup = (input, search, replacement, escape, stripEscape = true) => {
let result = '';
let i = 0;
while (i < input.length) {
// Check if the current index contains the escape string
if (input.substring(i, i + escape.length) === escape) {
// Don't replace escape chars when they don't precede the current search being performed
if (input.substring(i + escape.length, i + escape.length + search.length) !== search) {
result += input[i];
i++;
continue;
}
// Replace escape chars when they precede the current search being performed, unless explicitly told not to
if (stripEscape) {
result += input.substring(i + escape.length, i + escape.length + search.length);
i += escape.length + search.length;
}
else {
result += input.substring(i, i + escape.length + search.length);
i += escape.length + search.length;
}
// Check if the current index contains the search string
}
else if (input.substring(i, i + search.length) === search) {
result += replacement;
i += search.length;
// Append characters that didn't meet the previous criteria
}
else {
result += input[i];
i++;
}
}
return result;
};
this.matchedFiles = [];
// Ignore empty searches
if (!this._inner_search) {
return;
}
// Setup regex flags
let regexFlags = "";
if (this.matchAllOccurrences) { regexFlags += "g"; }
if (!this.caseSensitive) { regexFlags += "i"; }
// Setup regex search
const regexEscapeExp = new RegExp(/[/\-\\^$*+?.()|[\]{}]/g);
const standardSearch = new RegExp(this._inner_search.replace(regexEscapeExp, '\\$&'), regexFlags);
let regexSearch;
try {
regexSearch = new RegExp(this._inner_search, regexFlags);
}
catch (err) {
if (this.useRegex) {
this.onInvalidRegex(err);
return;
}
}
const search = this.useRegex ? regexSearch : standardSearch;
let fileEnumeration = this.fileEnumerationStart;
for (let i = 0; i < this.selectedFiles.length; ++i) {
const row = this.selectedFiles[i];
// Ignore files
if (!row.isFolder && !this.includeFiles) {
continue;
}
// Ignore folders
else if (row.isFolder && !this.includeFolders) {
continue;
}
// Get file extension and reappend the "." (only when the file has an extension)
let fileExtension = window.qBittorrent.Filesystem.fileExtension(row.original);
if (fileExtension) { fileExtension = "." + fileExtension; }
const fileNameWithoutExt = row.original.slice(0, row.original.lastIndexOf(fileExtension));
let matches = [];
let offset = 0;
switch (this.appliesTo) {
case "FilenameExtension":
matches = findMatches(search, `${fileNameWithoutExt}${fileExtension}`);
break;
case "Filename":
matches = findMatches(search, `${fileNameWithoutExt}`);
break;
case "Extension":
// Adjust the offset to ensure we perform the replacement at the extension location
offset = fileNameWithoutExt.length;
matches = findMatches(search, `${fileExtension}`);
break;
}
// Ignore rows without a match
if (!matches || matches.length == 0) {
continue;
}
let renamed = row.original;
for (let i = matches.length - 1; i >= 0; --i) {
const match = matches[i];
let replacement = this._inner_replacement;
// Replace numerical groups
for (let g = 0; g < match.length; ++g) {
let group = match[g];
if (!group) { continue; }
replacement = replaceGroup(replacement, `$${g}`, group, '\\', false);
}
// Replace named groups
for (let namedGroup in match.groups) {
replacement = replaceGroup(replacement, `$${namedGroup}`, match.groups[namedGroup], '\\', false);
}
// Replace auxiliary variables
for (let v = 'dddddddd'; v !== ''; v = v.substring(1)) {
let fileCount = fileEnumeration.toString().padStart(v.length, '0');
replacement = replaceGroup(replacement, `$${v}`, fileCount, '\\', false);
}
// Remove empty $ variable
replacement = replaceGroup(replacement, '$', '', '\\');
const wholeMatch = match[0];
const index = match['index'];
renamed = replaceBetween(renamed, index + offset, index + offset + wholeMatch.length, replacement);
}
row.renamed = renamed;
++fileEnumeration;
this.matchedFiles.push(row);
}
},
rename: async function() {
if (!this.matchedFiles || this.matchedFiles.length === 0 || !this.hash) {
this.onRenamed([]);
return;
}
let replaced = [];
const _inner_rename = async function(i) {
const match = this.matchedFiles[i];
const newName = match.renamed;
if (newName === match.original) {
// Original file name is identical to Renamed
return;
}
const isFolder = match.isFolder;
const parentPath = window.qBittorrent.Filesystem.folderName(match.path);
const oldPath = parentPath
? parentPath + window.qBittorrent.Filesystem.PathSeparator + match.original
: match.original;
const newPath = parentPath
? parentPath + window.qBittorrent.Filesystem.PathSeparator + newName
: newName;
let renameRequest = new Request({
url: isFolder ? 'api/v2/torrents/renameFolder' : 'api/v2/torrents/renameFile',
method: 'post',
data: {
hash: this.hash,
oldPath: oldPath,
newPath: newPath
}
});
try {
await renameRequest.send();
replaced.push(match);
}
catch (err) {
this.onRenameError(err, match);
}
}.bind(this);
const replacements = this.matchedFiles.length;
if (this.replaceAll) {
// matchedFiles are in DFS order so we rename in reverse
// in order to prevent unwanted folder creation
for (let i = replacements - 1; i >= 0; --i) {
await _inner_rename(i);
}
}
else {
// single replacements go linearly top-down because the
// file tree gets recreated after every rename
await _inner_rename(0);
}
this.onRenamed(replaced);
},
update: function() {
this._inner_update();
this.onChanged(this.matchedFiles);
}
});
return exports();
})();
Object.freeze(window.qBittorrent.MultiRename);