Skip to content

Commit af1cc13

Browse files
authored
fix(macos/ios): prepend the Downloads dir to the suggested download path (#1599)
* fix(macos/ios): prepend the Downloads dir to the suggested download path * typo * numerate files
1 parent eb562ca commit af1cc13

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

.changes/linux-download-handler-destination.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"wry": "patch"
33
---
44

5-
On Linux, the `download_started_handler` will now get the correct suggested destination path.
5+
On Linux, macOS and iOS, the `download_started_handler` will now get the correct suggested destination path.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ features = [
9494

9595
[target.'cfg(target_vendor = "apple")'.dependencies]
9696
url = "2.5"
97+
dirs = "6"
9798
block2 = "0.6"
9899
objc2 = { version = "0.6", features = [
99100
"exception",

src/webkitgtk/web_context.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ impl WebContextExt for super::WebContext {
323323
let uri = uri.to_string();
324324

325325
if let Some(download_started_handler) = download_started_handler.borrow_mut().as_mut() {
326-
let mut download_location =
326+
let mut download_destination =
327327
dirs::download_dir().unwrap_or_else(|| current_dir().unwrap_or_default());
328328

329-
let (mut suggested_filename, mut ext) = suggested_filename
329+
let (mut suggested_filename, ext) = suggested_filename
330330
.split_once('.')
331331
.map(|(base, ext)| (base, format!(".{ext}")))
332332
.unwrap_or((suggested_filename, "".to_string()));
@@ -337,17 +337,17 @@ impl WebContextExt for super::WebContext {
337337
suggested_filename = "Unknown";
338338
}
339339

340-
download_location.push(format!("{suggested_filename}{ext}"));
340+
download_destination.push(format!("{suggested_filename}{ext}"));
341341

342342
// WebView2 does not overwrite files but appends numbers
343343
let mut counter = 1;
344-
while download_location.exists() {
345-
download_location.set_file_name(format!("{suggested_filename} ({counter}){ext}"));
344+
while download_destination.exists() {
345+
download_destination.set_file_name(format!("{suggested_filename} ({counter}){ext}"));
346346
counter += 1;
347347
}
348348

349-
if download_started_handler(uri, &mut download_location) {
350-
download.set_destination(&download_location.to_string_lossy());
349+
if download_started_handler(uri, &mut download_destination) {
350+
download.set_destination(&download_destination.to_string_lossy());
351351
} else {
352352
download.cancel();
353353
}

src/wkwebview/class/wry_download_delegate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ define_class!(
3232
&self,
3333
download: &WKDownload,
3434
response: &NSURLResponse,
35-
suggested_path: &NSString,
35+
suggested_filename: &NSString,
3636
handler: &block2::Block<dyn Fn(*const NSURL)>,
3737
) {
38-
download_policy(self, download, response, suggested_path, handler);
38+
download_policy(self, download, response, suggested_filename, handler);
3939
}
4040

4141
#[unsafe(method(downloadDidFinish:))]

src/wkwebview/download.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{path::PathBuf, ptr::null_mut};
1+
use std::{env::current_dir, ptr::null_mut};
22

33
use objc2::{rc::Retained, runtime::ProtocolObject, DeclaredClass};
44
use objc2_foundation::{NSData, NSError, NSString, NSURLResponse, NSURL};
@@ -47,20 +47,36 @@ pub(crate) fn download_policy(
4747
this: &WryDownloadDelegate,
4848
download: &WKDownload,
4949
_response: &NSURLResponse,
50-
suggested_path: &NSString,
50+
suggested_filename: &NSString,
5151
completion_handler: &block2::Block<dyn Fn(*const NSURL)>,
5252
) {
5353
unsafe {
5454
let request = download.originalRequest().unwrap();
5555
let url = request.URL().unwrap().absoluteString().unwrap();
56-
let mut path = PathBuf::from(suggested_path.to_string());
56+
let suggested_filename = suggested_filename.to_string();
57+
let mut download_destination =
58+
dirs::download_dir().unwrap_or_else(|| current_dir().unwrap_or_default());
59+
60+
download_destination.push(&suggested_filename);
61+
62+
let (suggested_filename, ext) = suggested_filename
63+
.split_once('.')
64+
.map(|(base, ext)| (base, format!(".{ext}")))
65+
.unwrap_or((&suggested_filename, "".to_string()));
66+
67+
// WebView2 does not overwrite files but appends numbers
68+
let mut counter = 1;
69+
while download_destination.exists() {
70+
download_destination.set_file_name(format!("{suggested_filename} ({counter}){ext}"));
71+
counter += 1;
72+
}
5773

5874
let started_fn = &this.ivars().started;
5975
if let Some(started_fn) = started_fn {
6076
let mut started_fn = started_fn.borrow_mut();
61-
match started_fn(url.to_string().to_string(), &mut path) {
77+
match started_fn(url.to_string().to_string(), &mut download_destination) {
6278
true => {
63-
let path = NSString::from_str(&path.display().to_string());
79+
let path = NSString::from_str(&download_destination.display().to_string());
6480
let ns_url = NSURL::fileURLWithPath_isDirectory(&path, false);
6581
(*completion_handler).call((Retained::as_ptr(&ns_url),))
6682
}

0 commit comments

Comments
 (0)