electron-dl-manager
Version:
A library for implementing file downloads in Electron with 'save as' dialog and id support.
126 lines • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElectronDownloadManager = void 0;
const DownloadInitiator_1 = require("./DownloadInitiator");
const utils_1 = require("./utils");
/**
* This is used to solve an issue where multiple downloads are started at the same time.
* For example, Promise.all([download1, download2, ...]) will start both downloads at the same
* time. This is problematic because the will-download event is not guaranteed to fire in the
* order that the downloads were started.
*
* So we use this to make sure that will-download fires in the order that the downloads were
* started by executing the downloads in a sequential fashion.
*
* For more information see:
* https://github.com/theogravity/electron-dl-manager/issues/11
*/
class DownloadQueue {
promise = Promise.resolve();
add(task) {
this.promise = this.promise.then(() => task());
return this.promise;
}
}
/**
* Enables handling downloads in Electron.
*/
class ElectronDownloadManager {
downloadData;
logger;
downloadQueue = new DownloadQueue();
constructor(params = {}) {
this.downloadData = {};
this.logger = params.debugLogger || (() => { });
}
log(message) {
this.logger(message);
}
/**
* Returns the current download data
*/
getDownloadData(id) {
return this.downloadData[id];
}
/**
* Cancels a download
*/
cancelDownload(id) {
const data = this.downloadData[id];
if (data?.item) {
this.log(`[${id}] Cancelling download`);
data.item.cancel();
}
else {
this.log(`[${id}] Download ${id} not found for cancellation`);
}
}
/**
* Pauses a download
*/
pauseDownload(id) {
const data = this.downloadData[id];
if (data?.item) {
this.log(`[${id}] Pausing download`);
data.item.pause();
}
else {
this.log(`[${id}] Download ${id} not found for pausing`);
}
}
/**
* Resumes a download
*/
resumeDownload(id) {
const data = this.downloadData[id];
if (data?.item?.isPaused()) {
this.log(`[${id}] Resuming download`);
data.item.resume();
}
else {
this.log(`[${id}] Download ${id} not found or is not in a paused state`);
}
}
/**
* Returns the number of active downloads
*/
getActiveDownloadCount() {
return Object.values(this.downloadData).filter((data) => data.isDownloadInProgress()).length;
}
/**
* Starts a download. If saveDialogOptions has been defined in the config,
* the saveAs dialog will show up first.
*
* Returns the id of the download.
*/
async download(params) {
return this.downloadQueue.add(() => new Promise((resolve, reject) => {
try {
if (params.saveAsFilename && params.saveDialogOptions) {
return reject(Error("You cannot define both saveAsFilename and saveDialogOptions to start a download"));
}
const downloadInitiator = new DownloadInitiator_1.DownloadInitiator({
debugLogger: this.logger,
onCleanup: (data) => {
this.cleanup(data);
},
onDownloadInit: (data) => {
this.downloadData[data.id] = data;
resolve(data.id);
},
});
this.log(`[${downloadInitiator.getDownloadId()}] Registering download for url: ${(0, utils_1.truncateUrl)(params.url)}`);
params.window.webContents.session.once("will-download", downloadInitiator.generateOnWillDownload(params));
params.window.webContents.downloadURL(params.url, params.downloadURLOptions);
}
catch (e) {
reject(e);
}
}));
}
cleanup(data) {
delete this.downloadData[data.id];
}
}
exports.ElectronDownloadManager = ElectronDownloadManager;
//# sourceMappingURL=ElectronDownloadManager.js.map