Gokapi icon indicating copy to clipboard operation
Gokapi copied to clipboard

Bug: Downloading E2E file onto android via Firefox yields partial file

Open zwimer opened this issue 6 months ago • 8 comments

I E2E uploaded a roughly 7GB file to gokapi. On my laptop I can download this as expected without issue. When I go to my android phone and try to download it (my browser is Firefox) it tends to download somewhere between 10-60 MB then tells me the download has completed, showing no error.

It is possible this error is not at all related to gokapi, but I've never seen a partial-download marked as completed without error on Firefox on Android before, so I'm leaning towards this being related to E2E gokapi decryption.

zwimer avatar Aug 11 '25 05:08 zwimer

If it matters, the file size is larger than my total system RAM.

zwimer avatar Aug 11 '25 14:08 zwimer

Thanks for the report! I was able to reproduce the problem and I assume that this issue is related to it: https://github.com/jimmywarting/StreamSaver.js/issues/340

It seems to be a problem with Firefox itself, and I could only find one solutions to it: To save it as a blob, but that would save the whole file in memory and is not feasible for mobile devices. Apparently services like Mega.nz store the file in 5MB chunks and merge it afterwards. But merging would create the same problem.

If anyone know a solution to this problem, please let me know!

Forceu avatar Aug 28 '25 14:08 Forceu

One possible fix, if I understand the problem correctly, is to have the E2E encryption be done in chunks small enough that each chunk can be loaded into the average mobile device's memory. That is, instead of storing an encrypted file as one large encrypted chunk of binary data, store it as a sequence of encrypted chunks. Thus the download function would be a loop that downloads a chunk, decrypts it, then writes the output to the download file.

zwimer avatar Aug 28 '25 19:08 zwimer

Until the underlying issue is fixed, it'd consider the current behavior a bug rather than an unimplemented feature, as currently the download not only yields a partial file, but for me at least, is reported as successfully completed. I think an error condition should be raised instead.

zwimer avatar Aug 28 '25 19:08 zwimer

One possible fix, if I understand the problem correctly, is to have the E2E encryption be done in chunks small enough that each chunk can be loaded into the average mobile device's memory.

Decryption is not the problem, it is downloading the file as a stream. It would be easy to store the file in chunks, but in the end the merged file needs to be downloaded in the browser. As far as I found out, this would only be possible as a blob with Firefox on Android, which would require the file to be completely in memory.

You are right that there should be a warning, I will look into it.

Forceu avatar Aug 28 '25 20:08 Forceu

So the problem is that the output file has to be written all at once? Perhaps something like this might alleviate the issue? https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream

Source: https://stackoverflow.com/questions/39682465/javascript-writing-to-download-stream

zwimer avatar Aug 28 '25 20:08 zwimer

Gokapi is using https://github.com/jimmywarting/StreamSaver.js for that, which uses WriteableStream, or a Ponyfill for it. In the end it might also be a different problem, maybe related to the ServiceWorker, so this issue definitely needs more research. Unfortunately the FileSystemWritableFileStream you linked is not supported on Safari.

Forceu avatar Aug 29 '25 09:08 Forceu

Currently there is no way for Firefox and Safari to stream into a "save-as" file: https://caniuse.com/filesystem

psi-4ward avatar Oct 28 '25 11:10 psi-4ward