Skip to content

Commit

Permalink
move sanitize filename to shared
Browse files Browse the repository at this point in the history
  • Loading branch information
steam0r committed Apr 3, 2024
1 parent ce8cd4d commit 2219600
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions shared/api/utils/shared_files_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import path from "path";
import moment from "moment";
import sizeOfImage from "image-size";
import sanitizeFileName from "sanitize-filename";
import { UtilProvider } from "./util_provider.js";
import SharedUtil from "./shared_util.js";

Expand Down Expand Up @@ -174,4 +175,37 @@ export default class SharedFilesUtil extends SharedUtil

return icon;
}

realSanitizeFilename(_filename)
{
let filename = sanitizeFileName(_filename);
// eslint-disable-next-line no-control-regex
filename = filename.replace(/[^\x00-\x7F]/g, "_");
filename = filename.replace(/ /g, "_");
filename = filename.replace(/&/g, "_");
filename = filename.replace(/;/g, "_");
filename = filename.replace(/'/g, "_");
filename = filename.replace(/!/g, "_");
filename = filename.replace(/#/g, "_");
filename = filename.replace(/\$/g, "_");
filename = filename.replace(/\(/g, "_");
filename = filename.replace(/\)/g, "_");

const suffix = this.getSuffix(filename);
if (suffix) filename = filename.replace(suffix.toUpperCase(), suffix); // force lowercase file extensions

return filename;
}

getSuffix(filename)
{
let suffix = null;

if (!suffix)
for (const k in this.FILETYPES)
for (let j = 0; j < this.FILETYPES[k].length; j++)
if (filename.toLowerCase().endsWith(this.FILETYPES[k][j])) suffix = this.FILETYPES[k][j];

return suffix;
}
}

0 comments on commit 2219600

Please sign in to comment.