Skip to content

Commit

Permalink
add empty dir button
Browse files Browse the repository at this point in the history
  • Loading branch information
makamekm committed Apr 23, 2020
1 parent dfc23a6 commit 6a91e8c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 17 deletions.
16 changes: 16 additions & 0 deletions src/app/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface SettingsState {
isLoadingDelay: boolean;
load: () => Promise<void>;
save: () => Promise<void>;
empty: () => Promise<void>;
}

const SettingsForm = observer(({ state }: { state: SettingsState }) => {
Expand Down Expand Up @@ -98,6 +99,16 @@ const SettingsForm = observer(({ state }: { state: SettingsState }) => {
Remount Drive
</Button>
</ButtonGroup>
<ButtonGroup className="align-self-start mt-0 mb-3">
<Button
disabled={state.isDirty}
color="danger"
className="mb-2 mr-2 px-3"
onClick={state.empty}
>
Empty Drive
</Button>
</ButtonGroup>
</Col>
</FormGroup>
</Form>
Expand Down Expand Up @@ -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);
Expand Down
71 changes: 55 additions & 16 deletions src/electron/drive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ export const readDir = (dir) =>
});
});

export const rmDir = (dir) =>
new Promise<void>((r, e) => {
drive.rmdir(
dir,
{
recursive: true,
},
(err) => {
if (err) {
e(err);
} else {
r();
}
}
);
});
// export const rmDir = (dir) =>
// new Promise<void>((r, e) => {
// drive.rmdir(
// dir,
// {
// recursive: true,
// },
// (err) => {
// if (err) {
// e(err);
// } else {
// r();
// }
// }
// );
// });

export const unlink = (file) =>
new Promise<void>((r, e) => {
Expand All @@ -107,6 +107,45 @@ export const isExist = (file) =>
});
});

export const rmDir = (file) =>
new Promise<void>((r, e) => {
drive.rmdir(file, (err) => {
if (err) {
e(err);
} else {
r();
}
});
});

export const stat = (file) =>
new Promise<any>((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 {
Expand Down
13 changes: 12 additions & 1 deletion src/electron/handlers/get-drive-config.handler.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -30,3 +30,14 @@ ipcMain.handle(
ipc.sends.ON_DRIVE_CONFIG_UPDATE_FINISH();
}
);

ipcMain.handle(
nameofHandler("EMPTY_DRIVE_CONFIG"),
async (
event,
...args: Parameters<Ipc["handlers"]["EMPTY_DRIVE_CONFIG"]>
): Promise<ReturnType<Ipc["handlers"]["EMPTY_DRIVE_CONFIG"]>> => {
await emptyDir("/");
ipc.sends.ON_DRIVE_CONFIG_UPDATE_FINISH();
}
);
5 changes: 5 additions & 0 deletions src/shared/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export interface Ipc {
secretKey: string;
useDriveSwarm: boolean;
}) => void;
EMPTY_DRIVE_CONFIG: () => void;
};
sends: {
ON_COLLECT_STATS: () => void;
Expand Down Expand Up @@ -220,6 +221,10 @@ export const ipc = {
...args: Parameters<Ipc["handlers"]["SAVE_DRIVE_CONFIG"]>
): Promise<ReturnType<Ipc["handlers"]["SAVE_DRIVE_CONFIG"]>> =>
ipcRenderer.invoke(nameofHandler("SAVE_DRIVE_CONFIG"), ...args),
EMPTY_DRIVE_CONFIG: (
...args: Parameters<Ipc["handlers"]["EMPTY_DRIVE_CONFIG"]>
): Promise<ReturnType<Ipc["handlers"]["EMPTY_DRIVE_CONFIG"]>> =>
ipcRenderer.invoke(nameofHandler("EMPTY_DRIVE_CONFIG"), ...args),
},
sends: {
ON_COLLECT_STATS: (value: boolean) =>
Expand Down

0 comments on commit 6a91e8c

Please sign in to comment.