From 8b9e588869253a6d9b755101025fa4c97a27696b Mon Sep 17 00:00:00 2001 From: ddelange <14880945+ddelange@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:42:01 +0200 Subject: [PATCH] Switch from downloadjs to multi-download --- client/package-lock.json | 6 --- client/package.json | 1 - .../routes/App/FileTransfer/FileTransfer.js | 42 +++++++++++++++---- client/src/utils/fileShare.js | 4 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index e161071..ed97935 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -13,7 +13,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", @@ -6373,11 +6372,6 @@ "node": ">=12" } }, - "node_modules/downloadjs": { - "version": "1.4.7", - "resolved": "https://registry.npmjs.org/downloadjs/-/downloadjs-1.4.7.tgz", - "integrity": "sha1-9p+W+UDg0FU9rCkROYZaPNAQHjw=" - }, "node_modules/duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", diff --git a/client/package.json b/client/package.json index e7c9b84..dffc8e2 100644 --- a/client/package.json +++ b/client/package.json @@ -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", diff --git a/client/src/routes/App/FileTransfer/FileTransfer.js b/client/src/routes/App/FileTransfer/FileTransfer.js index 727aee9..11679fc 100644 --- a/client/src/routes/App/FileTransfer/FileTransfer.js +++ b/client/src/routes/App/FileTransfer/FileTransfer.js @@ -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'; @@ -22,6 +21,35 @@ 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 ; }; @@ -292,17 +320,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(); }, }); diff --git a/client/src/utils/fileShare.js b/client/src/utils/fileShare.js index 056a197..d3a7e35 100644 --- a/client/src/utils/fileShare.js +++ b/client/src/utils/fileShare.js @@ -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; @@ -227,4 +227,4 @@ class FileShare { } -export default FileShare; \ No newline at end of file +export default FileShare;