Skip to content

Commit 683f7fe

Browse files
authored
feat(desktop): add "Copy image" to image right-click context menu (#1579)
1 parent dcbb3ff commit 683f7fe

7 files changed

Lines changed: 307 additions & 13 deletions

File tree

‎desktop/src-tauri/Cargo.lock‎

Lines changed: 163 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎desktop/src-tauri/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ tauri-plugin-global-shortcut = "2"
9595
tauri-plugin-notification = "2.3.3"
9696
uuid = { version = "1", features = ["v4"] }
9797
png = "0.18"
98+
arboard = "3"
99+
image = { version = "0.25", default-features = false, features = ["jpeg", "png", "webp", "gif"] }
98100
zip = "8"
99101
sherpa-onnx = "1.12"
100102
regex = "1"

‎desktop/src-tauri/src/commands/media_download.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,66 @@ pub async fn fetch_media_bytes(
155155
Ok(tauri::ipc::Response::new(bytes))
156156
}
157157

158+
/// Copy an image from a relay media URL directly to the system clipboard.
159+
///
160+
/// Fetches the image, decodes it to RGBA8, and writes it to the clipboard via
161+
/// `arboard`. Same SSRF validation, size cap, and content policy as the download
162+
/// commands above.
163+
///
164+
/// `arboard` requires clipboard access on the main thread on macOS, so the
165+
/// write is dispatched via `run_on_main_thread` and the result is relayed back
166+
/// through a one-shot channel.
167+
#[tauri::command]
168+
pub async fn copy_image_to_clipboard(
169+
url: String,
170+
app: tauri::AppHandle,
171+
state: State<'_, AppState>,
172+
) -> Result<(), String> {
173+
let relay_base = relay_api_base_url_with_override(&state);
174+
validate_download_url(&url, &relay_base)?;
175+
176+
let bytes = fetch_blob_bytes(&url, &state).await?;
177+
detect_and_validate_mime(&bytes)?;
178+
179+
let img =
180+
image::load_from_memory(&bytes).map_err(|e| format!("failed to decode image: {e}"))?;
181+
182+
// Guard against decompression bombs: a small compressed file can decode to
183+
// a huge RGBA buffer. Cap at 50 MiB (matching the download size cap).
184+
let pixels = img.width() as u64 * img.height() as u64;
185+
if pixels * 4 > MAX_DOWNLOAD_BYTES {
186+
return Err("image too large to copy to clipboard".to_string());
187+
}
188+
189+
let rgba = img.to_rgba8();
190+
let (width, height) = (rgba.width() as usize, rgba.height() as usize);
191+
let raw = rgba.into_raw();
192+
193+
// arboard requires main-thread access on macOS. Use a sync channel so the
194+
// async command can await the result.
195+
let (tx, rx) = std::sync::mpsc::sync_channel::<Result<(), String>>(1);
196+
app.run_on_main_thread(move || {
197+
let result = arboard::Clipboard::new()
198+
.map_err(|e| format!("clipboard error: {e}"))
199+
.and_then(|mut clipboard| {
200+
clipboard
201+
.set_image(arboard::ImageData {
202+
width,
203+
height,
204+
bytes: std::borrow::Cow::Owned(raw),
205+
})
206+
.map_err(|e| format!("clipboard error: {e}"))
207+
});
208+
// Ignore send errors — the receiver dropped only if the command was
209+
// cancelled, in which case nobody is waiting for the result.
210+
let _ = tx.send(result);
211+
})
212+
.map_err(|e| format!("main thread dispatch failed: {e}"))?;
213+
214+
rx.recv()
215+
.map_err(|_| "clipboard result channel closed unexpectedly".to_string())?
216+
}
217+
158218
/// Fetch blob bytes from a (pre-validated) relay media URL through the app's
159219
/// HTTP client, enforcing the download size cap. The caller is responsible for
160220
/// validating the URL origin and for any content-type checks on the result.

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ pub fn run() {
508508
download_image,
509509
download_file,
510510
fetch_media_bytes,
511+
copy_image_to_clipboard,
511512
list_relay_members,
512513
get_my_relay_membership,
513514
add_relay_member,

0 commit comments

Comments
 (0)