Skip to content

best way to save what you love

api/create-filename: relax sanitizeString and use fullwidth replacements

jj2eadc3fbd8fa
diff --git a/api/src/processing/create-filename.js b/api/src/processing/create-filename.js
index 911b5603..26a85c1d 100644
--- a/api/src/processing/create-filename.js
+++ b/api/src/processing/create-filename.js
@@ -1,10 +1,25 @@
-const illegalCharacters = ['}', '{', '%', '>', '<', '^', ';', ':', '`', '$', '"', "@", '=', '?', '|', '*'];
+// characters that are disallowed on windows:
+// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
+const characterMap = {
+ '<': '<',
+ '>': '>',
+ ':': ':',
+ '"': '"',
+ '/': '/',
+ '\\': '\',
+ '|': '|',
+ '?': '?',
+ '*': '*'
+};
-const sanitizeString = (string) => {
- for (const i in illegalCharacters) {
- string = string.replaceAll("/", "_").replaceAll("\\", "_")
- .replaceAll(illegalCharacters[i], '')
+export const sanitizeString = (string) => {
+ // remove any potential control characters the string might contain
+ string = string.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
+
+ for (const [ char, replacement ] of Object.entries(characterMap)) {
+ string = string.replaceAll(char, replacement);
}
+
return string;
}