electron-dl-manager
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -166,4 +166,4 @@ "use strict"; | ||
offline: false, | ||
upload: -1, | ||
download: -1, | ||
downloadThroughput: -1, | ||
uploadThroughput: -1, | ||
latency: 0, | ||
@@ -182,23 +182,19 @@ }); | ||
*/ | ||
onWillDownload(id, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
onWillDownload(id, { directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
return async (event, item, webContents) => { | ||
item.pause(); | ||
const cancel = async () => { | ||
item.cancel(); | ||
if (callbacks.onDownloadCancelled) { | ||
this.log(`[${id}] Calling onDownloadCancelled`); | ||
const start = async () => { | ||
if (callbacks.onDownloadStarted) { | ||
this.log(`[${id}] Calling onDownloadStarted`); | ||
try { | ||
await callbacks.onDownloadCancelled({ | ||
id, | ||
await callbacks.onDownloadStarted({ | ||
...downloadData, | ||
item, | ||
event, | ||
webContents, | ||
percentCompleted: 0, | ||
resolvedFilename: item.getFilename(), | ||
cancelledFromSaveAsDialog: true, | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadCancelled: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents, cancelledFromSaveAsDialog: true }); | ||
this.log(`[${id}] Error during onDownloadStarted: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents }); | ||
} | ||
@@ -224,20 +220,6 @@ } | ||
this.log(`[${id}] Prompting save as dialog`); | ||
let result; | ||
try { | ||
result = await electron_1.dialog.showSaveDialog(window, { defaultPath: filePath, ...saveDialogOptions }); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error while showing save dialog: ${e}`); | ||
this.handleError(callbacks, e, { item, event, webContents }); | ||
await cancel(); | ||
return; | ||
} | ||
if (result.canceled) { | ||
this.log(`[${id}] User cancelled save as dialog`); | ||
await cancel(); | ||
return; | ||
} | ||
else { | ||
item.setSavePath(result.filePath); | ||
} | ||
// This actually isn't what shows the save dialog | ||
// If item.setSavePath() isn't called at all after some tiny period of time, | ||
// then the save dialog will show up, and it will use the options we set it to here | ||
item.setSaveDialogOptions({ ...saveDialogOptions, defaultPath: filePath }); | ||
} | ||
@@ -251,3 +233,2 @@ else { | ||
this.log(`[${id}] Associating ${id} to ${resolvedFilename}`); | ||
this.log(`[${id}] Initiating download item handlers`); | ||
const downloadData = { | ||
@@ -260,17 +241,2 @@ id, | ||
this.idToDownloadItems[id] = item; | ||
if (callbacks.onDownloadStarted) { | ||
this.log(`[${id}] Calling onDownloadStarted`); | ||
try { | ||
await callbacks.onDownloadStarted({ | ||
...downloadData, | ||
item, | ||
event, | ||
webContents, | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadStarted: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents }); | ||
} | ||
} | ||
const handlerConfig = { | ||
@@ -291,5 +257,50 @@ id, | ||
}); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
if (saveDialogOptions) { | ||
// Because the download happens concurrently as the user is choosing a save location | ||
// we need to wait for the save location to be chosen before we can start to fire out events | ||
// there's no good way to listen for this, so we need to poll | ||
const interval = setInterval(async () => { | ||
if (item.getSavePath()) { | ||
this.log(`User selected save path to ${item.getSavePath()}`); | ||
this.log(`[${id}] Initiating download item handlers`); | ||
clearInterval(interval); | ||
await start(); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
} | ||
else if (item.getState() === 'cancelled') { | ||
clearInterval(interval); | ||
this.log(`[${id}] Download was cancelled by user`); | ||
if (callbacks.onDownloadCancelled) { | ||
this.log(`[${id}] Calling onDownloadCancelled`); | ||
try { | ||
await callbacks.onDownloadCancelled({ | ||
id, | ||
item, | ||
event, | ||
webContents, | ||
cancelledFromSaveAsDialog: true, | ||
percentCompleted: 0, | ||
resolvedFilename: '', | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadCancelled: ${e}`); | ||
this.handleError(callbacks, e, { item, event, webContents }); | ||
} | ||
} | ||
} | ||
else { | ||
this.log(`[${id}] Waiting for save path to be chosen by user`); | ||
} | ||
}, 500); | ||
} | ||
else { | ||
this.log(`[${id}] Initiating download item handlers`); | ||
await start(); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
} | ||
}; | ||
@@ -296,0 +307,0 @@ } |
import crypto from 'crypto'; | ||
import { app, dialog } from 'electron'; | ||
import { app } from 'electron'; | ||
import extName from 'ext-name'; | ||
@@ -137,4 +137,4 @@ import * as path from 'path'; | ||
offline: false, | ||
upload: -1, | ||
download: -1, | ||
downloadThroughput: -1, | ||
uploadThroughput: -1, | ||
latency: 0, | ||
@@ -153,23 +153,19 @@ }); | ||
*/ | ||
onWillDownload(id, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
onWillDownload(id, { directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }) { | ||
return async (event, item, webContents) => { | ||
item.pause(); | ||
const cancel = async () => { | ||
item.cancel(); | ||
if (callbacks.onDownloadCancelled) { | ||
this.log(`[${id}] Calling onDownloadCancelled`); | ||
const start = async () => { | ||
if (callbacks.onDownloadStarted) { | ||
this.log(`[${id}] Calling onDownloadStarted`); | ||
try { | ||
await callbacks.onDownloadCancelled({ | ||
id, | ||
await callbacks.onDownloadStarted({ | ||
...downloadData, | ||
item, | ||
event, | ||
webContents, | ||
percentCompleted: 0, | ||
resolvedFilename: item.getFilename(), | ||
cancelledFromSaveAsDialog: true, | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadCancelled: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents, cancelledFromSaveAsDialog: true }); | ||
this.log(`[${id}] Error during onDownloadStarted: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents }); | ||
} | ||
@@ -195,20 +191,6 @@ } | ||
this.log(`[${id}] Prompting save as dialog`); | ||
let result; | ||
try { | ||
result = await dialog.showSaveDialog(window, { defaultPath: filePath, ...saveDialogOptions }); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error while showing save dialog: ${e}`); | ||
this.handleError(callbacks, e, { item, event, webContents }); | ||
await cancel(); | ||
return; | ||
} | ||
if (result.canceled) { | ||
this.log(`[${id}] User cancelled save as dialog`); | ||
await cancel(); | ||
return; | ||
} | ||
else { | ||
item.setSavePath(result.filePath); | ||
} | ||
// This actually isn't what shows the save dialog | ||
// If item.setSavePath() isn't called at all after some tiny period of time, | ||
// then the save dialog will show up, and it will use the options we set it to here | ||
item.setSaveDialogOptions({ ...saveDialogOptions, defaultPath: filePath }); | ||
} | ||
@@ -222,3 +204,2 @@ else { | ||
this.log(`[${id}] Associating ${id} to ${resolvedFilename}`); | ||
this.log(`[${id}] Initiating download item handlers`); | ||
const downloadData = { | ||
@@ -231,17 +212,2 @@ id, | ||
this.idToDownloadItems[id] = item; | ||
if (callbacks.onDownloadStarted) { | ||
this.log(`[${id}] Calling onDownloadStarted`); | ||
try { | ||
await callbacks.onDownloadStarted({ | ||
...downloadData, | ||
item, | ||
event, | ||
webContents, | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadStarted: ${e}`); | ||
this.handleError(callbacks, e, { id, item, event, webContents }); | ||
} | ||
} | ||
const handlerConfig = { | ||
@@ -262,5 +228,50 @@ id, | ||
}); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
if (saveDialogOptions) { | ||
// Because the download happens concurrently as the user is choosing a save location | ||
// we need to wait for the save location to be chosen before we can start to fire out events | ||
// there's no good way to listen for this, so we need to poll | ||
const interval = setInterval(async () => { | ||
if (item.getSavePath()) { | ||
this.log(`User selected save path to ${item.getSavePath()}`); | ||
this.log(`[${id}] Initiating download item handlers`); | ||
clearInterval(interval); | ||
await start(); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
} | ||
else if (item.getState() === 'cancelled') { | ||
clearInterval(interval); | ||
this.log(`[${id}] Download was cancelled by user`); | ||
if (callbacks.onDownloadCancelled) { | ||
this.log(`[${id}] Calling onDownloadCancelled`); | ||
try { | ||
await callbacks.onDownloadCancelled({ | ||
id, | ||
item, | ||
event, | ||
webContents, | ||
cancelledFromSaveAsDialog: true, | ||
percentCompleted: 0, | ||
resolvedFilename: '', | ||
}); | ||
} | ||
catch (e) { | ||
this.log(`[${id}] Error during onDownloadCancelled: ${e}`); | ||
this.handleError(callbacks, e, { item, event, webContents }); | ||
} | ||
} | ||
} | ||
else { | ||
this.log(`[${id}] Waiting for save path to be chosen by user`); | ||
} | ||
}, 500); | ||
} | ||
else { | ||
this.log(`[${id}] Initiating download item handlers`); | ||
await start(); | ||
item.on('updated', updatedHandler); | ||
item.once('done', doneHandler); | ||
item.resume(); | ||
} | ||
}; | ||
@@ -267,0 +278,0 @@ } |
@@ -78,3 +78,3 @@ import type { BrowserWindow, DownloadItem, Event, WebContents } from 'electron'; | ||
*/ | ||
protected onWillDownload(id: string, { window, directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }: DownloadParams): (event: Event, item: DownloadItem, webContents: WebContents) => Promise<void>; | ||
protected onWillDownload(id: string, { directory, overwrite, saveAsFilename, callbacks, saveDialogOptions, showBadge }: DownloadParams): (event: Event, item: DownloadItem, webContents: WebContents) => Promise<void>; | ||
protected itemOnUpdated({ id, event, item, webContents, callbacks, showBadge }: ItemHandlerParams): (_event: Event, state: 'progressing' | 'interrupted') => Promise<void>; | ||
@@ -81,0 +81,0 @@ protected itemOnDone({ id, event, item, webContents, callbacks, showBadge }: ItemHandlerParams): (_event: Event, state: 'completed' | 'cancelled' | 'interrupted') => Promise<void>; |
{ | ||
"name": "electron-dl-manager", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A library for implementing file downloads in Electron with 'save as' dialog and id support.", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
214394
1135