@@ -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.
0 commit comments