Skip to content

Commit

Permalink
Switch from downloadjs to multi-download (#174)
Browse files Browse the repository at this point in the history
* Delete FUNDING.yml

* fix: docker-compose now 

- docker-compose version 1.29.2, build unknown
- Linux 5.15.0-1029-oracle #35-Ubuntu SMP Tue Jan 24 15:17:52 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

```
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.blaze-server.environment.TRUST_PROXY contains true, which is an invalid type, it should be a string, number, or a null
```

* Migrate to GA4

Universal Analytics has been shut down

* Switch from downloadjs to multi-download

* Move to utils/download.js

* Amend comment

* Add URL.revokeObjectURL

* Simplify delay behaviour, add JSDoc comments

* Typo

* Fix typo

* Fix multi-file downloads for WebTorrent

* Update client/src/utils/download.js

Co-authored-by: ddelange <[email protected]>

---------

Co-authored-by: Akash Hamirwasia <[email protected]>
Co-authored-by: 薛定谔的meow <[email protected]>
Co-authored-by: Akash Hamirwasia <[email protected]>
  • Loading branch information
4 people authored Oct 28, 2024
1 parent 46c96d9 commit 2d34184
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 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
17 changes: 5 additions & 12 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 @@ -17,6 +16,7 @@ import Visualizer from '../../../utils/visualizer';
import formatSize from '../../../utils/formatSize';
import pluralize from '../../../utils/pluralize';
import urls from '../../../utils/urls';
import { multiDownload } from '../../../utils/download';
import constants from '../../../../../common/constants';
import roomsDispatch from '../../../reducers/rooms';

Expand Down Expand Up @@ -292,17 +292,10 @@ class FileTransfer extends PureComponent {
isSelectorEnabled: false,
});
},
onDone: (file, meta) => {
if (file !== undefined) {
if (Array.isArray(file)) {
file.forEach(file => {
file.getBlob((err, blob) => download(blob, file.name));
});
}
else {
download(file, meta.name, meta.type);
}
}
onDone: (files) => {
if (files === undefined) return;

multiDownload(files);
this.resetState();
},
});
Expand Down
41 changes: 41 additions & 0 deletions client/src/utils/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Adapted from https://github.com/sindresorhus/multi-download/blob/v4.0.0/index.js
// to take File as input https://developer.mozilla.org/en-US/docs/Web/API/File

/**
* Creates a promise that resolves after the specified number of milliseconds
*/
const delay = milliseconds => new Promise(resolve => {
setTimeout(resolve, milliseconds);
});

/**
* Downloads a single file
* @param file - An instance of the File type representing the file to download
*/
const download = async (file) => {
const a = document.createElement('a');
const url = URL.createObjectURL(file);
a.download = file.name;
a.href = url;
a.style.display = 'none';
document.body.append(a);
a.click();
await delay(100); // for Chrome
a.remove();
URL.revokeObjectURL(url);
};

/**
* Initiates multiple file downloads with a constant delay between each one
* @param files - An array of instances of the File type representing the files to download
*/
export const multiDownload = async (files) => {
if (!files) {
throw new Error('`files` required');
};

for (const file of files) {
download(file);
await delay(1000);
}
};
22 changes: 18 additions & 4 deletions client/src/utils/fileShare.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ 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 @@ -106,8 +108,20 @@ class FileShare {

torrent.on('upload', update);
torrent.on('download', update);
torrent.on('done', () => {
onDone(torrent.files);
torrent.on('done', async () => {
const files = await Promise.all(
torrent.files.map(
(file) =>
new Promise((resolve, reject) => {
// make regular File from webtorrent File https://github.com/webtorrent/webtorrent/blob/v1.9.7/lib/file.js#L13
file.getBlob((err, blob) => {
if (err) reject(err);
resolve(new File([blob], file.name, { type: file.type }));
});
})
)
);
onDone(files);
});
}

Expand Down Expand Up @@ -227,4 +241,4 @@ class FileShare {

}

export default FileShare;
export default FileShare;

0 comments on commit 2d34184

Please sign in to comment.