Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #41 - Add cancelDownload function to DownloadManager #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ if the download was successful the callback's error will be `null`, otherwise it
`url` returns the url of the downloaded file<br>
`filePath` location of where the file was saved


### DownloadManager.cancelDownload(url)

Cancel the downloaing task according to the url.

### url

Type: `string`<br>

The resource url passed to download() function previously.

Return true if the specific url download item is found and cancelled ok. Otherwise, return false.


### DownloadManager.bulkDownload(options, callback(error, finished, failed))

### options
Expand Down
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let downloadFolder = app.getPath('downloads');
let lastWindowCreated;

const queue = [];
const downloadingItems = {};

const _popQueueItem = (url) => {
let queueItem = queue.find(item => item.url === url);
Expand Down Expand Up @@ -59,6 +60,9 @@ function _registerListener(win, opts = {}) {

item.setSavePath(filePath);

// keep tracking to download task
downloadingItems[itemUrl] = {queueItem, downloadItem: item};

// Resuming an interrupted download
if (item.getState() === 'interrupted') {
item.resume();
Expand Down Expand Up @@ -91,6 +95,8 @@ function _registerListener(win, opts = {}) {
});

item.on('done', (e, state) => {
// release tracking
delete downloadingItems[itemUrl];

let finishedDownloadCallback = queueItem.callback || function () {};

Expand Down Expand Up @@ -160,6 +166,10 @@ const download = (options, callback) => {

if (typeof options.onLogin === 'function') {
request.on('login', options.onLogin)
} else if (options.username && options.password) {
request.on('login', (authInfo, callback) => {
callback(options.username, options.password)
Copy link
Owner

@danielnieto danielnieto Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these changes are relevant to 'abort download' functionality, correct me if I'm wrong

})
}

request.on('error', function (error) {
Expand Down Expand Up @@ -223,6 +233,24 @@ const download = (options, callback) => {
request.end();
};

const cancelDownload = (url) => {
// remove from queue first
const queueItem = _popQueueItem(url);
if (queueItem) {
return true;
}

// remove from downloading tasks
const item = downloadingItems[url];
if (item) {
item.downloadItem.cancel();
delete downloadingItems[url];
return true;
}

return false;
}

const bulkDownload = (options, callback) => {

options = Object.assign({}, { urls: [], path: '' }, options);
Expand Down Expand Up @@ -261,5 +289,6 @@ const bulkDownload = (options, callback) => {
module.exports = {
register,
download,
cancelDownload,
bulkDownload
};