From 6a91e8cea0c07a96c3d76945a7b417620bb284d3 Mon Sep 17 00:00:00 2001 From: makame Date: Fri, 24 Apr 2020 00:19:07 +0200 Subject: [PATCH] add empty dir button --- src/app/Settings/Settings.tsx | 16 +++++ src/electron/drive.ts | 71 ++++++++++++++----- .../handlers/get-drive-config.handler.ts | 13 +++- src/shared/ipc.ts | 5 ++ 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/src/app/Settings/Settings.tsx b/src/app/Settings/Settings.tsx index 4e37bcf..e1849f1 100755 --- a/src/app/Settings/Settings.tsx +++ b/src/app/Settings/Settings.tsx @@ -35,6 +35,7 @@ interface SettingsState { isLoadingDelay: boolean; load: () => Promise; save: () => Promise; + empty: () => Promise; } const SettingsForm = observer(({ state }: { state: SettingsState }) => { @@ -98,6 +99,16 @@ const SettingsForm = observer(({ state }: { state: SettingsState }) => { Remount Drive + + + @@ -126,6 +137,11 @@ export const Settings = observer(() => { state.isDirty = false; state.isLoading = false; }, + empty: async () => { + state.isLoading = true; + await ipc.handlers.EMPTY_DRIVE_CONFIG(); + state.isLoading = false; + }, })); useOnLoad(state.load); diff --git a/src/electron/drive.ts b/src/electron/drive.ts index f2208c9..bc9ab06 100644 --- a/src/electron/drive.ts +++ b/src/electron/drive.ts @@ -72,22 +72,22 @@ export const readDir = (dir) => }); }); -export const rmDir = (dir) => - new Promise((r, e) => { - drive.rmdir( - dir, - { - recursive: true, - }, - (err) => { - if (err) { - e(err); - } else { - r(); - } - } - ); - }); +// export const rmDir = (dir) => +// new Promise((r, e) => { +// drive.rmdir( +// dir, +// { +// recursive: true, +// }, +// (err) => { +// if (err) { +// e(err); +// } else { +// r(); +// } +// } +// ); +// }); export const unlink = (file) => new Promise((r, e) => { @@ -107,6 +107,45 @@ export const isExist = (file) => }); }); +export const rmDir = (file) => + new Promise((r, e) => { + drive.rmdir(file, (err) => { + if (err) { + e(err); + } else { + r(); + } + }); + }); + +export const stat = (file) => + new Promise((r, e) => { + drive.stat(file, (err, stats) => { + if (err) { + e(err); + } else { + r(stats); + } + }); + }); + +export const emptyDir = async (dirPath) => { + const files = await readDir(dirPath); + if (files.length > 0) + for (let file of files) { + const filePath = (dirPath === "/" ? dirPath : dirPath + "/") + file; + const stats = await stat(filePath); + if (stats.isFile()) { + await unlink(filePath); + } else { + await emptyDir(filePath); + } + } + if (dirPath !== "/") { + await rmDir(dirPath); + } +}; + export const closeDrive = () => { if (drive) { try { diff --git a/src/electron/handlers/get-drive-config.handler.ts b/src/electron/handlers/get-drive-config.handler.ts index 5479047..5aef1fc 100644 --- a/src/electron/handlers/get-drive-config.handler.ts +++ b/src/electron/handlers/get-drive-config.handler.ts @@ -1,7 +1,7 @@ import { ipcMain } from "electron"; import { nameofHandler, Ipc, ipc } from "~/shared/ipc"; -import { saveDriveConfig, getDriveConfig } from "../drive"; +import { saveDriveConfig, getDriveConfig, emptyDir } from "../drive"; import { getCollectPromise } from "./collect-stats.handler"; ipcMain.handle( @@ -30,3 +30,14 @@ ipcMain.handle( ipc.sends.ON_DRIVE_CONFIG_UPDATE_FINISH(); } ); + +ipcMain.handle( + nameofHandler("EMPTY_DRIVE_CONFIG"), + async ( + event, + ...args: Parameters + ): Promise> => { + await emptyDir("/"); + ipc.sends.ON_DRIVE_CONFIG_UPDATE_FINISH(); + } +); diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts index a88ca19..8a068fb 100644 --- a/src/shared/ipc.ts +++ b/src/shared/ipc.ts @@ -142,6 +142,7 @@ export interface Ipc { secretKey: string; useDriveSwarm: boolean; }) => void; + EMPTY_DRIVE_CONFIG: () => void; }; sends: { ON_COLLECT_STATS: () => void; @@ -220,6 +221,10 @@ export const ipc = { ...args: Parameters ): Promise> => ipcRenderer.invoke(nameofHandler("SAVE_DRIVE_CONFIG"), ...args), + EMPTY_DRIVE_CONFIG: ( + ...args: Parameters + ): Promise> => + ipcRenderer.invoke(nameofHandler("EMPTY_DRIVE_CONFIG"), ...args), }, sends: { ON_COLLECT_STATS: (value: boolean) =>