|
@@ -1,3 +1,4 @@
|
|
|
+use std::fs::File;
|
|
|
use std::path::Path;
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
@@ -5,17 +6,24 @@ use clap::ArgMatches;
|
|
|
use failure::{err_msg, Fail};
|
|
|
use ffsend_api::action::params::ParamsDataBuilder;
|
|
|
use ffsend_api::action::upload::Upload as ApiUpload;
|
|
|
+use ffsend_api::config::{UPLOAD_SIZE_MAX, UPLOAD_SIZE_MAX_RECOMMENDED};
|
|
|
use ffsend_api::reqwest::Client;
|
|
|
|
|
|
-use cmd::matcher::{
|
|
|
- Matcher,
|
|
|
- upload::UploadMatcher,
|
|
|
-};
|
|
|
+use cmd::matcher::{Matcher, MainMatcher, UploadMatcher};
|
|
|
use error::ActionError;
|
|
|
use progress::ProgressBar;
|
|
|
-use util::open_url;
|
|
|
+use util::{
|
|
|
+ ErrorHintsBuilder,
|
|
|
+ format_bytes,
|
|
|
+ open_url,
|
|
|
+ print_error,
|
|
|
+ print_error_msg,
|
|
|
+ prompt_yes,
|
|
|
+ quit,
|
|
|
+ quit_error_msg,
|
|
|
+};
|
|
|
#[cfg(feature = "clipboard")]
|
|
|
-use util::{print_error, set_clipboard};
|
|
|
+use util::set_clipboard;
|
|
|
|
|
|
/// A file upload action.
|
|
|
pub struct Upload<'a> {
|
|
@@ -34,12 +42,52 @@ impl<'a> Upload<'a> {
|
|
|
// TODO: create a trait for this method
|
|
|
pub fn invoke(&self) -> Result<(), ActionError> {
|
|
|
// Create the command matchers
|
|
|
+ let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
|
|
|
let matcher_upload = UploadMatcher::with(self.cmd_matches).unwrap();
|
|
|
|
|
|
// Get API parameters
|
|
|
let path = Path::new(matcher_upload.file()).to_path_buf();
|
|
|
let host = matcher_upload.host();
|
|
|
|
|
|
+ // TODO: ensure the file exists and is accessible
|
|
|
+
|
|
|
+ // Get the file size to warn about large files
|
|
|
+ if let Ok(size) = File::open(&path)
|
|
|
+ .and_then(|f| f.metadata())
|
|
|
+ .map(|m| m.len())
|
|
|
+ {
|
|
|
+ if size > UPLOAD_SIZE_MAX && !matcher_main.force() {
|
|
|
+ // The file is too large, show an error and quit
|
|
|
+ quit_error_msg(
|
|
|
+ format!(
|
|
|
+ "The file size is {}, bigger than the maximum allowed of {}",
|
|
|
+ format_bytes(size),
|
|
|
+ format_bytes(UPLOAD_SIZE_MAX),
|
|
|
+ ),
|
|
|
+ ErrorHintsBuilder::default()
|
|
|
+ .force(true)
|
|
|
+ .verbose(false)
|
|
|
+ .build()
|
|
|
+ .unwrap(),
|
|
|
+ );
|
|
|
+ } else if size > UPLOAD_SIZE_MAX_RECOMMENDED && !matcher_main.force() {
|
|
|
+ // The file is larger than the recommended maximum, warn
|
|
|
+ eprintln!(
|
|
|
+ "The file size is {}, bigger than the recommended maximum of {}",
|
|
|
+ format_bytes(size),
|
|
|
+ format_bytes(UPLOAD_SIZE_MAX_RECOMMENDED),
|
|
|
+ );
|
|
|
+
|
|
|
+ // Prompt the user to continue, quit if the user answered no
|
|
|
+ if !prompt_yes("Continue uploading?", Some(true), &matcher_main) {
|
|
|
+ println!("Upload cancelled");
|
|
|
+ quit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ print_error_msg("Failed to check the file size, ignoring");
|
|
|
+ }
|
|
|
+
|
|
|
// Create a reqwest client
|
|
|
let client = Client::new();
|
|
|
|