Export large files to device

My goal is to gain better understanding of the codebase, to learn to create a PR for resolving a bug in DeltaChat Android and Desktop.

I wrote a webxdc app.
This app locally generates large binary files, often more than 300 MB, and stores them as blobs in the webview’s IndexedDB.
I want my app to be able to export those files to the device that’s running DeltaChat.

Desktop: works for files smaller than ~ 220 MB. Larger than that, the file gets written, but has a size of 0 B.

Android: works for files smaller than ~ 70 MB. Larger than that, I get the following errors:

  • Error invoking sendToChat: Java exception was raised during method invocation.
  • Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing.

How is transferring large files from IndexedDB to device storage supposed to work?
I have no idea how to debug this, where do I start?

What I’ve got so far:
(sorry for no proper links: the forum doesn’t let me post them)


webxdc.js:99-151 — /deltachat/deltachat-android/blob/847c63a9/src/main/res/raw/webxdc.js#L99-L151

The webxdc.sendToChat method gets passed the file blob, serializes it to base64:

base64content = await blobToBase64(message.file.blob);

and passes it as a stringified JSON to the InternalJSApi.


WebxdcActivity.java:757-784 — /deltachat/deltachat-android/blob/847c63a9/src/main/java/org/thoughtcrime/securesms/WebxdcActivity.java#L757-L784

The InternalJSApi.sendToChat method deserializes the base64 back to binary:

data = Base64.decode(jsonObject.getString("base64"), Base64.NO_WRAP | Base64.NO_PADDING);

and passes it as an application/octet-stream to the DcHelper.

Questions:

  1. Why does the data have to be serialized to base64 for passing the js → java barrier?
  2. Could this be made more efficient by directly passing binary (“application/octet-stream”?) to reduce memory usage?

DcHelper.java:331-352 — /deltachat/deltachat-android/blob/847c63a9/src/main/java/org/thoughtcrime/securesms/connect/DcHelper.java#L331-L352

The DcHelper.sendToChat method creates an intent out of the data:

Uri uri = PersistentBlobProvider.getInstance().create(activity, data, mimeType, fileName);
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, uri);
ShareUtil.setSharedTitle(intent, activity.getString(R.string.send_file_to, fileName));

and starts an activity (which comes from the “DcContext”) with it.


Aaand that’s where my understanding currently ends.

Where do the error messages above come from?

And most importantly, any ideas how to fix this?

I’d be grateful for your help in getting further :slight_smile:

Well I don’t think you actually transfer blobs from the IndexedDB to the device storage directly. You load the blob from the IndexedDB into memory (into a variable) and then use that variable to export the blob.

If you are getting indexedDB related errors when exporting the file then it might be that you are handling indexedDB stuff in the wrong way and is killing the file export before it can finish.

But I don’t really have much experience using indexedDB directly so can’t say for sure, specially not without seeing the code.

Using indexedDB directly can be a bit tricky, you might want to try out one of the wrappers libraries that exist like localForage (highly recommended for simplicity), otherwise at least use one that turns it into a promise-based API.

That might be a problem with your app TBH. I just tried a 1 GB file on Desktop and it worked OK.

But yes, the fact that we require to convert the file to a base64 string instead of streaming it or just using the native <a download> sucks. Can cause the system to run out of memory.
And yes, I think one can optimize that.

Start by not using IndexedDB and passing a big base64 string to sendToChat directly.
Then also provide Delta Chat logs (not only the WebXDC app’s console).