Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(desktop): upload large pasted files from Rust (#9562)

Eric Hayes686be8d0998a
diff --git a/apps/web/native-commands.json b/apps/web/native-commands.json
index 4a8c3dc70b..03dd249ee8 100644
--- a/apps/web/native-commands.json
+++ b/apps/web/native-commands.json
@@ -116,6 +116,7 @@
"send_linux_notification",
"send_macos_notification",
"set_bundle_reload_hold",
+ "stage_desktop_pasteboard_files",
"upload_staged_file_to_presigned_url",
"whatsapp_passkey_authenticate"
]
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.test.ts
new file mode 100644
index 0000000000..7509a3da0b
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.test.ts
@@ -0,0 +1,89 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({
+ invoke: vi.fn(),
+ isDesktop: vi.fn(() => true),
+}));
+vi.mock('@tauri-apps/api/core', () => ({
+ invoke: mocks.invoke,
+ convertFileSrc: (path: string) => path,
+}));
+vi.mock('@core/util/platform', () => ({
+ isDesktopPlatform: mocks.isDesktop,
+}));
+
+import { getNativeStagedUpload } from '@core/mobile/nativeStagedUpload';
+import {
+ shouldStageDesktopPasteFiles,
+ stageDesktopPasteFiles,
+} from './desktopPasteboardFiles';
+
+function sizedFile(name: string, type: string, size: number): File {
+ const file = new File([], name, { type });
+ Object.defineProperty(file, 'size', { value: size });
+ return file;
+}
+
+function entryFile(entry: FileSystemFileEntry): Promise<File> {
+ return new Promise((resolve) => entry.file(resolve));
+}
+
+describe('desktop pasted files', () => {
+ beforeEach(() => {
+ mocks.invoke.mockReset();
+ mocks.isDesktop.mockReturnValue(true);
+ });
+
+ it('stages only when a desktop paste carries a large file', () => {
+ const small = sizedFile('note.txt', 'text/plain', 1024);
+ const large = sizedFile('clip.mov', 'video/quicktime', 2_000_000_000);
+
+ expect(shouldStageDesktopPasteFiles([small])).toBe(false);
+ expect(shouldStageDesktopPasteFiles([small, large])).toBe(true);
+
+ mocks.isDesktop.mockReturnValue(false);
+ expect(shouldStageDesktopPasteFiles([large])).toBe(false);
+ });
+
+ it('tags pasted files matching the staged pasteboard files', async () => {
+ const video = sizedFile('clip.mov', 'video/quicktime', 2_000_000_000);
+ const image = sizedFile('image.png', 'image/png', 5000);
+ mocks.invoke.mockResolvedValue([
+ {
+ token: 'paste-stage-abc',
+ name: 'clip.mov',
+ mimeType: 'application/octet-stream',
+ size: 2_000_000_000,
+ sha256: 'ab'.repeat(32),
+ },
+ ]);
+
+ const entries = await stageDesktopPasteFiles([video, image]);
+
+ expect(mocks.invoke).toHaveBeenCalledWith('stage_desktop_pasteboard_files');
+ const [first, second] = await Promise.all(entries.map(entryFile));
+ expect(first).toBe(video);
+ expect(second).toBe(image);
+ expect(getNativeStagedUpload(video)).toEqual({
+ source: 'pasteboard',
+ token: 'paste-stage-abc',
+ name: 'clip.mov',
+ mimeType: 'video/quicktime',
+ size: 2_000_000_000,
+ sha256: 'ab'.repeat(32),
+ previewSrc: undefined,
+ });
+ expect(getNativeStagedUpload(image)).toBeUndefined();
+ });
+
+ it('falls back to browser files when staging fails', async () => {
+ const video = sizedFile('clip.mov', 'video/quicktime', 2_000_000_000);
+ mocks.invoke.mockRejectedValue(new Error('no pasteboard'));
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+
+ const entries = await stageDesktopPasteFiles([video]);
+
+ expect(await entryFile(entries[0])).toBe(video);
+ expect(getNativeStagedUpload(video)).toBeUndefined();
+ });
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.ts
new file mode 100644
index 0000000000..1a75e4d393
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/desktopPasteboardFiles.ts
@@ -0,0 +1,61 @@
+import {
+ type NativeStagedUploadData,
+ tagNativeStagedUpload,
+} from '@core/mobile/nativeStagedUpload';
+import { createSyntheticFileEntry } from '@core/util/dataTransfer';
+import { isDesktopPlatform } from '@core/util/platform';
+import { invoke } from '@tauri-apps/api/core';
+import { ResultAsync } from 'neverthrow';
+
+/**
+ * Pasted files at least this large upload from Rust. Below it the Tauri HTTP
+ * plugin's in-memory copy of the body is harmless.
+ */
+const NATIVE_PASTE_MIN_BYTES = 16 * 1024 * 1024;
+
+/**
+ * Whether a desktop paste should stage its files natively. The Tauri HTTP
+ * plugin copies an upload body into the renderer, so a large pasted video
+ * crashes the desktop app unless Rust uploads it from disk.
+ */
+export function shouldStageDesktopPasteFiles(files: File[]): boolean {
+ return (
+ isDesktopPlatform() &&
+ files.some((file) => file.size >= NATIVE_PASTE_MIN_BYTES)
+ );
+}
+
+/**
+ * Stages the pasteboard's files in Rust and tags each matching pasted `File`,
+ * so uploads stream the staged copy instead of reading it into JS. Files Rust
+ * can't match (an image copied as data, other platforms) stay browser files.
+ */
+export async function stageDesktopPasteFiles(
+ files: File[]
+): Promise<FileSystemFileEntry[]> {
+ const staged = await ResultAsync.fromPromise(
+ invoke<NativeStagedUploadData[]>('stage_desktop_pasteboard_files'),
+ (error) => error
+ );
+ if (staged.isErr()) {
+ console.error('Failed to stage pasted files', staged.error);
+ return files.map(createSyntheticFileEntry);
+ }
+
+ const unclaimed = [...staged.value];
+ for (const file of files) {
+ const index = unclaimed.findIndex(
+ (media) => media.name === file.name && media.size === file.size
+ );
+ if (index === -1) continue;
+ const [media] = unclaimed.splice(index, 1);
+ // Previews keep using the real File; only the upload goes through Rust.
+ tagNativeStagedUpload(file, 'pasteboard', {
+ ...media,
+ mimeType: file.type || media.mimeType,
+ previewPath: null,
+ });
+ }
+
+ return files.map(createSyntheticFileEntry);
+}
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/filePastePlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/filePastePlugin.ts
index b284e2fabb..e14be4b994 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/filePastePlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/file-paste/filePastePlugin.ts
@@ -10,6 +10,10 @@ import {
type LexicalEditor,
PASTE_COMMAND,
} from 'lexical';
+import {
+ shouldStageDesktopPasteFiles,
+ stageDesktopPasteFiles,
+} from './desktopPasteboardFiles';
type FilePastePluginProps = {
onPasteFilesAndDirs: (
@@ -18,6 +22,13 @@ type FilePastePluginProps = {
) => void;
};
+async function pasteStagedDesktopFiles(
+ files: File[],
+ props: FilePastePluginProps
+) {
+ props.onPasteFilesAndDirs(await stageDesktopPasteFiles(files), []);
+}
+
function registerFilePastePlugin(
editor: LexicalEditor,
props: FilePastePluginProps
@@ -61,6 +72,17 @@ function registerFilePastePlugin(
props.onPasteFilesAndDirs([], directoryEntries);
return true;
}
+
+ const files = Array.from(data.files);
+ if (shouldStageDesktopPasteFiles(files)) {
+ event.preventDefault();
+ fireAndForget(
+ pasteStagedDesktopFiles(files, props),
+ 'markdown-file-paste'
+ );
+ return true;
+ }
+
props.onPasteFilesAndDirs(fileEntries, []);
return true;
},
diff --git a/apps/web/src/lib/core/mobile/nativeStagedUpload.ts b/apps/web/src/lib/core/mobile/nativeStagedUpload.ts
index c128626f92..f30de45742 100644
--- a/apps/web/src/lib/core/mobile/nativeStagedUpload.ts
+++ b/apps/web/src/lib/core/mobile/nativeStagedUpload.ts
@@ -37,11 +37,26 @@ export function createNativeStagedUploadFile(
source: NativeStagedUploadSource,
media: NativeStagedUploadData
): File | null {
+ const file = new File([], media.name ?? '', {
+ type: media.mimeType ?? undefined,
+ });
+ return tagNativeStagedUpload(file, source, media) ? file : null;
+}
+
+/**
+ * Marks `file` as staged natively, so uploads send the staged copy from Rust
+ * instead of reading `file` into JS. Desktop tags the real pasted `File`, which
+ * keeps its bytes for local previews.
+ */
+export function tagNativeStagedUpload(
+ file: File,
+ source: NativeStagedUploadSource,
+ media: NativeStagedUploadData
+): boolean {
if (!media.token || !media.name || !media.mimeType || media.size == null) {
- return null;
+ return false;
}
- const file = new File([], media.name, { type: media.mimeType });
nativeStagedUploads.set(file, {
source,
token: media.token,
@@ -53,7 +68,7 @@ export function createNativeStagedUploadFile(
? convertFileSrc(media.previewPath)
: undefined,
});
- return file;
+ return true;
}
export function getNativeStagedUpload(
diff --git a/apps/web/src/lib/queries/email/local-drafts.ts b/apps/web/src/lib/queries/email/local-drafts.ts
index d3a78c0eeb..804c95427f 100644
--- a/apps/web/src/lib/queries/email/local-drafts.ts
+++ b/apps/web/src/lib/queries/email/local-drafts.ts
@@ -270,8 +270,11 @@ async function copyFile(file: File): Promise<Blob> {
pending = (async () => {
const staged = getNativeStagedUpload(file);
if (!staged) return file;
- if (!staged.previewSrc)
+ if (!staged.previewSrc) {
+ // Desktop tags the real pasted File, which still holds its bytes.
+ if (file.size === staged.size) return file;
throw new Error('Attachment bytes are unavailable on this device');
+ }
const response = await fetch(staged.previewSrc);
if (!response.ok)
throw new Error('Unable to preserve attachment on this device');
diff --git a/apps/web/tauri/src-tauri/README.md b/apps/web/tauri/src-tauri/README.md
index cdebe746b3..60ae1de5b8 100644
--- a/apps/web/tauri/src-tauri/README.md
+++ b/apps/web/tauri/src-tauri/README.md
@@ -29,6 +29,18 @@ its existing `android_mobile_plugin` export commands. Older iOS installations
retain Web Share until the next native release; see
[the bridge guide](../file_export_plugin/AGENTS.md).
+## Desktop pasted files
+
+The Tauri HTTP plugin copies an upload body into the renderer, so a large
+pasted file (a video copied in Finder) crashed the desktop app. A desktop
+paste carrying a file of 16 MB or more calls `stage_desktop_pasteboard_files`
+(`src/desktop/pasteboard.rs`): on macOS it reads the pasteboard's file URLs,
+clones each file into the pasteboard staging directory, and hashes it, as the
+iOS pasteboard plugin stages images. The web client tags the pasted `File`s
+with the staged token, keeps them for local previews, and uploads through
+`upload_staged_file_to_presigned_url`. Other desktop platforms stage nothing
+and keep the browser upload. This needs a desktop native release.
+
## Desktop app icons
Only desktop icons carry the ALPHA badge; iOS and Android icons are unbadged.
diff --git a/apps/web/tauri/src-tauri/src/desktop.rs b/apps/web/tauri/src-tauri/src/desktop.rs
index 095541c32d..0a963218b6 100644
--- a/apps/web/tauri/src-tauri/src/desktop.rs
+++ b/apps/web/tauri/src-tauri/src/desktop.rs
@@ -8,3 +8,4 @@
//! tooling/xtask/.../scripts/ios_native_tree.sh).
pub(crate) mod macrod;
+pub(crate) mod pasteboard;
diff --git a/apps/web/tauri/src-tauri/src/desktop/pasteboard.rs b/apps/web/tauri/src-tauri/src/desktop/pasteboard.rs
new file mode 100644
index 0000000000..ee472d5d6b
--- /dev/null
+++ b/apps/web/tauri/src-tauri/src/desktop/pasteboard.rs
@@ -0,0 +1,164 @@
+//! Stages files pasted from the macOS pasteboard for a Rust-side upload, as
+//! the iOS pasteboard plugin does.
+//!
+//! Chromium hands a pasted Finder file to JS as a `File`, and uploading it
+//! through the Tauri HTTP plugin copies every byte into the renderer, which
+//! crashes the app for large videos. Instead the file is copied (an APFS clone
+//! on the same volume) into the pasteboard staging directory and hashed here;
+//! JS then uploads it with `upload_staged_file_to_presigned_url`.
+
+use serde::Serialize;
+use staged_upload_constants::{PASTEBOARD_STAGING_DIRECTORY_NAME, PASTEBOARD_TOKEN_PREFIX};
+use tauri::{AppHandle, Manager};
+
+#[derive(Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct StagedPasteboardFile {
+ token: String,
+ name: String,
+ mime_type: String,
+ size: u64,
+ sha256: String,
+}
+
+/// Stages every regular file on the general pasteboard. Returns an empty list
+/// when it holds no file URLs, and on platforms other than macOS.
+#[tauri::command]
+#[tracing::instrument(name = "desktop.pasteboard.stage", skip_all, err)]
+pub(crate) async fn stage_desktop_pasteboard_files(
+ app: AppHandle,
+) -> Result<Vec<StagedPasteboardFile>, String> {
+ let paths = pasteboard_file_paths();
+ if paths.is_empty() {
+ return Ok(Vec::new());
+ }
+
+ let staging_dir = app
+ .path()
+ .app_cache_dir()
+ .map_err(|error| format!("failed to resolve app cache directory: {error}"))?
+ .join(PASTEBOARD_STAGING_DIRECTORY_NAME);
+
+ tauri::async_runtime::spawn_blocking(move || {
+ std::fs::create_dir_all(&staging_dir)
+ .map_err(|error| format!("failed to create pasteboard staging directory: {error}"))?;
+ let mut staged = Vec::with_capacity(paths.len());
+ for path in paths {
+ if path.is_file() {
+ staged.push(stage_file(&path, &staging_dir)?);
+ }
+ }
+ tracing::info!(count = staged.len(), "staged desktop pasteboard files");
+ Ok(staged)
+ })
+ .await
+ .map_err(|error| format!("pasteboard staging task failed: {error}"))?
+}
+
+fn stage_file(
+ source: &std::path::Path,
+ staging_dir: &std::path::Path,
+) -> Result<StagedPasteboardFile, String> {
+ use sha2::{Digest, Sha256};
+ use std::io::Read;
+
+ let name = source
+ .file_name()
+ .and_then(|name| name.to_str())
+ .ok_or("pasted file has no valid name")?
+ .to_string();
+ let token = format!("{PASTEBOARD_TOKEN_PREFIX}{}", uuid::Uuid::new_v4().simple());
+ let target = staging_dir.join(format!("{token}-{name}"));
+
+ // std::fs::copy clones on APFS, so a same-volume video stages instantly.
+ let size = std::fs::copy(source, &target)
+ .map_err(|error| format!("failed to stage pasted file: {error}"))?;
+
+ let mut file = std::fs::File::open(&target)
+ .map_err(|error| format!("failed to open staged file: {error}"))?;
+ let mut hasher = Sha256::new();
+ let mut buffer = vec![0; 1024 * 1024];
+ loop {
+ let read = file
+ .read(&mut buffer)
+ .map_err(|error| format!("failed to hash staged file: {error}"))?;
+ if read == 0 {
+ break;
+ }
+ hasher.update(&buffer[..read]);
+ }
+ let sha256 = hasher
+ .finalize()
+ .iter()
+ .map(|byte| format!("{byte:02x}"))
+ .collect();
+
+ Ok(StagedPasteboardFile {
+ mime_type: mime_guess::from_path(&name)
+ .first_or_octet_stream()
+ .essence_str()
+ .to_string(),
+ token,
+ name,
+ size,
+ sha256,
+ })
+}
+
+#[cfg(target_os = "macos")]
+fn pasteboard_file_paths() -> Vec<std::path::PathBuf> {
+ use objc2::msg_send;
+ use objc2::rc::{Retained, autoreleasepool};
+ use objc2::runtime::{AnyClass, AnyObject};
+ use objc2_foundation::{NSString, ns_string};
+
+ autoreleasepool(|_| {
+ let (Some(pasteboard_class), Some(url_class)) =
+ (AnyClass::get(c"NSPasteboard"), AnyClass::get(c"NSURL"))
+ else {
+ return Vec::new();
+ };
+
+ // SAFETY: standard AppKit/Foundation selectors with their documented
+ // signatures; nullable returns are read as `Option`.
+ unsafe {
+ let pasteboard: Option<Retained<AnyObject>> =
+ msg_send![pasteboard_class, generalPasteboard];
+ let Some(pasteboard) = pasteboard else {
+ return Vec::new();
+ };
+ let items: Option<Retained<AnyObject>> = msg_send![&pasteboard, pasteboardItems];
+ let Some(items) = items else {
+ return Vec::new();
+ };
+
+ let count: usize = msg_send![&items, count];
+ let mut paths = Vec::with_capacity(count);
+ for index in 0..count {
+ let item: Retained<AnyObject> = msg_send![&items, objectAtIndex: index];
+ let url_string: Option<Retained<NSString>> =
+ msg_send![&item, stringForType: ns_string!("public.file-url")];
+ let Some(url_string) = url_string else {
+ continue;
+ };
+ // Finder writes file reference URLs (file:///.file/id=...);
+ // filePathURL resolves them to a path.
+ let url: Option<Retained<AnyObject>> =
+ msg_send![url_class, URLWithString: &*url_string];
+ let Some(url) = url else { continue };
+ let path_url: Option<Retained<AnyObject>> = msg_send![&url, filePathURL];
+ let Some(path_url) = path_url else { continue };
+ let path: Option<Retained<NSString>> = msg_send![&path_url, path];
+ if let Some(path) = path {
+ paths.push(std::path::PathBuf::from(path.to_string()));
+ }
+ }
+ paths
+ }
+ })
+}
+
+#[cfg(not(target_os = "macos"))]
+fn pasteboard_file_paths() -> Vec<std::path::PathBuf> {
+ Vec::new()
+}
diff --git a/apps/web/tauri/src-tauri/src/lib.rs b/apps/web/tauri/src-tauri/src/lib.rs
index b12254baf8..e1fcb6ae1f 100644
--- a/apps/web/tauri/src-tauri/src/lib.rs
+++ b/apps/web/tauri/src-tauri/src/lib.rs
@@ -460,6 +460,8 @@ pub fn run_with(builder: tauri::Builder<AppRuntime>, configure_window: Configure
desktop::macrod::macrod_stop,
#[cfg(desktop)]
desktop::macrod::macrod_status,
+ #[cfg(desktop)]
+ desktop::pasteboard::stage_desktop_pasteboard_files,
])
.setup(move |app| {
#[cfg(desktop)]