-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch from downloadjs to multi-download (#174)
* 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
1 parent
46c96d9
commit 2d34184
Showing
5 changed files
with
64 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters