Open-source download manager & offline media toolbox for YouTube, music, social media, learning platforms & 1,800+ sites. Built with Tauri 2.0, Rust & Svelte.
Rust downloadmanagerguihacktoberfesthomebrewlightweightlinuxlinux-distributionlocal-firstmacosmacos-packagemedia-toolboxopen-sourcerustsveltetauritrendingtrending-repositorieswindowswindows-desktopytdlp
feat(clipboard): add functionality to copy files to clipboard across platforms
Tonho545e58966944
diff --git a/src-tauri/omniget-core/src/core/clipboard.rs b/src-tauri/omniget-core/src/core/clipboard.rsnew file mode 100644index 00000000..8a9b0ed0--- /dev/null+++ b/src-tauri/omniget-core/src/core/clipboard.rs@@ -0,0 +1,151 @@+use std::path::Path;++#[cfg(target_os = "windows")]+const CREATE_NO_WINDOW: u32 = 0x08000000;++const MAX_CLIPBOARD_FILE_SIZE: u64 = 1_073_741_824; // 1 GB++pub async fn copy_file_to_clipboard(path: &Path) -> anyhow::Result<()> {+ let metadata = tokio::fs::metadata(path).await?;+ if metadata.len() > MAX_CLIPBOARD_FILE_SIZE {+ tracing::info!(+ "[clipboard] skipping copy: file too large ({} bytes)",+ metadata.len()+ );+ return Ok(());+ }++ let path_str = path.to_string_lossy().to_string();++ #[cfg(target_os = "macos")]+ {+ copy_file_macos(&path_str).await+ }++ #[cfg(target_os = "linux")]+ {+ copy_file_linux(&path_str).await+ }++ #[cfg(target_os = "windows")]+ {+ copy_file_windows(&path_str).await+ }+}++#[cfg(target_os = "macos")]+async fn copy_file_macos(path: &str) -> anyhow::Result<()> {+ let output = tokio::process::Command::new("osascript")+ .args([+ "-e",+ &format!("set the clipboard to POSIX file \"{}\"", path),+ ])+ .output()+ .await?;++ if !output.status.success() {+ let stderr = String::from_utf8_lossy(&output.stderr);+ return Err(anyhow::anyhow!("osascript failed: {}", stderr));+ }++ tracing::info!("[clipboard] copied file to clipboard (macOS): {}", path);+ Ok(())+}++#[cfg(target_os = "linux")]+async fn copy_file_linux(path: &str) -> anyhow::Result<()> {+ let uri = format!("file://{}", path);++ let xclip = tokio::process::Command::new("xclip")+ .args(["-selection", "clipboard", "-target", "text/uri-list"])+ .stdin(std::process::Stdio::piped())+ .spawn();++ match xclip {+ Ok(mut child) => {+ if let Some(ref mut stdin) = child.stdin {+ use tokio::io::AsyncWriteExt;+ stdin.write_all(uri.as_bytes()).await?;+ }+ let output = child.wait_with_output().await?;+ if output.status.success() {+ tracing::info!("[clipboard] copied file to clipboard (xclip): {}", path);+ return Ok(());+ }+ }+ Err(_) => {+ tracing::debug!("[clipboard] xclip not found, trying xsel");+ }+ }++ let xsel = tokio::process::Command::new("xsel")+ .args(["--clipboard", "--input"])+ .stdin(std::process::Stdio::piped())+ .spawn();++ match xsel {+ Ok(mut child) => {+ if let Some(ref mut stdin) = child.stdin {+ use tokio::io::AsyncWriteExt;+ stdin.write_all(uri.as_bytes()).await?;+ }+ let output = child.wait_with_output().await?;+ if output.status.success() {+ tracing::info!("[clipboard] copied file URI to clipboard (xsel): {}", path);+ return Ok(());+ }+ }+ Err(_) => {+ tracing::debug!("[clipboard] xsel not found, trying wl-copy");+ }+ }++ let wl_copy = tokio::process::Command::new("wl-copy")+ .args(["--type", "text/uri-list"])+ .stdin(std::process::Stdio::piped())+ .spawn();++ match wl_copy {+ Ok(mut child) => {+ if let Some(ref mut stdin) = child.stdin {+ use tokio::io::AsyncWriteExt;+ stdin.write_all(uri.as_bytes()).await?;+ }+ let output = child.wait_with_output().await?;+ if output.status.success() {+ tracing::info!(+ "[clipboard] copied file to clipboard (wl-copy): {}",+ path+ );+ return Ok(());+ }+ }+ Err(_) => {}+ }++ Err(anyhow::anyhow!(+ "No clipboard tool found (tried xclip, xsel, wl-copy)"+ ))+}++#[cfg(target_os = "windows")]+async fn copy_file_windows(path: &str) -> anyhow::Result<()> {+ let ps_script = format!(+ "Set-Clipboard -LiteralPath '{}'",+ path.replace('\'', "''")+ );++ let output = tokio::process::Command::new("powershell")+ .args(["-NoProfile", "-NonInteractive", "-Command", &ps_script])+ .creation_flags(CREATE_NO_WINDOW)+ .output()+ .await?;++ if !output.status.success() {+ let stderr = String::from_utf8_lossy(&output.stderr);+ return Err(anyhow::anyhow!("PowerShell Set-Clipboard failed: {}", stderr));+ }++ tracing::info!("[clipboard] copied file to clipboard (Windows): {}", path);+ Ok(())+}