Skip to content

[RichDocuments] downloadAs JSON key mismatch - file download fails with Collabora Online #17076

Closed

Description

@chrip

Bug Description

When downloading/exporting documents from Collabora Online (richdocuments) in the Nextcloud Android app, the downloadAs JavaScript interface fails to parse the JSON message due to a key name mismatch between the JavaScript side and the Android side.

Root Cause

JavaScript side (mobile.js wraps the message):

{
  "MessageName": "downloadAs",
  "Values": {
    "url": "https://...",
    "format": "pdf"
  }
}

Android side (RichDocumentsEditorWebView.kt:133-149) expects:

{
  "URL": "https://...",
  "Type": "pdf"
}

The Android code uses downloadJson.getString("URL") and downloadJson.getString("TYPE") which fail because the actual JSON keys are lowercase (url, format).

Affected Code

  • app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.kt - RichDocumentsMobileInterface.downloadAs() method
  • Keys defined in companion object: URL = "URL", TYPE = "Type", FILENAME = "filename"

Reproduction Steps

  1. Open a document in Collabora Online via the Nextcloud Android app
  2. Click "Export as PDF" (or any other export/download format)
  3. The download silently fails (caught by JSONException catch block at line 148)

Expected Behavior

The file should download to the device's Downloads folder.

Proposed Fix

Update downloadAs() to handle both key naming conventions:

@JavascriptInterface
fun downloadAs(json: String?) {
    try {
        json ?: return
        val downloadJson = JSONObject(json)
        
        // Support both old and new key formats from Collabora Online
        val urlStr = downloadJson.optString("URL", downloadJson.optString("url", ""))
        if (urlStr.isEmpty()) return
        val url = urlStr.toUri()
        
        // Support both "Type" and "format" keys
        val type = downloadJson.optString("Type", downloadJson.optString("format", ""))
        
        when {
            "print".equals(type, ignoreCase = true) -> printFile(url)
            "slideshow".equals(type, ignoreCase = true) -> showSlideShow(url)
            else -> {
                val downloadFileName = downloadJson.optString("filename", fileName)
                downloadFile(url, downloadFileName)
            }
        }
    } catch (e: JSONException) {
        Log_OC.e(this, "Failed to parse download json message: $e")
    }
}

Key changes:

  1. Use optString() instead of getString() to avoid exceptions on missing keys
  2. Check for both "URL" and "url" (case-insensitive)
  3. Check for both "Type" and "format" (case-insensitive)
  4. Use ignoreCase = true for type comparison

Environment

  • Nextcloud Android app version: (check latest)
  • Collabora Online (richdocuments) server app version: 8.x+
  • This affects any Collabora Online version that sends url/format keys instead of URL/Type

Activity

  1. juliusknorr commented on May 21, 2026

    @juliusknorr
    Member

    I'm curious if that changed somehow or where the difference originated from.

    Type and URL are the underlying Collabora post messages (https://sdk.collaboraonline.com/docs/postmessage_api.html). Richdocuments has not changed it, so wondering if Collabora did rename those at some point?

    @pedropintosilva Do you have any insight there?

  2. juliusknorr commented on May 21, 2026

    @juliusknorr
    Member
  3. chrip commented on May 21, 2026

    @chrip
    Author

    Same bug in the iOS app nextcloud/ios#4114

  4. alperozturk96 commented on May 22, 2026

    @alperozturk96
    Collaborator

    I can create a PR with fallback to current one with findings here if nothing else needed to investigated?

  5. chrip commented on May 22, 2026

    @chrip
    Author

    @alperozturk96 Yes please, that would be great!
    Just for notice, the fix was AI-generated from static code analysis. I don't have an Android device to verify the actual behavior.

  6. linked a pull request that will close this issuefix(rich documents): download as #17083on May 22, 2026
  7. alperozturk96 commented on May 22, 2026

    @alperozturk96
    Collaborator

    I created PR but I can't find export button, what am I missing?

    Screen.Recording.2026-05-22.at.16.00.55.mp4
  8. chrip commented on May 26, 2026

    @chrip
    Author

    @alperozturk96 Thank you for this quick fix! Have you tested it in Collabora? In the screen recording is Euro-office shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions