Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/linux-download-handler-destination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Linux, the `download_started_handler` will now get the correct suggested destination path.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
53 changes: 39 additions & 14 deletions src/webkitgtk/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
borrow::Cow,
cell::RefCell,
collections::VecDeque,
env::current_dir,
path::{Path, PathBuf},
rc::Rc,
sync::{
Expand Down Expand Up @@ -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"
Comment thread
FabianLars marked this conversation as resolved.
Resolved
} 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();
Expand Down