Skip to content

Commit 4286e8c

Browse files
committed
Switch from downloadjs to multi-download
1 parent 0e6f609 commit 4286e8c

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

client/package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"classnames": "^2.3.2",
2424
"copy-to-clipboard": "^3.3.3",
2525
"date-fns": "^2.29.3",
26-
"downloadjs": "^1.4.7",
2726
"nanoid": "^3.3.4",
2827
"preact": "^10.3.2",
2928
"preact-feather": "^4.2.1",

client/src/routes/App/FileTransfer/FileTransfer.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { h, createRef } from 'preact';
2-
import download from 'downloadjs';
32
import { route } from 'preact-router';
43
import { PureComponent, forwardRef, memo } from 'preact/compat';
54
import { ArrowLeft, CheckCircle, Home, Plus, Image, Film, Box, Music, File, Zap, Share2, Send } from 'preact-feather';
@@ -22,6 +21,37 @@ import roomsDispatch from '../../../reducers/rooms';
2221

2322
import './FileTransfer.scss';
2423

24+
25+
// adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js
26+
// to take File https://developer.mozilla.org/en-US/docs/Web/API/File
27+
const delay = milliseconds => new Promise(resolve => {
28+
setTimeout(resolve, milliseconds);
29+
});
30+
31+
const download = async (file) => {
32+
const a = document.createElement('a');
33+
a.download = file.name;
34+
a.href = URL.createObjectURL(file);
35+
a.style.display = 'none';
36+
document.body.append(a);
37+
a.click();
38+
39+
// Chrome requires the timeout
40+
await delay(100);
41+
a.remove();
42+
};
43+
44+
const multiDownload = async (files) => {
45+
if (!files) {
46+
throw new Error('`files` required');
47+
}
48+
49+
for (const [index, file] of files.entries()) {
50+
await delay(index * 1000); // eslint-disable-line no-await-in-loop
51+
download(file);
52+
}
53+
}
54+
2555
const CanvasUnwrapped = (props, ref) => {
2656
return <canvas ref={ref} {...props} />;
2757
};
@@ -292,17 +322,17 @@ class FileTransfer extends PureComponent {
292322
isSelectorEnabled: false,
293323
});
294324
},
295-
onDone: (file, meta) => {
325+
onDone: (files) => {
296326
if (file !== undefined) {
297327
if (Array.isArray(file)) {
298-
file.forEach(file => {
299-
file.getBlob((err, blob) => download(blob, file.name));
300-
});
328+
multiDownload(
329+
// make regular File from webtorrent File https://github.com/webtorrent/webtorrent/blob/v2.4.14/lib/file.js#L7
330+
files.map(file => new File([file.getBlob()], file.name, {type: file.type}))
331+
);
301332
}
302333
else {
303-
download(file, meta.name, meta.type);
334+
download(file);
304335
}
305-
}
306336
this.resetState();
307337
},
308338
});

client/src/utils/fileShare.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class FileShare {
5050
this.socket.listen(constants.FILE_INIT, (data) => {
5151
if (data.end) {
5252
if (fileParts.length) {
53-
onDone(new Blob(fileParts), metaData.meta[0]);
53+
onDone(new File(fileParts, metaData.meta[0].name, {type: metaData.meta[0].type}));
5454
fileParts = [];
5555
size = 0;
5656
statProg = 0.25;
@@ -227,4 +227,4 @@ class FileShare {
227227

228228
}
229229

230-
export default FileShare;
230+
export default FileShare;

0 commit comments

Comments
 (0)