Skip to content

Commit

Permalink
Switch from downloadjs to multi-download
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelange committed Aug 2, 2024
1 parent 0e6f609 commit 4286e8c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
6 changes: 0 additions & 6 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"classnames": "^2.3.2",
"copy-to-clipboard": "^3.3.3",
"date-fns": "^2.29.3",
"downloadjs": "^1.4.7",
"nanoid": "^3.3.4",
"preact": "^10.3.2",
"preact-feather": "^4.2.1",
Expand Down
44 changes: 37 additions & 7 deletions client/src/routes/App/FileTransfer/FileTransfer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { h, createRef } from 'preact';
import download from 'downloadjs';
import { route } from 'preact-router';
import { PureComponent, forwardRef, memo } from 'preact/compat';
import { ArrowLeft, CheckCircle, Home, Plus, Image, Film, Box, Music, File, Zap, Share2, Send } from 'preact-feather';
Expand All @@ -22,6 +21,37 @@ import roomsDispatch from '../../../reducers/rooms';

import './FileTransfer.scss';


// adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js
// to take File https://developer.mozilla.org/en-US/docs/Web/API/File
const delay = milliseconds => new Promise(resolve => {
setTimeout(resolve, milliseconds);
});

const download = async (file) => {
const a = document.createElement('a');
a.download = file.name;
a.href = URL.createObjectURL(file);
a.style.display = 'none';
document.body.append(a);
a.click();

// Chrome requires the timeout
await delay(100);
a.remove();
};

const multiDownload = async (files) => {
if (!files) {
throw new Error('`files` required');
}

for (const [index, file] of files.entries()) {
await delay(index * 1000); // eslint-disable-line no-await-in-loop
download(file);
}
}

const CanvasUnwrapped = (props, ref) => {
return <canvas ref={ref} {...props} />;
};
Expand Down Expand Up @@ -292,17 +322,17 @@ class FileTransfer extends PureComponent {
isSelectorEnabled: false,
});
},
onDone: (file, meta) => {
onDone: (files) => {
if (file !== undefined) {
if (Array.isArray(file)) {
file.forEach(file => {
file.getBlob((err, blob) => download(blob, file.name));
});
multiDownload(
// make regular File from webtorrent File https://github.com/webtorrent/webtorrent/blob/v2.4.14/lib/file.js#L7
files.map(file => new File([file.getBlob()], file.name, {type: file.type}))
);
}
else {
download(file, meta.name, meta.type);
download(file);
}
}
this.resetState();
},
});
Expand Down
4 changes: 2 additions & 2 deletions client/src/utils/fileShare.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class FileShare {
this.socket.listen(constants.FILE_INIT, (data) => {
if (data.end) {
if (fileParts.length) {
onDone(new Blob(fileParts), metaData.meta[0]);
onDone(new File(fileParts, metaData.meta[0].name, {type: metaData.meta[0].type}));
fileParts = [];
size = 0;
statProg = 0.25;
Expand Down Expand Up @@ -227,4 +227,4 @@ class FileShare {

}

export default FileShare;
export default FileShare;

0 comments on commit 4286e8c

Please sign in to comment.