Merge branch 'main' into feature/makefile

This commit is contained in:
Yash 2024-11-29 19:56:33 +05:30
commit 3b3ebcef9b
2 changed files with 49 additions and 11 deletions

View File

@ -1,5 +1,7 @@
const _ = require('lodash'); const _ = require('lodash');
const fs = require('fs'); const fs = require('fs');
const fsExtra = require('fs-extra');
const os = require('os');
const path = require('path'); const path = require('path');
const { ipcMain, shell, dialog, app } = require('electron'); const { ipcMain, shell, dialog, app } = require('electron');
const { envJsonToBru, bruToJson, jsonToBru, jsonToCollectionBru } = require('../bru'); const { envJsonToBru, bruToJson, jsonToBru, jsonToCollectionBru } = require('../bru');
@ -17,7 +19,9 @@ const {
isWSLPath, isWSLPath,
normalizeWslPath, normalizeWslPath,
normalizeAndResolvePath, normalizeAndResolvePath,
safeToRename safeToRename,
isWindowsOS,
isValidFilename
} = require('../utils/filesystem'); } = require('../utils/filesystem');
const { openCollectionDialog } = require('../app/collections'); const { openCollectionDialog } = require('../app/collections');
const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON } = require('../utils/common'); const { generateUidBasedOnHash, stringifyJson, safeParseJSON, safeStringifyJSON } = require('../utils/common');
@ -201,7 +205,9 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
if (fs.existsSync(pathname)) { if (fs.existsSync(pathname)) {
throw new Error(`path: ${pathname} already exists`); 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); const content = jsonToBru(request);
await writeFile(pathname, content); await writeFile(pathname, content);
} catch (error) { } catch (error) {
@ -358,23 +364,35 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
const newBruFilePath = bruFile.replace(oldPath, newPath); const newBruFilePath = bruFile.replace(oldPath, newPath);
moveRequestUid(bruFile, newBruFilePath); moveRequestUid(bruFile, newBruFilePath);
} }
return fs.renameSync(oldPath, newPath);
if (isWindowsOS() && !isWSLPath(oldPath)) {
const tempDir = path.join(os.tmpdir(), `temp-folder-${Date.now()}`);
await fsExtra.copy(oldPath, tempDir);
await fsExtra.move(tempDir, newPath, { overwrite: true });
await fsExtra.remove(oldPath);
} else {
await fs.rename(oldPath, newPath);
}
return newPath;
} }
const isBru = hasBruExtension(oldPath); if (!hasBruExtension(oldPath)) {
if (!isBru) {
throw new Error(`path: ${oldPath} is not a bru file`); throw new Error(`path: ${oldPath} is not a bru file`);
} }
// update name in file and save new copy, then delete old copy if (!isValidFilename(newName)) {
const data = fs.readFileSync(oldPath, 'utf8'); throw new Error(`path: ${newName} is not a valid filename`);
const jsonData = bruToJson(data); }
// update name in file and save new copy, then delete old copy
const data = await fs.promises.readFile(oldPath, 'utf8'); // Use async read
const jsonData = bruToJson(data);
jsonData.name = newName; jsonData.name = newName;
moveRequestUid(oldPath, newPath); moveRequestUid(oldPath, newPath);
const content = jsonToBru(jsonData); const content = jsonToBru(jsonData);
await fs.unlinkSync(oldPath); await fs.promises.unlink(oldPath);
await writeFile(newPath, content); await writeFile(newPath, content);
return newPath; return newPath;

View File

@ -160,6 +160,24 @@ const sanitizeDirectoryName = (name) => {
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-'); return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
}; };
const isWindowsOS = () => {
return os.platform() === 'win32';
}
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) => { const safeToRename = (oldPath, newPath) => {
try { try {
// If the new path doesn't exist, it's safe to rename // If the new path doesn't exist, it's safe to rename
@ -170,7 +188,7 @@ const safeToRename = (oldPath, newPath) => {
const oldStat = fs.statSync(oldPath); const oldStat = fs.statSync(oldPath);
const newStat = fs.statSync(newPath); const newStat = fs.statSync(newPath);
if (os.platform() === 'win32') { if (isWindowsOS()) {
// Windows-specific comparison: // Windows-specific comparison:
// Check if both files have the same birth time, size (Since, Win FAT-32 doesn't use inodes) // Check if both files have the same birth time, size (Since, Win FAT-32 doesn't use inodes)
@ -204,5 +222,7 @@ module.exports = {
searchForFiles, searchForFiles,
searchForBruFiles, searchForBruFiles,
sanitizeDirectoryName, sanitizeDirectoryName,
safeToRename isWindowsOS,
safeToRename,
isValidFilename
}; };