Implement upload logic from API, use pathbuf

This commit is contained in:
timvisee 2018-03-09 00:37:02 +01:00
parent a66b9dd67f
commit 061e6e0658
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
3 changed files with 24 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use std::path::Path; use std::path::{Path, PathBuf};
use mime_guess::{get_mime_type, Mime}; use mime_guess::{get_mime_type, Mime};
use openssl::symm::encrypt_aead; use openssl::symm::encrypt_aead;
@ -27,12 +27,12 @@ pub struct Upload {
host: Url, host: Url,
/// The file to upload. /// The file to upload.
path: Box<Path>, path: PathBuf,
} }
impl Upload { impl Upload {
/// Construct a new upload action. /// Construct a new upload action.
pub fn new(host: Url, path: Box<Path>) -> Self { pub fn new(host: Url, path: PathBuf) -> Self {
Self { Self {
host, host,
path, path,
@ -98,7 +98,7 @@ impl Upload {
-> Result<(BufReader<EncryptedFileReaderTagged>, u64)> -> Result<(BufReader<EncryptedFileReaderTagged>, u64)>
{ {
// Open the file // Open the file
let file = match File::open(&self.path) { let file = match File::open(self.path.as_path()) {
Ok(file) => file, Ok(file) => file,
Err(_) => return Err(UploadError::FileError), Err(_) => return Err(UploadError::FileError),
}; };
@ -180,6 +180,7 @@ impl Upload {
} }
/// Errors that may occur in the upload action. /// Errors that may occur in the upload action.
#[derive(Debug)]
pub enum UploadError { pub enum UploadError {
/// The given file is not not an existing file. /// The given file is not not an existing file.
/// Maybe it is a directory, or maybe it doesn't exist. /// Maybe it is a directory, or maybe it doesn't exist.

View file

@ -1,6 +1,6 @@
extern crate mime_guess; extern crate mime_guess;
extern crate openssl; extern crate openssl;
extern crate reqwest; pub extern crate reqwest;
pub extern crate url; pub extern crate url;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;

View file

@ -1,12 +1,17 @@
extern crate ffsend_api; extern crate ffsend_api;
extern crate open;
mod action; mod action;
mod app; mod app;
mod cmd; mod cmd;
mod util; mod util;
use std::path::Path;
use cmd::Handler; use cmd::Handler;
use cmd::cmd_upload::CmdUpload; use cmd::cmd_upload::CmdUpload;
use ffsend_api::action::upload::Upload;
use ffsend_api::reqwest::Client;
/// Application entrypoint. /// Application entrypoint.
fn main() { fn main() {
@ -36,9 +41,18 @@ fn invoke_action(handler: &Handler) {
/// The upload action. /// The upload action.
fn action_upload(cmd_upload: &CmdUpload) { fn action_upload(cmd_upload: &CmdUpload) {
// // Get the path and host // // Get the path and host
// let path = Path::new(cmd_upload.file()); let path = Path::new(cmd_upload.file()).to_path_buf();
// let host = cmd_upload.host(); let host = cmd_upload.host();
// // Open the URL in the browser // Create a reqwest client
// open::that(url).expect("failed to open URL"); let client = Client::new();
// Create an upload action
let upload = Upload::new(host, path);
let file = upload.invoke(&client).unwrap();
// Open the URL in the browser
let url = file.download_url();
println!("Download URL: {}", url);
open::that(url).expect("failed to open URL");
} }