diff --git a/src/api/fs.ts b/src/api/fs.ts index 9be256685..a7255e765 100644 --- a/src/api/fs.ts +++ b/src/api/fs.ts @@ -5,9 +5,4 @@ export async function getDirFiles(dirPath: string) { const electronFiles = await Promise.all(files.map(getElectronFile)); return electronFiles; } -export { - isFolder, - removeFile, - removeFolder, - readTextFile, -} from '../services/fs'; +export { isFolder, moveFile, readTextFile } from '../services/fs'; diff --git a/src/preload.ts b/src/preload.ts index ae71ca4e7..fc123b84e 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -45,7 +45,7 @@ import { openDirectory, } from './api/common'; import { fixHotReloadNext12 } from './utils/preload'; -import { isFolder, getDirFiles, removeFile, removeFolder } from './api/fs'; +import { isFolder, getDirFiles, moveFile } from './api/fs'; import { convertHEIC, generateImageThumbnail } from './api/imageProcessor'; import { setupLogging } from './utils/logging'; import { @@ -105,6 +105,5 @@ windowObject['ElectronAPIs'] = { logRendererProcessMemoryUsage, registerForegroundEventListener, openDirectory, - removeFile, - removeFolder, + moveFile, }; diff --git a/src/services/fs.ts b/src/services/fs.ts index 9535e139e..d4e6de025 100644 --- a/src/services/fs.ts +++ b/src/services/fs.ts @@ -242,16 +242,20 @@ export async function readTextFile(filePath: string) { return await fs.readFile(filePath, 'utf-8'); } -export async function removeFile(filePath: string) { - if (!existsSync(filePath)) { - return; +export async function moveFile( + sourcePath: string, + destinationPath: string +): Promise { + if (!existsSync(sourcePath)) { + throw new Error('File does not exist'); } - await fs.unlink(filePath); -} - -export async function removeFolder(folderPath: string) { - if (!existsSync(folderPath)) { - return; + if (existsSync(destinationPath)) { + throw new Error('Destination file already exists'); } - fs.rmSync(folderPath, { recursive: true, force: true }); + // check if destination folder exists + const destinationFolder = path.dirname(destinationPath); + if (!existsSync(destinationFolder)) { + throw new Error('Destination folder does not exist'); + } + await fs.rename(sourcePath, destinationPath); }