From 99ce48be8c8b07bd7b3cf8167c9b19874cb19812 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 21 Dec 2021 15:34:59 +0000 Subject: [PATCH] Abstract electron download path behind an opaque id (#292) --- src/webcontents-handler.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/webcontents-handler.ts b/src/webcontents-handler.ts index 0434f1d..8523695 100644 --- a/src/webcontents-handler.ts +++ b/src/webcontents-handler.ts @@ -245,8 +245,13 @@ function onEditableContextMenu(ev: Event, params: ContextMenuParams) { ev.preventDefault(); } -ipcMain.on('userDownloadOpen', function(ev: IpcMainEvent, { path }) { - shell.openPath(path); +let userDownloadIndex = 0; +const userDownloadMap = new Map(); // Map from id to path +ipcMain.on('userDownloadAction', function(ev: IpcMainEvent, { id, open = false }) { + if (open) { + shell.openPath(userDownloadMap.get(id)); + } + userDownloadMap.delete(id); }); export default (webContents: WebContents): void => { @@ -270,8 +275,10 @@ export default (webContents: WebContents): void => { item.once('done', (event, state) => { if (state === 'completed') { const savePath = item.getSavePath(); + const id = userDownloadIndex++; + userDownloadMap.set(id, savePath); webContents.send('userDownloadCompleted', { - path: savePath, + id, name: path.basename(savePath), }); }