Audit rename

This commit is contained in:
Manav Rathi 2024-04-14 09:27:52 +05:30
parent b977f982dd
commit 72dd47fa7b
No known key found for this signature in database
5 changed files with 19 additions and 23 deletions

View file

@ -8,6 +8,9 @@ import { Readable } from "node:stream";
export const fsExists = (path: string) => existsSync(path);
export const fsRename = (oldPath: string, newPath: string) =>
fs.rename(oldPath, newPath);
/**
* Write a (web) ReadableStream to a file at the given {@link filePath}.
*
@ -115,11 +118,6 @@ export const deleteFolder = async (folderPath: string) => {
await fs.rmdir(folderPath);
};
export const rename = async (oldPath: string, newPath: string) => {
if (!existsSync(oldPath)) throw new Error("Path does not exist");
await fs.rename(oldPath, newPath);
};
export const deleteFile = async (filePath: string) => {
// Ensure it exists
if (!existsSync(filePath)) return;

View file

@ -22,10 +22,10 @@ import {
deleteFile,
deleteFolder,
fsExists,
fsRename,
isFolder,
moveFile,
readTextFile,
rename,
saveFileToDisk,
saveStreamToDisk,
} from "./fs";
@ -169,6 +169,10 @@ export const attachIPCHandlers = () => {
ipcMain.handle("fsExists", (_, path) => fsExists(path));
ipcMain.handle("fsRename", (_, oldPath: string, newPath: string) =>
fsRename(oldPath, newPath),
);
// - FS Legacy
ipcMain.handle("checkExistsAndCreateDir", (_, dirPath) =>
@ -197,10 +201,6 @@ export const attachIPCHandlers = () => {
ipcMain.handle("deleteFile", (_, path: string) => deleteFile(path));
ipcMain.handle("rename", (_, oldPath: string, newPath: string) =>
rename(oldPath, newPath),
);
// - Upload
ipcMain.handle("getPendingUploads", () => getPendingUploads());

View file

@ -99,6 +99,9 @@ const skipAppUpdate = (version: string) => {
const fsExists = (path: string): Promise<boolean> =>
ipcRenderer.invoke("fsExists", path);
const fsRename = (oldPath: string, newPath: string): Promise<void> =>
ipcRenderer.invoke("fsRename", oldPath, newPath);
// - AUDIT below this
// - Conversion
@ -244,9 +247,6 @@ const deleteFolder = (path: string): Promise<void> =>
const deleteFile = (path: string): Promise<void> =>
ipcRenderer.invoke("deleteFile", path);
const rename = (oldPath: string, newPath: string): Promise<void> =>
ipcRenderer.invoke("rename", oldPath, newPath);
// - Upload
const getPendingUploads = (): Promise<{
@ -348,6 +348,7 @@ contextBridge.exposeInMainWorld("electron", {
// - FS
fs: {
exists: fsExists,
rename: fsRename,
},
// - FS legacy
@ -360,7 +361,6 @@ contextBridge.exposeInMainWorld("electron", {
moveFile,
deleteFolder,
deleteFile,
rename,
// - Upload

View file

@ -513,7 +513,7 @@ class ExportService {
newCollectionExportName,
);
try {
await ensureElectron().rename(
await ensureElectron().fs.rename(
oldCollectionExportPath,
newCollectionExportPath,
);
@ -1168,7 +1168,7 @@ class ExportService {
};
rename = (oldPath: string, newPath: string) => {
return ensureElectron().rename(oldPath, newPath);
return ensureElectron().fs.rename(oldPath, newPath);
};
checkExistsAndCreateDir = (path: string) => {

View file

@ -155,15 +155,14 @@ export interface Electron {
* or watching some folders for changes and syncing them automatically.
*
* Towards this end, this fs object provides some generic file system access
* functions that are needed for such features. In addition, there are other
* feature specific methods too in the top level electron object.
* functions that are needed for such features (in some cases, there are
* other feature specific methods too in the top level electron object).
*/
fs: {
/**
* Return true if there is a file or directory at the given
* {@link path}.
*/
/** Return true if there is an item at the given {@link path}. */
exists: (path: string) => Promise<boolean>;
/** Rename {@link oldPath} to {@link newPath} */
rename: (oldPath: string, newPath: string) => Promise<void>;
};
/*
@ -287,7 +286,6 @@ export interface Electron {
moveFile: (oldPath: string, newPath: string) => Promise<void>;
deleteFolder: (path: string) => Promise<void>;
deleteFile: (path: string) => Promise<void>;
rename: (oldPath: string, newPath: string) => Promise<void>;
// - Upload