refactored sanitizeFilename()
This commit is contained in:
@@ -271,20 +271,30 @@
|
|||||||
|
|
||||||
const lastDot = filenameWithoutPath.lastIndexOf(".");
|
const lastDot = filenameWithoutPath.lastIndexOf(".");
|
||||||
const extension = lastDot !== -1 ? filenameWithoutPath.slice(lastDot) : "";
|
const extension = lastDot !== -1 ? filenameWithoutPath.slice(lastDot) : "";
|
||||||
let filenameWithoutPathAndExtension =
|
let nameOnly =
|
||||||
lastDot !== -1
|
lastDot !== -1
|
||||||
? filenameWithoutPath.slice(0, lastDot)
|
? filenameWithoutPath.slice(0, lastDot)
|
||||||
: filenameWithoutPath;
|
: filenameWithoutPath;
|
||||||
|
|
||||||
let cleanedFilename = filenameWithoutPathAndExtension
|
const charMap = {
|
||||||
.replace(/ /g, "_")
|
Ä: "Ae",
|
||||||
.replace(/Ä/g, "Ae")
|
ä: "ae",
|
||||||
.replace(/ä/g, "ae")
|
Ö: "Oe",
|
||||||
.replace(/Ö/g, "Oe")
|
ö: "oe",
|
||||||
.replace(/ö/g, "oe")
|
Ü: "Ue",
|
||||||
.replace(/Ü/g, "Ue")
|
ü: "ue",
|
||||||
.replace(/ü/g, "ue")
|
ß: "ss",
|
||||||
.replace(/ß/g, "ss");
|
};
|
||||||
|
|
||||||
|
let cleanedFilename = nameOnly.replace(/./g, (char) => {
|
||||||
|
if (charMap[char]) {
|
||||||
|
return charMap[char];
|
||||||
|
}
|
||||||
|
if (char === " ") {
|
||||||
|
return "_";
|
||||||
|
}
|
||||||
|
return char;
|
||||||
|
});
|
||||||
|
|
||||||
cleanedFilename = cleanedFilename.replace(/[^a-zA-Z0-9._-]+/g, "_");
|
cleanedFilename = cleanedFilename.replace(/[^a-zA-Z0-9._-]+/g, "_");
|
||||||
|
|
||||||
@@ -294,9 +304,9 @@
|
|||||||
|
|
||||||
cleanedFilename = cleanedFilename.replace(/^_+|_+$/g, "");
|
cleanedFilename = cleanedFilename.replace(/^_+|_+$/g, "");
|
||||||
|
|
||||||
const maxLenFilename = 128;
|
const MAX_LEN = 128;
|
||||||
if (cleanedFilename.length > maxLenFilename) {
|
if (cleanedFilename.length > MAX_LEN) {
|
||||||
cleanedFilename = cleanedFilename.slice(0, maxLenFilename);
|
cleanedFilename = cleanedFilename.slice(0, MAX_LEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
return cleanedFilename + extension;
|
return cleanedFilename + extension;
|
||||||
|
Reference in New Issue
Block a user