refactor: added filename validator checks for rename-item and new-request

This commit is contained in:
Pragadesh-45 2024-10-09 12:15:35 +05:30
parent 79f6a844c8
commit b0040e86d0

View File

@ -17,7 +17,8 @@ const {
isWSLPath,
normalizeWslPath,
normalizeAndResolvePath,
safeToRename
safeToRename,
isValidFilename
} = require('../utils/filesystem');
const { openCollectionDialog } = require('../app/collections');
const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON } = require('../utils/common');
@ -195,7 +196,9 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
if (fs.existsSync(pathname)) {
throw new Error(`path: ${pathname} already exists`);
}
if (!isValidFilename(request.name)) {
throw new Error(`path: ${request.name}.bru is not a valid filename`);
}
const content = jsonToBru(request);
await writeFile(pathname, content);
} catch (error) {
@ -360,6 +363,10 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
throw new Error(`path: ${oldPath} is not a bru file`);
}
if (!isValidFilename(newName)) {
throw new Error(`path: ${newName} is not a valid filename`);
}
// update name in file and save new copy, then delete old copy
const data = fs.readFileSync(oldPath, 'utf8');
const jsonData = bruToJson(data);