Skip to content

Commit 534e89b

Browse files
committed
sanitizeName
1 parent c094f38 commit 534e89b

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

scripts/content-scripts/ufs_global.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const UfsGlobal = {
3232
getWatchingVideoSrc,
3333
},
3434
Utils: {
35+
sanitizeName,
3536
debounce,
3637
makeUrlValid,
3738
getNumberFormatter,
@@ -517,16 +518,48 @@ function getWatchingVideoSrc() {
517518
}
518519
}
519520
}
521+
// #endregion
522+
523+
// #region Utils
524+
525+
// https://github.com/parshap/node-sanitize-filename/blob/master/index.js
526+
// https://github.com/Dinoosauro/tiktok-to-ytdlp/blob/main/script.js
527+
function sanitizeName(name, modifyIfPosible = true) {
528+
if (typeof name !== "string") {
529+
throw new Error("Input must be string");
530+
}
531+
const replacement = "";
532+
const illegalRe = /[\/\?<>\\:\*\|"]/g;
533+
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
534+
const reservedRe = /^\.+$/;
535+
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
536+
const windowsTrailingRe = /[\. ]+$/;
537+
if (modifyIfPosible) {
538+
name = name
539+
.replaceAll("<", "‹")
540+
.replaceAll(">", "›")
541+
.replaceAll(":", "∶")
542+
.replaceAll('"', "″")
543+
.replaceAll("/", "∕")
544+
.replaceAll("\\", "∖")
545+
.replaceAll("|", "¦")
546+
.replaceAll("?", "¿");
547+
}
548+
const sanitized = name
549+
.replace(illegalRe, replacement)
550+
.replace(controlRe, replacement)
551+
.replace(reservedRe, replacement)
552+
.replace(windowsReservedRe, replacement)
553+
.replace(windowsTrailingRe, replacement);
554+
return sanitized; // TODO truncates to length of 255
555+
}
520556
function debounce(func, delay) {
521557
let timeout;
522558
return (...args) => {
523559
clearTimeout(timeout);
524560
timeout = setTimeout(() => func.apply(this, args), delay);
525561
};
526562
}
527-
// #endregion
528-
529-
// #region Utils
530563

531564
const numberFormatCached = {};
532565
/**

scripts/tiktok_batchDownload.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default {
224224
return checkbox;
225225
}
226226

227-
const sleep = UfsGlobal.Utils.sleep;
227+
const { sleep, sanitizeName } = UfsGlobal.Utils;
228228

229229
async function getLinkVideos(videoUrls) {
230230
if (!videoUrls.length) return;
@@ -263,14 +263,14 @@ export default {
263263
// conflictAction: "overwrite",
264264
// filename:
265265
// "tiktok/" +
266-
// sanitizeFileName(
266+
// sanitizeName(
267267
// CACHED.videoById.get(id)?.name || id
268268
// ) +
269269
// ".mp4",
270270
// });
271271
links.push({
272272
url: link,
273-
name: sanitizeFileName(cached?.name || id) + ".mp4",
273+
name: sanitizeName(cached?.name || id) + ".mp4",
274274
});
275275
} else {
276276
progressDiv.innerText = `[LỖI] Không thể tải video ${url}.`;
@@ -337,19 +337,3 @@ export default {
337337
},
338338
},
339339
};
340-
341-
function sanitizeFileName(fileName) {
342-
// Định nghĩa các ký tự hợp lệ (chữ cái, số, dấu gạch dưới, dấu gạch ngang và dấu chấm)
343-
const validChars = /^[a-zA-Z0-9_\-.]+$/;
344-
345-
// Lược bỏ các ký tự không hợp lệ
346-
let sanitizedFileName = "";
347-
for (let char of fileName) {
348-
if (validChars.test(char)) {
349-
sanitizedFileName += char;
350-
}
351-
}
352-
353-
// Trả về tên tệp đã lược bỏ các ký tự không hợp lệ
354-
return sanitizedFileName;
355-
}

0 commit comments

Comments
 (0)