Skip to content

Commit

Permalink
feat: 同时选择多个文件夹一次进行扫描
Browse files Browse the repository at this point in the history
  • Loading branch information
yubaoquan committed Apr 15, 2024
1 parent 487ccf8 commit 97df6cd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
17 changes: 9 additions & 8 deletions src/main/fs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ export const getDirs = async (rootDir = '/') => {
};

export const scanDuplicatedFiles = async ({
dir,
dirs,
beforeHash,
afterHash,
beforeAll,
}: {
dir: string;
dirs: string[];
beforeHash?: (node: any) => void;
afterHash: (node: any, hash: string) => void;
beforeAll: (nodes: any[]) => void;
}) => {
if (!dir) return [];
if (!dirs.length) return [];
type Task = {
node: any;
start: () => Promise<void>;
};
const tasks: Task[] = [];
const tree = dirTree(dir, { attributes: ['size', 'type'] });

const ret = {};

Expand Down Expand Up @@ -91,14 +90,16 @@ export const scanDuplicatedFiles = async ({
});
};

walk(tree.children);
dirs
.map((dir) => dirTree(dir, { attributes: ['size', 'type'] }))
.forEach((tree) => walk(tree.children));

const allNodesToScan = tasks.map((task) => task.node);
beforeAll(allNodesToScan);

await Promise.all(tasks.map((task) => task.start()));

console.info(`ret`, ret);
console.info(`tree`, tree);

return ret;
};
Expand All @@ -115,10 +116,10 @@ export const batchDeleteFiles = async (filePaths: string[]) => {

export const selectFolder = async () => {
const folderPaths = dialog.showOpenDialogSync({
properties: ['openDirectory'],
properties: ['openDirectory', 'multiSelections'],
});

return folderPaths?.[0];
return folderPaths || [];
};

export const openFolder = (folderPath: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/main/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const handleGetFolders = async (ipcMain) => {
};

const handleDuplicatedScanFiles = async ({ ipcMain, mainWindow }) => {
ipcMain.handle(RendererMessage.ScanDuplicatedFiles, async (_event, folderPath) => {
ipcMain.handle(RendererMessage.ScanDuplicatedFiles, async (_event, folderPaths) => {
const result = await scanDuplicatedFiles({
dir: folderPath,
dirs: folderPaths,
beforeAll(nodes) {
mainWindow.webContents.send(MainMessage.BeforeAllHash, nodes);
},
Expand Down
4 changes: 2 additions & 2 deletions src/preload/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { ElectronAPI } from '@electron-toolkit/preload'
type API = {
ping: () => Promise<string>
getFolders: (dir?: string) => Promise<any[]>
scanDuplicatedFiles: (dir: string) => Promise<any[]>
scanDuplicatedFiles: (dir: string[]) => Promise<any[]>
deleteFiles: (files: string[]) => Promise<boolean>
openDevTools: (open: boolean) => void
listenFromMain: (channel: string, listener: (...args: any[]) => void) => void
selectFolder: () => Promise<string>
selectFolder: () => Promise<string[]>
openFolder: (folder: string) => void
toggleTheme: () => Promise<boolean>
getIsDark: () => Promise<boolean>
Expand Down
4 changes: 2 additions & 2 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { RendererMessage } from '../common/message';
const api = {
ping: () => ipcRenderer.invoke(RendererMessage.Ping),
getFolders: (folder: string) => ipcRenderer.invoke(RendererMessage.GetFolders, folder),
scanDuplicatedFiles: (folder: string) =>
ipcRenderer.invoke(RendererMessage.ScanDuplicatedFiles, folder),
scanDuplicatedFiles: (folders: string[]) =>
ipcRenderer.invoke(RendererMessage.ScanDuplicatedFiles, folders),
deleteFiles: (files: string[]) => ipcRenderer.invoke(RendererMessage.DeleteFiles, files),
openDevTools: (open: boolean) => ipcRenderer.invoke(RendererMessage.OpenDevTools, open),
listenFromMain: (channel: string, listener: (...args: any[]) => void) => {
Expand Down
33 changes: 27 additions & 6 deletions src/renderer/src/views/dutor-view.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,25 @@
$t('dutor.selectFolder')
}}</v-btn>
</v-col>
<v-col cols="8">{{ targetFolder }}</v-col>
<v-col cols="8">
<div
v-for="folderPath in targetFolders"
:key="folderPath"
class="flex justify-between items-center mb-2"
>
<span>{{ folderPath }}</span>
<v-btn
density="compact"
icon="mdi-close"
@click="handleDeleteTargetFolderClick(folderPath)"
></v-btn>
</div>
</v-col>
<v-col cols="2" class="text-right">
<v-btn
color="primary"
:loading="isScanning"
:disabled="!targetFolder"
:disabled="!targetFolders.length"
@click="handleScanClick"
>{{ $t('dutor.startScan') }}</v-btn
>
Expand Down Expand Up @@ -92,7 +105,7 @@ const {
// startFakeData,
} = useScanProgress();
const targetFolder = ref<string>('');
const targetFolders = ref<string[]>([]);
const isScanning = ref(false);
const showMessage = ref(false);
const toastMessage = ref<string>('');
Expand All @@ -113,7 +126,7 @@ provide('toast', toast);
const handleScanClick = async () => {
isScanning.value = true;
resetProgress();
const result = await window.api.scanDuplicatedFiles(targetFolder.value);
const result = await window.api.scanDuplicatedFiles(toRaw(targetFolders.value));
isScanning.value = false;
filesGroups.value = Object.entries(result)
Expand Down Expand Up @@ -169,8 +182,16 @@ const handleDeleteAllConfirm = () => {
};
const handleSelectFolderClick = async () => {
const folderPath = await window.api.selectFolder();
targetFolder.value = folderPath;
const folderPaths = await window.api.selectFolder();
folderPaths.forEach((folderPath) => {
if (!targetFolders.value.includes(folderPath)) {
targetFolders.value.push(folderPath);
}
});
};
const handleDeleteTargetFolderClick = (folderPath: string) => {
targetFolders.value = targetFolders.value.filter((path) => path !== folderPath);
};
window.api.listenFromMain(MainMessage.BeforeHash, (path) => {
Expand Down

0 comments on commit 97df6cd

Please sign in to comment.