Skip to content

Commit 6649244

Browse files
relrelbHerschel
authored andcommitted
desktop: De-duplicate file reading code
Unify the sandboxed and non-sandboxed cases.
1 parent ad42b99 commit 6649244

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

desktop/src/navigator.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,41 +120,31 @@ impl NavigatorBackend for ExternalNavigatorBackend {
120120
let client = self.client.clone();
121121

122122
match processed_url.scheme() {
123-
#[cfg(not(feature = "sandbox"))]
124123
"file" => Box::pin(async move {
125124
let path = processed_url.to_file_path().unwrap_or_default();
126125

127-
fs::read(path).map_err(|e| Error::FetchError(e.to_string()))
128-
}),
129-
130-
#[cfg(feature = "sandbox")]
131-
"file" => Box::pin(async move {
132-
use rfd::{FileDialog, MessageButtons, MessageDialog, MessageLevel};
133-
use std::io::ErrorKind;
134-
135-
let path = processed_url.to_file_path().unwrap_or_default();
136-
137-
fs::read(path.clone()).or_else(|e| {
138-
if matches!(e.kind(), ErrorKind::PermissionDenied) {
139-
let mut display_dir = path.clone();
140-
141-
display_dir.pop();
126+
fs::read(&path).or_else(|e| {
127+
if cfg!(feature = "sandbox") {
128+
use rfd::{FileDialog, MessageButtons, MessageDialog, MessageLevel};
129+
use std::io::ErrorKind;
142130

143-
let attempt_sandbox_open = MessageDialog::new()
144-
.set_level(MessageLevel::Warning)
145-
.set_description(&format!("The current movie is attempting to read files stored in {}.\n\nTo allow it to do so, click Yes, and then Open to grant read access to that directory.\n\nOtherwise, click No to deny access.", display_dir.into_os_string().to_string_lossy()))
146-
.set_buttons(MessageButtons::YesNo)
147-
.show();
131+
if e.kind() == ErrorKind::PermissionDenied {
132+
let attempt_sandbox_open = MessageDialog::new()
133+
.set_level(MessageLevel::Warning)
134+
.set_description(&format!("The current movie is attempting to read files stored in {}.\n\nTo allow it to do so, click Yes, and then Open to grant read access to that directory.\n\nOtherwise, click No to deny access.", path.parent().unwrap().to_string_lossy()))
135+
.set_buttons(MessageButtons::YesNo)
136+
.show();
148137

149-
if attempt_sandbox_open {
150-
FileDialog::new().set_directory(path.clone()).pick_folder();
138+
if attempt_sandbox_open {
139+
FileDialog::new().set_directory(&path).pick_folder();
151140

152-
return fs::read(path).map_err(|e| Error::FetchError(e.to_string()));
141+
return fs::read(&path);
142+
}
153143
}
154144
}
155145

156-
Err(Error::FetchError(e.to_string()))
157-
})
146+
Err(e)
147+
}).map_err(|e| Error::FetchError(e.to_string()))
158148
}),
159149
_ => Box::pin(async move {
160150
let client =

0 commit comments

Comments
 (0)