diff --git a/package.json b/package.json index 3a2a3fd27..688723916 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e357dd93d..bcdb2c727 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -221,6 +221,9 @@ importers: slate-react: specifier: ^0.125.1 version: 0.125.1(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(slate-dom@0.124.1(slate@0.124.1))(slate@0.124.1) + 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 + create-web-stream@1.1.3: + resolution: {integrity: sha512-ocdWcsi6F8/8f0L/Fsbt3UmPSB+7H5hXR0aRr7Wiu7TXC1Z8vwlwxpeO4+gMgJ8g4TqwM5oTnSid43Jxm2ArwA==} + engines: {node: '>=18.9.0'} + cross-fetch@4.0.0: resolution: {integrity: sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==} @@ -4875,6 +4882,9 @@ packages: tar-mini@0.2.0: resolution: {integrity: sha512-+qfUHz700DWnRutdUsxRRVZ38G1Qr27OetwaMYTdg8hcPxf46U0S1Zf76dQMWRBmusOt2ZCK5kbIaiLkoGO7WQ==} + tauri-plugin-android-fs-api@28.4.0: + resolution: {integrity: sha512-YOo1P+cRjuaoPbTNZprMhr56ruKO4hIDq7kJOum91NqkN+VWY9F+UPAdjZokhzJSsbkeY0nsb1VDLXb2/h3f3w==} + temp-dir@2.0.0: resolution: {integrity: sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==} engines: {node: '>=8'} @@ -7989,6 +7999,8 @@ snapshots: optionalDependencies: typescript: 5.9.3 + create-web-stream@1.1.3: {} + cross-fetch@4.0.0: dependencies: node-fetch: 2.7.0 @@ -9753,6 +9765,11 @@ snapshots: tar-mini@0.2.0: {} + tauri-plugin-android-fs-api@28.4.0: + dependencies: + '@tauri-apps/api': 2.11.0 + create-web-stream: 1.1.3 + temp-dir@2.0.0: {} tempy@0.6.0: diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index f8774fab1..a76004252 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -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" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 21d72c09b..1c0a9fa1a 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -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"] diff --git a/src-tauri/capabilities/android.json b/src-tauri/capabilities/android.json new file mode 100644 index 000000000..c557d637e --- /dev/null +++ b/src-tauri/capabilities/android.json @@ -0,0 +1,7 @@ +{ + "$schema": "../gen/schemas/mobile-schema.json", + "identifier": "android-capability", + "platforms": ["android"], + "windows": ["main"], + "permissions": ["android-fs:default"] +} diff --git a/src-tauri/gen/android/app/src/main/AndroidManifest.xml b/src-tauri/gen/android/app/src/main/AndroidManifest.xml index 951a539d6..b4ed86945 100644 --- a/src-tauri/gen/android/app/src/main/AndroidManifest.xml +++ b/src-tauri/gen/android/app/src/main/AndroidManifest.xml @@ -34,6 +34,10 @@ + + + + @@ -48,4 +52,7 @@ android:resource="@xml/file_paths" /> + + + diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4cc9d6687..edfa467b0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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()) diff --git a/src/app/components/image-viewer/ImageViewer.tsx b/src/app/components/image-viewer/ImageViewer.tsx index 55df2294b..ab3fde562 100644 --- a/src/app/components/image-viewer/ImageViewer.tsx +++ b/src/app/components/image-viewer/ImageViewer.tsx @@ -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(); diff --git a/src/app/components/message/FileHeader.tsx b/src/app/components/message/FileHeader.tsx index b7cc7fc77..02fe9afd2 100644 --- a/src/app/components/message/FileHeader.tsx +++ b/src/app/components/message/FileHeader.tsx @@ -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]) ); diff --git a/src/app/components/message/content/FileContent.tsx b/src/app/components/message/content/FileContent.tsx index eef1f7937..596a43d49 100644 --- a/src/app/components/message/content/FileContent.tsx +++ b/src/app/components/message/content/FileContent.tsx @@ -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) => ( downloadState.status === AsyncStatus.Success - ? FileSaver.saveAs(downloadState.data, getDownloadFilename(body)) + ? void saveFileToDevice(downloadState.data, getDownloadFilename(body), mimeType) : download() } disabled={downloadState.status === AsyncStatus.Loading} diff --git a/src/app/utils/download.ts b/src/app/utils/download.ts index 6e853e6c9..7ab144243 100644 --- a/src/app/utils/download.ts +++ b/src/app/utils/download.ts @@ -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 { + 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 { + 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); +}