-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
index.js
197 lines (161 loc) · 5.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import process from 'node:process';
import path from 'node:path';
import {
app,
BrowserWindow,
shell,
dialog,
} from 'electron';
import {unusedFilenameSync} from 'unused-filename';
import pupa from 'pupa';
import extName from 'ext-name';
export class CancelError extends Error {}
const getFilenameFromMime = (name, mime) => {
const extensions = extName.mime(mime);
if (extensions.length !== 1) {
return name;
}
return `${name}.${extensions[0].ext}`;
};
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 = {
showBadge: true,
showProgressBar: true,
...options,
};
const listener = (event, item, webContents) => {
downloadItems.add(item);
totalBytes += item.getTotalBytes();
const window_ = BrowserWindow.fromWebContents(webContents);
if (!window_) {
throw new Error('Failed to get window from web contents.');
}
if (options.directory && !path.isAbsolute(options.directory)) {
throw new Error('The `directory` option must be an absolute path');
}
const directory = options.directory ?? app.getPath('downloads');
let filePath;
if (options.filename) {
filePath = path.join(directory, options.filename);
} else {
const filename = item.getFilename();
const name = path.extname(filename) ? filename : getFilenameFromMime(filename, item.getMimeType());
filePath = options.overwrite ? path.join(directory, name) : unusedFilenameSync(path.join(directory, name));
}
const errorMessage = options.errorMessage ?? 'The download of {filename} was interrupted';
if (options.saveAs) {
item.setSaveDialogOptions({defaultPath: filePath, ...options.dialogOptions});
} else {
item.setSavePath(filePath);
}
item.on('updated', () => {
receivedBytes = completedBytes;
for (const item of downloadItems) {
receivedBytes += item.getReceivedBytes();
}
if (options.showBadge && ['darwin', 'linux'].includes(process.platform)) {
app.badgeCount = activeDownloadItems();
}
if (!window_.isDestroyed() && options.showProgressBar) {
window_.setProgressBar(progressDownloadItems());
}
if (typeof options.onProgress === 'function') {
const itemTransferredBytes = item.getReceivedBytes();
const itemTotalBytes = item.getTotalBytes();
options.onProgress({
percent: itemTotalBytes ? itemTransferredBytes / itemTotalBytes : 0,
transferredBytes: itemTransferredBytes,
totalBytes: itemTotalBytes,
});
}
if (typeof options.onTotalProgress === 'function') {
options.onTotalProgress({
percent: progressDownloadItems(),
transferredBytes: receivedBytes,
totalBytes,
});
}
});
item.on('done', (event, state) => {
completedBytes += item.getTotalBytes();
downloadItems.delete(item);
if (options.showBadge && ['darwin', 'linux'].includes(process.platform)) {
app.badgeCount = activeDownloadItems();
}
if (!window_.isDestroyed() && !activeDownloadItems()) {
window_.setProgressBar(-1);
receivedBytes = 0;
completedBytes = 0;
totalBytes = 0;
}
if (options.unregisterWhenDone) {
session.removeListener('will-download', listener);
}
// eslint-disable-next-line unicorn/prefer-switch
if (state === 'cancelled') {
if (typeof options.onCancel === 'function') {
options.onCancel(item);
}
callback(new CancelError());
} else if (state === 'interrupted') {
const message = pupa(errorMessage, {filename: path.basename(filePath)});
callback(new Error(message));
} else if (state === 'completed') {
const savePath = item.getSavePath();
if (process.platform === 'darwin') {
app.dock.downloadFinished(savePath);
}
if (options.openFolderWhenDone) {
shell.showItemInFolder(savePath);
}
if (typeof options.onCompleted === 'function') {
options.onCompleted({
fileName: item.getFilename(), // Just for backwards compatibility. TODO: Remove in the next major version.
filename: item.getFilename(),
path: savePath,
fileSize: item.getReceivedBytes(),
mimeType: item.getMimeType(),
url: item.getURL(),
});
}
callback(null, item);
}
});
if (typeof options.onStarted === 'function') {
options.onStarted(item);
}
};
session.on('will-download', listener);
}
export default function electronDl(options = {}) {
app.on('session-created', session => {
registerListener(session, options, (error, _) => {
if (error && !(error instanceof CancelError)) {
const errorTitle = options.errorTitle ?? 'Download Error';
dialog.showErrorBox(errorTitle, error.message);
}
});
});
}
export async function download(window_, url, options) {
return new Promise((resolve, reject) => {
options = {
...options,
unregisterWhenDone: true,
};
registerListener(window_.webContents.session, options, (error, item) => {
if (error) {
reject(error);
} else {
resolve(item);
}
});
window_.webContents.downloadURL(url);
});
}