prevent downloading internal pages, better error message

master
chris west 1 year ago
parent b29ea2cee4
commit 38dff71ac0

@ -112,7 +112,7 @@ fn clean_response(res: &mut String) {
}) })
} }
/// Downloads a binary to disk to a provided file name. /// Downloads menu or text to disk as `filename`.
/// Allows canceling with Ctrl-c, but it's /// Allows canceling with Ctrl-c, but it's
/// kind of hacky - needs the UI receiver passed in. /// kind of hacky - needs the UI receiver passed in.
/// Returns a tuple of: /// Returns a tuple of:
@ -134,7 +134,7 @@ pub fn download_url_with_filename(
.create_new(true) .create_new(true)
.append(true) .append(true)
.open(&path) .open(&path)
.map_err(|e| error!("`open` error: {}", e))?; .map_err(|e| error!("{}", e))?;
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let mut bytes = 0; let mut bytes = 0;

@ -260,7 +260,6 @@ impl UI {
}) })
} }
/// Download a binary file. Used by `open()` internally. /// Download a binary file. Used by `open()` internally.
fn download(&mut self, url: &str) -> Result<()> { fn download(&mut self, url: &str) -> Result<()> {
let url = url.to_string(); let url = url.to_string();
@ -686,21 +685,24 @@ impl UI {
'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?, 'b' => self.open("Bookmarks", "gopher://phetch/1/bookmarks")?,
'd' => { 'd' => {
let url = match self.views.get(self.focused) { let url = match self.views.get(self.focused) {
Some(view)=> String::from(view.url()), Some(view) => String::from(view.url()),
None => {return Err(error!("Could not get url from view"));}, None => return Err(error!("Could not get URL from view")),
}; };
let url = url.as_str();
if url.starts_with("gopher://phetch/") {
return Err(error!("Can't download internal phetch pages."));
}
let u = gopher::parse_url(&url); let u = gopher::parse_url(&url);
let default_filename = u let default_filename = u.sel.split_terminator('/').rev().next().unwrap_or("");
.sel if let Some(filename) = self.prompt("Save to disk as: ", default_filename) {
.split_terminator('/') if filename.trim().is_empty() {
.rev() return Err(error!("Please provide a filename."));
.next() }
.unwrap_or(""); match self.download_file_with_filename(url, String::from(filename)) {
if let Some(filename) = self.prompt("Provide a filepath: ", default_filename){
match self.download_file_with_filename(url.as_str(), String::from(filename)){
Ok(()) => (), Ok(()) => (),
Err(e) => return Err(error!("Save failed: {}", e)), Err(e) => return Err(error!("Download failed: {}", e)),
} }
} }
} }

Loading…
Cancel
Save