Fix/invalid file name handling (#3274)

* feat: implement utility function `isValidFilename`
* refactor: added filename validator checks for `rename-item` and `new-request`
* chore: added `fileName.startsWith('.')`
This commit is contained in:
Pragadesh-45
2024-11-29 12:04:59 +05:30
committed by GitHub
parent 39a44e9b4f
commit 707cddea90
2 changed files with 25 additions and 3 deletions

View File

@@ -160,6 +160,20 @@ const sanitizeDirectoryName = (name) => {
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
};
const isValidFilename = (fileName) => {
const inValidChars = /[\\/:*?"<>|]/;
if (!fileName || inValidChars.test(fileName)) {
return false;
}
if (fileName.endsWith(' ') || fileName.endsWith('.') || fileName.startsWith('.')) {
return false;
}
return true;
};
const safeToRename = (oldPath, newPath) => {
try {
// If the new path doesn't exist, it's safe to rename
@@ -204,5 +218,6 @@ module.exports = {
searchForFiles,
searchForBruFiles,
sanitizeDirectoryName,
safeToRename
safeToRename,
isValidFilename
};