thedesk/app/node_modules/electron-dl/index.js

139 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
'use strict';
const path = require('path');
const electron = require('electron');
const unusedFilename = require('unused-filename');
const pupa = require('pupa');
const extName = require('ext-name');
const app = electron.app;
const shell = electron.shell;
function getFilenameFromMime(name, mime) {
const exts = extName.mime(mime);
if (exts.length !== 1) {
return name;
}
return `${name}.${exts[0].ext}`;
}
2018-05-02 14:14:03 +10:00
function registerListener(session, options, cb = () => {}) {
2018-01-28 23:22:43 +11:00
const downloadItems = new Set();
let receivedBytes = 0;
let completedBytes = 0;
let totalBytes = 0;
const activeDownloadItems = () => downloadItems.size;
const progressDownloadItems = () => receivedBytes / totalBytes;
2018-05-02 14:14:03 +10:00
options = Object.assign({
showBadge: true
}, options);
2018-01-28 23:22:43 +11:00
const listener = (e, item, webContents) => {
downloadItems.add(item);
totalBytes += item.getTotalBytes();
let hostWebContents = webContents;
if (webContents.getType() === 'webview') {
hostWebContents = webContents.hostWebContents;
}
const win = electron.BrowserWindow.fromWebContents(hostWebContents);
2018-05-02 14:14:03 +10:00
const dir = options.directory || app.getPath('downloads');
2018-01-28 23:22:43 +11:00
let filePath;
2018-05-02 14:14:03 +10:00
if (options.filename) {
filePath = path.join(dir, options.filename);
2018-01-28 23:22:43 +11:00
} else {
const filename = item.getFilename();
const name = path.extname(filename) ? filename : getFilenameFromMime(filename, item.getMimeType());
filePath = unusedFilename.sync(path.join(dir, name));
}
2018-05-02 14:14:03 +10:00
const errorMessage = options.errorMessage || 'The download of {filename} was interrupted';
const errorTitle = options.errorTitle || 'Download Error';
2018-01-28 23:22:43 +11:00
2018-05-02 14:14:03 +10:00
if (!options.saveAs) {
2018-01-28 23:22:43 +11:00
item.setSavePath(filePath);
}
item.on('updated', () => {
receivedBytes = [...downloadItems].reduce((receivedBytes, item) => {
receivedBytes += item.getReceivedBytes();
return receivedBytes;
}, completedBytes);
2018-05-02 14:14:03 +10:00
if (options.showBadge && ['darwin', 'linux'].includes(process.platform)) {
2018-01-28 23:22:43 +11:00
app.setBadgeCount(activeDownloadItems());
}
if (!win.isDestroyed()) {
win.setProgressBar(progressDownloadItems());
}
2018-05-02 14:14:03 +10:00
if (typeof options.onProgress === 'function') {
options.onProgress(progressDownloadItems());
2018-01-28 23:22:43 +11:00
}
});
item.on('done', (e, state) => {
completedBytes += item.getTotalBytes();
downloadItems.delete(item);
2018-05-02 14:14:03 +10:00
if (options.showBadge && ['darwin', 'linux'].includes(process.platform)) {
2018-01-28 23:22:43 +11:00
app.setBadgeCount(activeDownloadItems());
}
if (!win.isDestroyed() && !activeDownloadItems()) {
win.setProgressBar(-1);
receivedBytes = 0;
completedBytes = 0;
totalBytes = 0;
}
if (state === 'interrupted') {
const message = pupa(errorMessage, {filename: item.getFilename()});
electron.dialog.showErrorBox(errorTitle, message);
cb(new Error(message));
} else if (state === 'completed') {
if (process.platform === 'darwin') {
app.dock.downloadFinished(filePath);
}
2018-05-02 14:14:03 +10:00
if (options.openFolderWhenDone) {
shell.showItemInFolder(path.join(dir, item.getFilename()));
2018-01-28 23:22:43 +11:00
}
2018-05-02 14:14:03 +10:00
if (options.unregisterWhenDone) {
2018-01-28 23:22:43 +11:00
session.removeListener('will-download', listener);
}
cb(null, item);
}
});
};
session.on('will-download', listener);
}
2018-05-02 14:14:03 +10:00
module.exports = (options = {}) => {
2018-01-28 23:22:43 +11:00
app.on('session-created', session => {
2018-05-02 14:14:03 +10:00
registerListener(session, options);
2018-01-28 23:22:43 +11:00
});
};
2018-05-02 14:14:03 +10:00
module.exports.download = (win, url, options) => new Promise((resolve, reject) => {
options = Object.assign({}, options, {unregisterWhenDone: true});
2018-01-28 23:22:43 +11:00
2018-05-02 14:14:03 +10:00
registerListener(win.webContents.session, options, (err, item) => {
2018-01-28 23:22:43 +11:00
if (err) {
reject(err);
} else {
resolve(item);
}
});
win.webContents.downloadURL(url);
});