fix(android): save downloaded attachments via native file system plugin

This commit is contained in:
Erwan Leboucher committed 2026-07-17 20:50:19 +02:00
1 parent eb445e15e5
commit 38fd34f708
11 files changed
+112 -10

No files matched your search

+1
View File
@@ -102,6 +102,7 @@
"slate-dom": "^0.124.1",
"slate-history": "^0.113.1",
"slate-react": "^0.125.1",
"tauri-plugin-android-fs-api": "28.4.0",
"ua-parser-js": "^2.0.10",
"virtua": "^0.49.2",
"workbox-precaching": "^7.4.1"
+17
View File
@@ -221,6 +221,9 @@ importers:
slate-react:
specifier: ^0.125.1
version: 0.125.1([email protected]([email protected]))([email protected])([email protected]([email protected]))([email protected])
tauri-plugin-android-fs-api:
specifier: 28.4.0
version: 28.4.0
ua-parser-js:
specifier: ^2.0.10
version: 2.0.10
@@ -3338,6 +3341,10 @@ packages:
typescript:
optional: true
[email protected]:
resolution: {integrity: sha512-ocdWcsi6F8/8f0L/Fsbt3UmPSB+7H5hXR0aRr7Wiu7TXC1Z8vwlwxpeO4+gMgJ8g4TqwM5oTnSid43Jxm2ArwA==}
engines: {node: '>=18.9.0'}
[email protected]:
resolution: {integrity: sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==}
@@ -4875,6 +4882,9 @@ packages:
[email protected]:
resolution: {integrity: sha512-+qfUHz700DWnRutdUsxRRVZ38G1Qr27OetwaMYTdg8hcPxf46U0S1Zf76dQMWRBmusOt2ZCK5kbIaiLkoGO7WQ==}
[email protected]:
resolution: {integrity: sha512-YOo1P+cRjuaoPbTNZprMhr56ruKO4hIDq7kJOum91NqkN+VWY9F+UPAdjZokhzJSsbkeY0nsb1VDLXb2/h3f3w==}
[email protected]:
resolution: {integrity: sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==}
engines: {node: '>=8'}
@@ -7989,6 +7999,8 @@ snapshots:
optionalDependencies:
typescript: 5.9.3
[email protected]: {}
[email protected]:
dependencies:
node-fetch: 2.7.0
@@ -9753,6 +9765,11 @@ snapshots:
[email protected]: {}
[email protected]:
dependencies:
'@tauri-apps/api': 2.11.0
create-web-stream: 1.1.3
[email protected]: {}
[email protected]:
+31
View File
@@ -4091,6 +4091,7 @@ dependencies = [
"sha2",
"tauri",
"tauri-build",
"tauri-plugin-android-fs",
"tauri-plugin-clipboard-manager",
"tauri-plugin-deep-link",
"tauri-plugin-edge-to-edge",
@@ -4632,6 +4633,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sync_async"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b815ef8e053247ad785b136d233ee9c9872eeda5319f2719d57cac88270c3dd9"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "sync_wrapper"
version = "1.0.2"
@@ -4888,6 +4900,25 @@ dependencies = [
"walkdir",
]
[[package]]
name = "tauri-plugin-android-fs"
version = "28.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "999e39313646a1f48f65270e75df20664a692bf28fa21f44a16bf8706c154f31"
dependencies = [
"base64 0.22.1",
"glob",
"percent-encoding",
"schemars 0.8.22",
"serde",
"serde_json",
"sync_async",
"tauri",
"tauri-plugin",
"tauri-plugin-fs",
"thiserror 2.0.18",
]
[[package]]
name = "tauri-plugin-clipboard-manager"
version = "2.3.2"
+3
View File
@@ -60,6 +60,9 @@ gtk = "0.18"
tauri-plugin-notifications = { git = "https://github.com/SableClient/tauri-plugin-notifications.git", rev = "9d15b2b2c8798dc29ce723d743f0f3b4921d6a54", features = ["push-notifications", "unified-push"] }
tauri-plugin-edge-to-edge = { git = "https://github.com/SableClient/tauri-plugin-edge-to-edge.git", rev = "33c6116c27be28c06df5a9d02231ecc5fdeb93c5" }
[target.'cfg(target_os = "android")'.dependencies]
tauri-plugin-android-fs = "=28.4.0"
[features]
default = ["wry"]
custom-protocol = ["tauri/custom-protocol"]
+7
View File
@@ -0,0 +1,7 @@
{
"$schema": "../gen/schemas/mobile-schema.json",
"identifier": "android-capability",
"platforms": ["android"],
"windows": ["main"],
"permissions": ["android-fs:default"]
}
@@ -34,6 +34,10 @@
<data android:scheme="sable" />
<data android:scheme="moe.sable.app" />
</intent-filter>
<!-- DEEP LINK PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
</activity>
@@ -48,4 +52,7 @@
android:resource="@xml/file_paths" />
</provider>
</application>
<!-- ANDROID FS PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
<!-- ANDROID FS PLUGIN. AUTO-GENERATED. DO NOT REMOVE. -->
</manifest>
+3
View File
@@ -205,6 +205,9 @@ pub fn run() {
#[cfg(mobile)]
let builder = builder.plugin(tauri_plugin_edge_to_edge::init());
#[cfg(target_os = "android")]
let builder = builder.plugin(tauri_plugin_android_fs::init());
let app = builder
.plugin(tauri_plugin_clipboard_manager::init())
.plugin(tauri_plugin_http::init())
@@ -1,6 +1,5 @@
import type { MouseEventHandler } from 'react';
import { useEffect, useRef, useState } from 'react';
import FileSaver from 'file-saver';
import classNames from 'classnames';
import {
Box,
@@ -37,7 +36,7 @@ import { CheckerboardIcon, CopyIcon, DownloadIcon } from '@phosphor-icons/react'
import FocusTrap from 'focus-trap-react';
import { stopPropagation } from '$utils/keyboard';
import { copyImageToClipboard } from '$utils/dom';
import { getDownloadFilename } from '$utils/download';
import { getDownloadFilename, saveFileToDevice } from '$utils/download';
export type ImageViewerProps = {
alt: string;
@@ -100,7 +99,7 @@ export const ImageViewer = as<'div', ImageViewerProps>(
const handleDownload = async () => {
const fileContent = await downloadMedia(src);
FileSaver.saveAs(fileContent, getDownloadFilename(filename, alt, 'image'));
await saveFileToDevice(fileContent, getDownloadFilename(filename, alt, 'image'));
};
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
+2 -3
View File
@@ -3,13 +3,12 @@ import { Download, sizedIcon } from '$components/icons/phosphor';
import type { ReactNode } from 'react';
import { useCallback } from 'react';
import type { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
import FileSaver from 'file-saver';
import { mimeTypeToExt } from '$utils/mimeTypes';
import { useMatrixClient } from '$hooks/useMatrixClient';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp } from '$utils/matrix';
import { getDownloadFilename } from '$utils/download';
import { getDownloadFilename, saveFileToDevice } from '$utils/download';
const badgeStyles = { maxWidth: toRem(100) };
@@ -32,7 +31,7 @@ export function FileDownloadButton({ filename, url, mimeType, encInfo }: FileDow
: await downloadMedia(mediaUrl);
const fileURL = URL.createObjectURL(fileContent);
FileSaver.saveAs(fileURL, getDownloadFilename(filename));
await saveFileToDevice(fileContent, getDownloadFilename(filename), mimeType);
return fileURL;
}, [mx, url, useAuthentication, mimeType, encInfo, filename])
);
@@ -14,7 +14,6 @@ import {
as,
} from 'folds';
import { ArrowRight, Download, sizedIcon, Warning } from '$components/icons/phosphor';
import FileSaver from 'file-saver';
import type { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
import FocusTrap from 'focus-trap-react';
import type { IFileInfo } from '$types/matrix/common';
@@ -31,7 +30,7 @@ import { stopPropagation } from '$utils/keyboard';
import { decryptFile, downloadEncryptedMedia, downloadMedia, mxcUrlToHttp } from '$utils/matrix';
import { useMediaAuthentication } from '$hooks/useMediaAuthentication';
import { ModalWide } from '$styles/Modal.css';
import { getDownloadFilename } from '$utils/download';
import { getDownloadFilename, saveFileToDevice } from '$utils/download';
const renderErrorButton = (retry: () => void, text: string) => (
<TooltipProvider
@@ -258,7 +257,7 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
: await downloadMedia(mediaUrl);
const fileURL = URL.createObjectURL(fileContent);
FileSaver.saveAs(fileURL, getDownloadFilename(body));
await saveFileToDevice(fileContent, getDownloadFilename(body), mimeType);
return fileURL;
}, [mx, url, useAuthentication, mimeType, encInfo, body])
);
@@ -273,7 +272,7 @@ export function DownloadFile({ body, mimeType, url, info, encInfo }: DownloadFil
size="400"
onClick={() =>
downloadState.status === AsyncStatus.Success
? FileSaver.saveAs(downloadState.data, getDownloadFilename(body))
? void saveFileToDevice(downloadState.data, getDownloadFilename(body), mimeType)
: download()
}
disabled={downloadState.status === AsyncStatus.Loading}
+36
View File
@@ -1,3 +1,8 @@
import FileSaver from 'file-saver';
import { isTauri } from '@tauri-apps/api/core';
import { type as osType } from '@tauri-apps/plugin-os';
import { fetch } from '$utils/fetch';
const INVALID_FILENAME_CHARS = /[<>:"/\\|?*]/g;
const CONTROL_CHARS = /\p{Cc}/gu;
const BIDI_CONTROL_CHARS = /[\u202a-\u202e\u2066-\u2069]/g;
@@ -44,3 +49,34 @@ export const getDownloadFilename = (
body?: unknown,
fallback = 'download'
): string => sanitizeDownloadFilename(getAttachmentFilename(filename, body, fallback), fallback);
async function resolveBlob(input: Blob | string): Promise<Blob> {
if (typeof input !== 'string') return input;
const response = await fetch(input);
return response.blob();
}
// On Android the browser anchor download is a no-op (the WebView has no download
// handler), so route through the native file system plugin; elsewhere file-saver works.
export async function saveFileToDevice(
input: Blob | string,
filename: string,
mimeType?: string
): Promise<void> {
if (isTauri() && osType() === 'android') {
const blob = await resolveBlob(input);
const bytes = new Uint8Array(await blob.arrayBuffer());
const { AndroidFs, AndroidPublicGeneralPurposeDir } = await import(
'tauri-plugin-android-fs-api'
);
const uri = await AndroidFs.createNewPublicFile(
AndroidPublicGeneralPurposeDir.Download,
filename,
mimeType || blob.type || null
);
await AndroidFs.writeFile(uri, bytes);
return;
}
FileSaver.saveAs(input, filename);
}