diff --git a/api/src/action/upload.rs b/api/src/action/upload.rs index 4aca5e6..6e8fc6b 100644 --- a/api/src/action/upload.rs +++ b/api/src/action/upload.rs @@ -229,7 +229,8 @@ impl UploadResponse { SendFile::new_now( self.id, host, - self.url, + Url::parse(&self.url) + .expect("upload response URL parse error"), key.secret().to_vec(), self.owner, ) diff --git a/api/src/file/file.rs b/api/src/file/file.rs index a15bb63..3153fd3 100644 --- a/api/src/file/file.rs +++ b/api/src/file/file.rs @@ -21,7 +21,7 @@ pub struct File { host: Url, /// The file URL that was provided by the server. - url: String, + url: Url, /// The secret key that is required to download the file. secret: Vec, @@ -36,7 +36,7 @@ impl File { id: String, time: DateTime, host: Url, - url: String, + url: Url, secret: Vec, owner_key: String, ) -> Self { @@ -54,7 +54,7 @@ impl File { pub fn new_now( id: String, host: Url, - url: String, + url: Url, secret: Vec, owner_key: String, ) -> Self { @@ -79,7 +79,11 @@ impl File { } /// Get the download URL of the file, with the secret key included. - pub fn download_url(&self) -> String { - format!("{}#{}", self.url, self.secret()) + pub fn download_url(&self) -> Url { + // Get the download URL, and add the secret fragment + let mut url = self.url.clone(); + url.set_fragment(Some(&self.secret())); + + url } } diff --git a/cli/src/action/upload.rs b/cli/src/action/upload.rs index de9599f..2b4270f 100644 --- a/cli/src/action/upload.rs +++ b/cli/src/action/upload.rs @@ -2,9 +2,9 @@ use std::path::Path; use ffsend_api::action::upload::Upload as ApiUpload; use ffsend_api::reqwest::Client; -use open; use cmd::cmd_upload::CmdUpload; +use util::open_url; /// A file upload action. pub struct Upload<'a> { @@ -22,7 +22,7 @@ impl<'a> Upload<'a> { /// Invoke the upload action. // TODO: create a trait for this method pub fn invoke(&self) { - // Get API action parameters + // Get API parameters let path = Path::new(self.cmd.file()).to_path_buf(); let host = self.cmd.host(); @@ -37,6 +37,6 @@ impl<'a> Upload<'a> { let url = file.download_url(); println!("Download URL: {}", url); // TODO: do not expect, but return an error - open::that(url).expect("failed to open URL"); + open_url(url).expect("failed to open URL"); } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 96e7ed2..d99dc75 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,5 +1,4 @@ extern crate ffsend_api; -extern crate open; mod action; mod app; diff --git a/cli/src/util.rs b/cli/src/util.rs index c98b15f..fcbcd09 100644 --- a/cli/src/util.rs +++ b/cli/src/util.rs @@ -1,4 +1,9 @@ -use std::process::exit; +extern crate open; + +use std::io::Error as IoError; +use std::process::{exit, ExitStatus}; + +use ffsend_api::url::Url; /// Quit the application with an error code, /// and print the given error message. @@ -9,3 +14,15 @@ pub fn quit_error>(err: S) -> ! { // Quit exit(1); } + +/// Open the given URL in the users default browser. +/// The browsers exit statis is returned. +pub fn open_url(url: Url) -> Result { + open_path(url.as_str()) +} + +/// Open the given path or URL using the program configured on the system. +/// The program exit statis is returned. +pub fn open_path(path: &str) -> Result { + open::that(path) +}