Abstract URL opening in utility, use Url instances

This commit is contained in:
timvisee 2018-03-18 21:01:54 +01:00
parent c2611049df
commit a4aa1b93e5
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
5 changed files with 32 additions and 11 deletions

View file

@ -229,7 +229,8 @@ impl UploadResponse {
SendFile::new_now( SendFile::new_now(
self.id, self.id,
host, host,
self.url, Url::parse(&self.url)
.expect("upload response URL parse error"),
key.secret().to_vec(), key.secret().to_vec(),
self.owner, self.owner,
) )

View file

@ -21,7 +21,7 @@ pub struct File {
host: Url, host: Url,
/// The file URL that was provided by the server. /// The file URL that was provided by the server.
url: String, url: Url,
/// The secret key that is required to download the file. /// The secret key that is required to download the file.
secret: Vec<u8>, secret: Vec<u8>,
@ -36,7 +36,7 @@ impl File {
id: String, id: String,
time: DateTime<Utc>, time: DateTime<Utc>,
host: Url, host: Url,
url: String, url: Url,
secret: Vec<u8>, secret: Vec<u8>,
owner_key: String, owner_key: String,
) -> Self { ) -> Self {
@ -54,7 +54,7 @@ impl File {
pub fn new_now( pub fn new_now(
id: String, id: String,
host: Url, host: Url,
url: String, url: Url,
secret: Vec<u8>, secret: Vec<u8>,
owner_key: String, owner_key: String,
) -> Self { ) -> Self {
@ -79,7 +79,11 @@ impl File {
} }
/// Get the download URL of the file, with the secret key included. /// Get the download URL of the file, with the secret key included.
pub fn download_url(&self) -> String { pub fn download_url(&self) -> Url {
format!("{}#{}", self.url, self.secret()) // Get the download URL, and add the secret fragment
let mut url = self.url.clone();
url.set_fragment(Some(&self.secret()));
url
} }
} }

View file

@ -2,9 +2,9 @@ use std::path::Path;
use ffsend_api::action::upload::Upload as ApiUpload; use ffsend_api::action::upload::Upload as ApiUpload;
use ffsend_api::reqwest::Client; use ffsend_api::reqwest::Client;
use open;
use cmd::cmd_upload::CmdUpload; use cmd::cmd_upload::CmdUpload;
use util::open_url;
/// A file upload action. /// A file upload action.
pub struct Upload<'a> { pub struct Upload<'a> {
@ -22,7 +22,7 @@ impl<'a> Upload<'a> {
/// Invoke the upload action. /// Invoke the upload action.
// TODO: create a trait for this method // TODO: create a trait for this method
pub fn invoke(&self) { pub fn invoke(&self) {
// Get API action parameters // Get API parameters
let path = Path::new(self.cmd.file()).to_path_buf(); let path = Path::new(self.cmd.file()).to_path_buf();
let host = self.cmd.host(); let host = self.cmd.host();
@ -37,6 +37,6 @@ impl<'a> Upload<'a> {
let url = file.download_url(); let url = file.download_url();
println!("Download URL: {}", url); println!("Download URL: {}", url);
// TODO: do not expect, but return an error // TODO: do not expect, but return an error
open::that(url).expect("failed to open URL"); open_url(url).expect("failed to open URL");
} }
} }

View file

@ -1,5 +1,4 @@
extern crate ffsend_api; extern crate ffsend_api;
extern crate open;
mod action; mod action;
mod app; mod app;

View file

@ -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, /// Quit the application with an error code,
/// and print the given error message. /// and print the given error message.
@ -9,3 +14,15 @@ pub fn quit_error<S: AsRef<str>>(err: S) -> ! {
// Quit // Quit
exit(1); exit(1);
} }
/// Open the given URL in the users default browser.
/// The browsers exit statis is returned.
pub fn open_url(url: Url) -> Result<ExitStatus, IoError> {
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<ExitStatus, IoError> {
open::that(path)
}