Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from downloadjs to multi-download #174

Merged
merged 15 commits into from
Oct 28, 2024

Conversation

ddelange
Copy link

@ddelange ddelange commented Aug 2, 2024

Fix #163

blenderskool and others added 7 commits October 21, 2022 23:32
- docker-compose version 1.29.2, build unknown
- Linux 5.15.0-1029-oracle blenderskool#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
```
Universal Analytics has been shut down
Comment on lines 30 to 41
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();
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use downloadjs for this? From what I can see, delay of 1s is something that is to be added for fixing the issue. The individual file download seems to use same process as downloadjs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in addition to the 1s delay, to me it looks like this additional 100ms delay before removing the a again is not present in downloadjs. it looks like they immediately remove it after clicking: https://github.com/rndme/download/blob/09d6492f47ef18feca39c3d748960dce44f93a89/download.js#L116-L117

maybe chrome introduced the need for this additional short delay somewhere in the last 8 years (the latest release of downloadjs)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is a nice elimination of some legacy/unmaintained code, in exchange for a download function that's only 10 lines without dependencies

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see the timeout. Changes seems fine to me, I'll also test it on my end in couple of browsers to ensure that it doesn't break existing flows.

Copy link
Author

@ddelange ddelange Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! we might be able to reduce the 1000ms, I blamed it to a commit related to cross-origin URLs sindresorhus/multi-download@bf7580d#r144977818

client/src/routes/App/FileTransfer/FileTransfer.js Outdated Show resolved Hide resolved
});
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}))
Copy link
Author

@ddelange ddelange Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, it looks like File.getBlob was removed in webtorrent v2.

this seems related to them switching to local storage in v2 as opposed to keeping complete torrents in memory in v1 ref webtorrent/webtorrent#86 (comment)

edit: they now have File.blob to load the whole torrent into memory and (more interestingly) File.streamURL (requires client.createServer to be ran beforehand).

Maybe blaze can even get rid of download.js altogether using the local storage functionality of webtorrent v2 🤔

they use FileSystemDirectoryHandle ref webtorrent/webtorrent#2208 this PR was superceded again

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blaze currently uses Webtorrent v1, and we'll need to upgrade to v2. From what I can recall since the last time I had checked, there were a bunch of API changes associated with v2 that we'd also have to make in Blaze which I wasn't fond of picking up then. Would you be interested in picking this up?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'm also afraid it's a can of worms... the local storage feature might make it worthwhile though. I guess a 10GB file would now fill up 10GB of my swap right?

Copy link
Author

@ddelange ddelange Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

judging by the File API docs, I think using streamURL you can initiate a download before the torrent has finished downloading, and so download.js could theoretically be called upon as soon as the torrent is initiated.

most importantly, that way the file doesn't need to go fully into memory in a Blob in order to send to downloads.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file.getBlob and file.blob are functionally identical, it was simply renamed to match the W3C file API, rather than create our own structure, but getBlob also loaded the entire file into memory

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah just saw your cross comment, so file.streamURL is a viable option to get around the 'whole file in memory' pattern in the current downloading setup, with minimal overhead from client.createServer. that's great to hear and probably the way to move forward there!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the local storage feature might make it worthwhile though

@ddelange isn't the local storage limited to like 10MBs? I wonder how it would manage files of much higher sizes in such a limited space

Copy link
Author

@ddelange ddelange Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in v2 the torrents directly download to /tmp/webtorrent by default using file system access.

path can be set on a per-torrent basis by the receiver before the torrent is added (folder selection is google chrome only if I understood the comments correctly)

Copy link

@ThaUnknown ThaUnknown Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in v2 the torrents directly download to /tmp/webtorrent by default using file system access.

for the sake of simplicity lets say sure, but if you want specifics, it actually writes to individual chunks via FSA's origin private file system, which functionally is no different from the likes of localStorage or IDB, except its a LOT faster, and allows for MUCH, MUCH, MUCH bigger file sizes, in extreme cases up to terabytes

on chrome that's a bit different, because IF YOU CHOOSE TO specify a rootDir, it can emulate the file structure in a specified folder as already mentioned, on top of saving chunks as cache to OPFS

that's the big difference for webtorrent v2, the storage and interfaces it provides:

  • createServer now provides insanely powerful, 0 overhead methods for rendering media like video, text, images using browser's own tools, instead of implementing custom tooling for it, which also allows other libraries to consume what webtorrent creates as its exposed via plain URL
  • storage isn't now memory, so we're no longer at the poor sub 2GB file limit, the most I could get webtorrent to store now is ~2TB after which chrome stopped enjoying handling so much data and started erroring

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for pointing to rootDir!

api.md points to storeOpts.rootDir but I think that should be opts.rootDir right?

based on the comments in api.md, I'm now not sure what's the difference between opts.path and opts.rootDir. should I ignore opts.path and only work with opts.rootDir?

@ddelange
Copy link
Author

ddelange commented Aug 6, 2024

webtorrent v2 aside, @blenderskool did you get a chance to test more than 10 files?

@blenderskool
Copy link
Owner

@ddelange Not yet, will check it tomorrow. By the way, are you done with the changes for this PR?

@ddelange
Copy link
Author

ddelange commented Aug 6, 2024

I'd say ready for review again yeah :)

@ddelange
Copy link
Author

hi @blenderskool, did you get a chance to test more than 10 files with this fix?

@ddelange
Copy link
Author

hi @blenderskool 👋 friendly reminder:)

Copy link
Owner

@blenderskool blenderskool left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddelange Finally got some time to test the changes. I don't know if it's just me but I see following errors with just a single file! Have you tested your changes?

image

@blenderskool
Copy link
Owner

@ddelange I've pushed some fixes to your code and I can now see the multi-file(and single-file) download to work. Do test it from your end too if it is working as expected.

@blenderskool blenderskool changed the base branch from master to next October 15, 2024 12:56
@ddelange
Copy link
Author

awesome, your changes look clean!

@ddelange
Copy link
Author

ready for merge from my side 👍

@blenderskool blenderskool merged commit 2d34184 into blenderskool:next Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Transferring more than 10 files with Google Chrome
4 participants