Skip to content

Commit

Permalink
Require Electron 7
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 7, 2020
1 parent dde9016 commit 0248ff9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
os: osx
language: node_js
node_js:
- '10'
- '12'
59 changes: 31 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@ const unusedFilename = require('unused-filename');
const pupa = require('pupa');
const extName = require('ext-name');

function getFilenameFromMime(name, mime) {
const exts = extName.mime(mime);
const getFilenameFromMime = (name, mime) => {
const extensions = extName.mime(mime);

if (exts.length !== 1) {
if (extensions.length !== 1) {
return name;
}

return `${name}.${exts[0].ext}`;
}
return `${name}.${extensions[0].ext}`;
};

function registerListener(session, options, cb = () => {}) {
function registerListener(session, options, callback = () => {}) {
const downloadItems = new Set();
let receivedBytes = 0;
let completedBytes = 0;
let totalBytes = 0;
const activeDownloadItems = () => downloadItems.size;
const progressDownloadItems = () => receivedBytes / totalBytes;

options = Object.assign({
showBadge: true
}, options);
options = {
showBadge: true,
...options
};

const listener = (e, item, webContents) => {
const listener = (event, item, webContents) => {
downloadItems.add(item);
totalBytes += item.getTotalBytes();

Expand All @@ -36,17 +37,16 @@ function registerListener(session, options, cb = () => {}) {
({hostWebContents} = webContents);
}

const win = BrowserWindow.fromWebContents(hostWebContents);
const window_ = BrowserWindow.fromWebContents(hostWebContents);

const dir = options.directory || app.getPath('downloads');
const directory = options.directory || app.getPath('downloads');
let filePath;
if (options.filename) {
filePath = path.join(dir, options.filename);
filePath = path.join(directory, options.filename);
} else {
const filename = item.getFilename();
const name = path.extname(filename) ? filename : getFilenameFromMime(filename, item.getMimeType());

filePath = unusedFilename.sync(path.join(dir, name));
filePath = unusedFilename.sync(path.join(directory, name));
}

const errorMessage = options.errorMessage || 'The download of {filename} was interrupted';
Expand All @@ -70,8 +70,8 @@ function registerListener(session, options, cb = () => {}) {
app.badgeCount = activeDownloadItems();
}

if (!win.isDestroyed()) {
win.setProgressBar(progressDownloadItems());
if (!window_.isDestroyed()) {
window_.setProgressBar(progressDownloadItems());
}

if (typeof options.onProgress === 'function') {
Expand All @@ -94,8 +94,8 @@ function registerListener(session, options, cb = () => {}) {
app.badgeCount = activeDownloadItems();
}

if (!win.isDestroyed() && !activeDownloadItems()) {
win.setProgressBar(-1);
if (!window_.isDestroyed() && !activeDownloadItems()) {
window_.setProgressBar(-1);
receivedBytes = 0;
completedBytes = 0;
totalBytes = 0;
Expand All @@ -112,17 +112,17 @@ function registerListener(session, options, cb = () => {}) {
} else if (state === 'interrupted') {
const message = pupa(errorMessage, {filename: item.getFilename()});
dialog.showErrorBox(errorTitle, message);
cb(new Error(message));
callback(new Error(message));
} else if (state === 'completed') {
if (process.platform === 'darwin') {
app.dock.downloadFinished(filePath);
}

if (options.openFolderWhenDone) {
shell.showItemInFolder(path.join(dir, item.getFilename()));
shell.showItemInFolder(path.join(directory, item.getFilename()));
}

cb(null, item);
callback(null, item);
}
});
};
Expand All @@ -136,16 +136,19 @@ module.exports = (options = {}) => {
});
};

module.exports.download = (win, url, options) => new Promise((resolve, reject) => {
options = Object.assign({}, options, {unregisterWhenDone: true});
module.exports.download = (window_, url, options) => new Promise((resolve, reject) => {
options = {
...options,
unregisterWhenDone: true
};

registerListener(win.webContents.session, options, (err, item) => {
if (err) {
reject(err);
registerListener(window_.webContents.session, options, (error, item) => {
if (error) {
reject(error);
} else {
resolve(item);
}
});

win.webContents.downloadURL(url);
window_.webContents.downloadURL(url);
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"description": "Simplified file downloads for your Electron app",
"license": "MIT",
"repository": "sindresorhus/electron-dl",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"scripts": {
"//": "Enable tsd again when the Electron issue is fixed",
"start": "electron run.js",
"test": "xo && ava && tsd"
"test": "xo && ava"
},
"files": [
"index.js",
Expand All @@ -31,10 +33,10 @@
"unused-filename": "^2.1.0"
},
"devDependencies": {
"@types/node": "^12.12.8",
"@types/node": "^13.1.4",
"ava": "^2.4.0",
"cp-file": "^7.0.0",
"electron": "^7.1.2",
"electron": "^7.1.7",
"minimist": "^1.2.0",
"node-static": "^0.7.11",
"pify": "^4.0.1",
Expand Down
5 changes: 1 addition & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

> Simplified file downloads for your [Electron](https://electronjs.org) app

## Why?

- One function call instead of having to manually implement a lot of [boilerplate](index.js).
Expand All @@ -13,13 +12,13 @@

<img src="screenshot.png" width="82">


## Install

```
$ npm install electron-dl
```

Requires Electron 7 or later.

## Usage

Expand Down Expand Up @@ -158,7 +157,6 @@ Default: `true`

Shows the file count badge on macOS/Linux dock icons when download is in progress.


## Development

After making changes, run the automated tests:
Expand All @@ -173,7 +171,6 @@ And before submitting a pull request, run the manual tests to manually verify th
npm start
```


## Related

- [electron-debug](https://github.com/sindresorhus/electron-debug) - Adds useful debug features to your Electron app
Expand Down

0 comments on commit 0248ff9

Please sign in to comment.