diff --git a/index.d.ts b/index.d.ts index 26977c4..2cc70e9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,6 +6,14 @@ declare namespace electronDl { transferredBytes: number; totalBytes: number; } + + interface File { + filename: string, + path: string, + fileSize: number, + mimeType: string, + url: string + } interface Options { /** @@ -60,11 +68,24 @@ declare namespace electronDl { Optional callback that receives an object containing information about the progress of the current download item. */ readonly onProgress?: (progress: Progress) => void; + + /** + Optional callback that receives an object containing information about the combined progress of all download items done within any registered window. + + Each time a new download is started, the next callback will include it. The progress percentage could therefore become smaller again. + This callback provides the same data that is used for the progress bar on the app icon. + */ + readonly onTotalProgress?: (file: File) => void; /** Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item) for which the download has been cancelled. */ readonly onCancel?: (item: DownloadItem) => void; + + /** + Optional callback that receives an object with information about an item that has been completed. It is called for each completed item. + */ + readonly onCompleted?: (completed: Completed) => void; /** Reveal the downloaded file in the system file manager, and if possible, select the file. diff --git a/index.js b/index.js index 5129887..f8d3b24 100644 --- a/index.js +++ b/index.js @@ -113,6 +113,14 @@ function registerListener(session, options, callback = () => {}) { totalBytes: itemTotalBytes }); } + + if (typeof options.onTotalProgress === 'function') { + options.onTotalProgress({ + percent: progressDownloadItems(), + transferredBytes: receivedBytes, + totalBytes + }); + } }); item.on('done', (event, state) => { @@ -150,6 +158,16 @@ function registerListener(session, options, callback = () => {}) { shell.showItemInFolder(filePath); } + if (typeof options.onCompleted === 'function') { + options.onCompleted({ + fileName: item.getFilename(), + path: item.getSavePath(), + fileSize: item.getReceivedBytes(), + mimeType: item.getMimeType(), + url: item.getURL() + }); + } + callback(null, item); } }); diff --git a/readme.md b/readme.md index 3131f4e..f0ffe0a 100644 --- a/readme.md +++ b/readme.md @@ -142,12 +142,44 @@ Optional callback that receives an object containing information about the progr } ``` +#### onTotalProgress + +Type: `Function` + +Optional callback that receives an object containing information about the combined progress of all download items done within any registered window. + +Each time a new download is started, the next callback will include it. The progress percentage could therefore become smaller again. +This callback provides the same data that is used for the progress bar on the app icon. + +```js +{ + percent: 0.1, + transferredBytes: 100, + totalBytes: 1000 +} +``` + #### onCancel Type: `Function` Optional callback that receives the [download item](https://electronjs.org/docs/api/download-item) for which the download has been cancelled. +#### onCompleted + +Type: `Function` + +Optional callback that receives an object with information about an item that has been completed. It is called for each completed item. + +```js +{ + filename: 'file.zip', + path: '/path/file.zip', + fileSize: 503320, + mimeType: 'application/zip', + url: 'https://example.com/file.zip' +} + #### openFolderWhenDone Type: `boolean`\