From 529d450732d014df4e107b138ee374d8acdaf650 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Wed, 6 Aug 2025 15:47:52 +0200 Subject: [PATCH 1/4] fix(linux): set suggested filename for download started handler --- .../linux-download-handler-destination.md | 5 ++ Cargo.lock | 39 ++++++++++++++ Cargo.toml | 1 + src/webkitgtk/web_context.rs | 53 ++++++++++++++----- 4 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 .changes/linux-download-handler-destination.md diff --git a/.changes/linux-download-handler-destination.md b/.changes/linux-download-handler-destination.md new file mode 100644 index 000000000..fd3f14bb1 --- /dev/null +++ b/.changes/linux-download-handler-destination.md @@ -0,0 +1,5 @@ +--- +"wry": "patch" +--- + +On Linux, the `download_started_handler` will now get the correct suggested destination path. diff --git a/Cargo.lock b/Cargo.lock index 46b99cb5d..442649e85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -533,6 +533,27 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.59.0", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -2018,6 +2039,12 @@ version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "orbclient" version = "0.3.48" @@ -2496,6 +2523,17 @@ dependencies = [ "bitflags 2.8.0", ] +[[package]] +name = "redox_users" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror 2.0.11", +] + [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -4096,6 +4134,7 @@ dependencies = [ "block2 0.6.0", "cookie", "crossbeam-channel", + "dirs", "dpi", "dunce", "gdkx11", diff --git a/Cargo.toml b/Cargo.toml index d1324a052..d26fbe266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ soup3 = { version = "0.5", optional = true } x11-dl = { version = "2.21", optional = true } gdkx11 = { version = "0.18", optional = true } percent-encoding = "2.3" +dirs = "6" [target."cfg(target_os = \"windows\")".dependencies] webview2-com = "0.38" diff --git a/src/webkitgtk/web_context.rs b/src/webkitgtk/web_context.rs index 42321ace0..1ed3a260d 100644 --- a/src/webkitgtk/web_context.rs +++ b/src/webkitgtk/web_context.rs @@ -12,6 +12,7 @@ use std::{ borrow::Cow, cell::RefCell, collections::VecDeque, + env::current_dir, path::{Path, PathBuf}, rc::Rc, sync::{ @@ -312,27 +313,51 @@ impl WebContextExt for super::WebContext { ) { let context = &self.os.context; - let download_started_handler = RefCell::new(download_started_handler); + let download_started_handler = Rc::new(RefCell::new(download_started_handler)); let failed = Rc::new(RefCell::new(false)); context.connect_download_started(move |_context, download| { - if let Some(uri) = download.request().and_then(|req| req.uri()) { - let uri = uri.to_string(); - let mut download_location = download - .destination() - .map(PathBuf::from) - .unwrap_or_default(); + let download_started_handler = download_started_handler.clone(); + download.connect_decide_destination(move |download, suggested_filename| { + if let Some(uri) = download.request().and_then(|req| req.uri()) { + let uri = uri.to_string(); + + if let Some(download_started_handler) = download_started_handler.borrow_mut().as_mut() { + let mut download_location = + dirs::download_dir().unwrap_or_else(|| current_dir().unwrap_or_default()); + + let suggested_filename = if uri.starts_with("data:") { + // for data: downloads webkitgtk will suggest to use the data as the filename + // for example `"data:attachment/text," + encodeURI("some text")` will result in `text,some text` + "unknown" + } else { + suggested_filename + }; + + download_location.push(suggested_filename); + + // WebView2 does not overwrite files but appends numbers + let mut counter = 1; + while download_location.exists() { + let (base, ext) = suggested_filename + .split_once('.') + .map(|(base, ext)| (base, format!(".{ext}"))) + .unwrap_or((suggested_filename, "".to_string())); + + download_location.set_file_name(format!("{base} ({counter}){ext}")); + counter += 1; + } - if let Some(download_started_handler) = download_started_handler.borrow_mut().as_mut() { - if download_started_handler(uri, &mut download_location) { - download.connect_response_notify(move |download| { + if download_started_handler(uri, &mut download_location) { download.set_destination(&download_location.to_string_lossy()); - }); - } else { - download.cancel(); + } else { + download.cancel(); + } } } - } + // TODO: check if we may also need `false` + true + }); download.connect_failed({ let failed = failed.clone(); From 4537f28369c2ae1f8d07147c35d242f9347f5fe9 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Tue, 12 Aug 2025 13:32:58 +0200 Subject: [PATCH 2/4] upper case to match macos --- src/webkitgtk/web_context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webkitgtk/web_context.rs b/src/webkitgtk/web_context.rs index 1ed3a260d..187b4dca9 100644 --- a/src/webkitgtk/web_context.rs +++ b/src/webkitgtk/web_context.rs @@ -329,7 +329,7 @@ impl WebContextExt for super::WebContext { let suggested_filename = if uri.starts_with("data:") { // for data: downloads webkitgtk will suggest to use the data as the filename // for example `"data:attachment/text," + encodeURI("some text")` will result in `text,some text` - "unknown" + "Unknown" } else { suggested_filename }; From 7c9634e2da068c66cbb098ca5e6c030ed6df7765 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Tue, 12 Aug 2025 21:35:50 +0200 Subject: [PATCH 3/4] better extension handling --- src/webkitgtk/web_context.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/webkitgtk/web_context.rs b/src/webkitgtk/web_context.rs index 187b4dca9..3d9e0557c 100644 --- a/src/webkitgtk/web_context.rs +++ b/src/webkitgtk/web_context.rs @@ -326,25 +326,32 @@ impl WebContextExt for super::WebContext { let mut download_location = dirs::download_dir().unwrap_or_else(|| current_dir().unwrap_or_default()); - let suggested_filename = if uri.starts_with("data:") { - // for data: downloads webkitgtk will suggest to use the data as the filename - // for example `"data:attachment/text," + encodeURI("some text")` will result in `text,some text` - "Unknown" - } else { - suggested_filename - }; + let (mut suggested_filename, mut ext) = suggested_filename + .split_once('.') + .map(|(base, ext)| (base, format!(".{ext}"))) + .unwrap_or((suggested_filename, "".to_string())); + + // Weird bug but webkitgtk sets the extension to `.jpe` for JPEG files in these cases: + // + // + // + // Other 4 character extensions like avif,webp seem to be set correctly. + if ext == ".jpe" { + ext = ".jpeg".to_string(); + } - download_location.push(suggested_filename); + // for `data:` downloads, webkitgtk will suggest to use the raw data as the filename + // for example `"data:attachment/text,sometext"` will result in `text,sometext` + if uri.starts_with("data:") { + suggested_filename = "Unknown"; + } + + download_location.push(format!("{suggested_filename}{ext}")); // WebView2 does not overwrite files but appends numbers let mut counter = 1; while download_location.exists() { - let (base, ext) = suggested_filename - .split_once('.') - .map(|(base, ext)| (base, format!(".{ext}"))) - .unwrap_or((suggested_filename, "".to_string())); - - download_location.set_file_name(format!("{base} ({counter}){ext}")); + download_location.set_file_name(format!("{suggested_filename} ({counter}){ext}")); counter += 1; } From da58369dfd3767c85e788d4f56b12ecca02e6eea Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Wed, 13 Aug 2025 14:56:39 +0200 Subject: [PATCH 4/4] remove jpe->jepg --- src/webkitgtk/web_context.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/webkitgtk/web_context.rs b/src/webkitgtk/web_context.rs index 3d9e0557c..ed2244847 100644 --- a/src/webkitgtk/web_context.rs +++ b/src/webkitgtk/web_context.rs @@ -331,15 +331,6 @@ impl WebContextExt for super::WebContext { .map(|(base, ext)| (base, format!(".{ext}"))) .unwrap_or((suggested_filename, "".to_string())); - // Weird bug but webkitgtk sets the extension to `.jpe` for JPEG files in these cases: - // - // - // - // Other 4 character extensions like avif,webp seem to be set correctly. - if ext == ".jpe" { - ext = ".jpeg".to_string(); - } - // for `data:` downloads, webkitgtk will suggest to use the raw data as the filename // for example `"data:attachment/text,sometext"` will result in `text,sometext` if uri.starts_with("data:") {