Fix/rename-collection-support-wsl (#2892)

* fix: normalize wsl path for rename item

* added isWSLPath, normalizeWslPath

* revert normalize action on actions.js

* added WSL path checking and apply Win UNC normalize
This commit is contained in:
Pragadesh-45 2024-08-23 16:36:54 +05:30 committed by GitHub
parent 8b76ecede3
commit 0b9554c8cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 2 deletions

View File

@ -374,6 +374,7 @@ export const newFolder = (folderName, collectionUid, itemUid) => (dispatch, getS
}); });
}; };
// rename item
export const renameItem = (newName, itemUid, collectionUid) => (dispatch, getState) => { export const renameItem = (newName, itemUid, collectionUid) => (dispatch, getState) => {
const state = getState(); const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid); const collection = findCollectionByUid(state.collections.collections, collectionUid);
@ -718,7 +719,7 @@ export const newHttpRequest = (params) => (dispatch, getState) => {
const pathParams = parsePathParams(requestUrl); const pathParams = parsePathParams(requestUrl);
each(pathParams, (pathParm) => { each(pathParams, (pathParm) => {
pathParams.enabled = true; pathParams.enabled = true;
pathParm.type = 'path' pathParm.type = 'path';
}); });
const params = [...queryParams, ...pathParams]; const params = [...queryParams, ...pathParams];

View File

@ -13,7 +13,9 @@ const {
browseFiles, browseFiles,
createDirectory, createDirectory,
searchForBruFiles, searchForBruFiles,
sanitizeDirectoryName sanitizeDirectoryName,
isWSLPath,
normalizeWslPath,
} = 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');
@ -326,6 +328,14 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
// rename item // rename item
ipcMain.handle('renderer:rename-item', async (event, oldPath, newPath, newName) => { ipcMain.handle('renderer:rename-item', async (event, oldPath, newPath, newName) => {
try { try {
// Normalize paths if they are WSL paths
if (isWSLPath(oldPath)) {
oldPath = normalizeWslPath(oldPath);
}
if (isWSLPath(newPath)) {
newPath = normalizeWslPath(newPath);
}
if (!fs.existsSync(oldPath)) { if (!fs.existsSync(oldPath)) {
throw new Error(`path: ${oldPath} does not exist`); throw new Error(`path: ${oldPath} does not exist`);
} }

View File

@ -50,6 +50,18 @@ const normalizeAndResolvePath = (pathname) => {
return path.resolve(pathname); return path.resolve(pathname);
}; };
function isWSLPath(pathname) {
// Check if the path starts with the WSL prefix
// eg. "\\wsl.localhost\Ubuntu\home\user\bruno\collection\scripting\api\req\getHeaders.bru"
return pathname.startsWith('/wsl.localhost/') || pathname.startsWith('\\wsl.localhost\\');
}
function normalizeWslPath(pathname) {
// Replace the WSL path prefix and convert forward slashes to backslashes
// This is done to achieve WSL paths (linux style) to Windows UNC equivalent (Universal Naming Conversion)
return pathname.replace(/^\/wsl.localhost/, '\\\\wsl.localhost').replace(/\//g, '\\');
}
const writeFile = async (pathname, content) => { const writeFile = async (pathname, content) => {
try { try {
fs.writeFileSync(pathname, content, { fs.writeFileSync(pathname, content, {
@ -143,6 +155,8 @@ const searchForBruFiles = (dir) => {
return searchForFiles(dir, '.bru'); return searchForFiles(dir, '.bru');
}; };
// const isW
const sanitizeDirectoryName = (name) => { const sanitizeDirectoryName = (name) => {
return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-'); return name.replace(/[<>:"/\\|?*\x00-\x1F]+/g, '-');
}; };
@ -154,6 +168,8 @@ module.exports = {
isFile, isFile,
isDirectory, isDirectory,
normalizeAndResolvePath, normalizeAndResolvePath,
isWSLPath,
normalizeWslPath,
writeFile, writeFile,
writeBinaryFile, writeBinaryFile,
hasJsonExtension, hasJsonExtension,