From a565453949fad49d777d5dade3428461c8e987ba Mon Sep 17 00:00:00 2001 From: timvisee Date: Tue, 31 Mar 2020 02:12:13 +0200 Subject: [PATCH] Generate random name when uploading unnamed archives Fixes https://github.com/timvisee/ffsend/issues/109 --- Cargo.lock | 1 + Cargo.toml | 1 + src/action/upload.rs | 37 +++++++++++++++---------------------- src/util.rs | 12 ++++++++++++ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82d3c67..b56bbda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,6 +510,7 @@ dependencies = [ "pbr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "qr2term 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 4.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 69c9b81..5a7597b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,6 +118,7 @@ pathdiff = "0.1" pbr = "1" prettytable-rs = "0.8" qr2term = { version = "0.1", optional = true } +rand = "0.7" regex = "1.3" rpassword = "4" serde = "1" diff --git a/src/action/upload.rs b/src/action/upload.rs index df76221..ff61826 100644 --- a/src/action/upload.rs +++ b/src/action/upload.rs @@ -36,7 +36,7 @@ use crate::urlshorten; use crate::util::set_clipboard; use crate::util::{ format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit, quit_error_msg, - ErrorHintsBuilder, + rand_alphanum_string, ErrorHintsBuilder, }; /// A file upload action. @@ -141,28 +141,21 @@ impl<'a> Upload<'a> { // Select the file name to use if not set if file_name.is_none() { - // Require user to specify name if multiple files are given - if paths.len() > 1 { - quit_error_msg( - "you must specify a file name for the archive", - ErrorHintsBuilder::default() - .name(true) - .verbose(false) - .build() - .unwrap(), - ); - } - // Derive name from given file - file_name = Some( - path.canonicalize() - .map_err(|err| ArchiveError::FileName(Some(err)))? - .file_name() - .ok_or(ArchiveError::FileName(None))? - .to_str() - .map(|s| s.to_owned()) - .ok_or(ArchiveError::FileName(None))?, - ); + if paths.len() == 1 { + file_name = Some( + path.canonicalize() + .map_err(|err| ArchiveError::FileName(Some(err)))? + .file_name() + .ok_or(ArchiveError::FileName(None))? + .to_str() + .map(|s| s.to_owned()) + .ok_or(ArchiveError::FileName(None))?, + ); + } else { + // Unable to derive file name from paths, generate random + file_name = Some(format!("ffsend-archive-{}", rand_alphanum_string(8))); + } } // Get the current working directory, including working directory as highest possible root, canonicalize it diff --git a/src/util.rs b/src/util.rs index f4a02ab..41fef9e 100644 --- a/src/util.rs +++ b/src/util.rs @@ -17,6 +17,7 @@ use std::fmt::{Debug, Display}; #[cfg(feature = "clipboard-bin")] use std::io::ErrorKind as IoErrorKind; use std::io::{stderr, stdin, Error as IoError, Write}; +use std::iter; use std::path::Path; use std::path::PathBuf; use std::process::{exit, ExitStatus}; @@ -39,6 +40,8 @@ use ffsend_api::{ reqwest, url::Url, }; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; use regex::Regex; use rpassword::prompt_password_stderr; #[cfg(feature = "clipboard-bin")] @@ -1120,3 +1123,12 @@ impl From for FollowError { FollowError::Response(err) } } + +/// Generate a random alphanumeric string with the given length. +pub fn rand_alphanum_string(len: usize) -> String { + let mut rng = thread_rng(); + iter::repeat(()) + .map(|()| rng.sample(Alphanumeric)) + .take(len) + .collect() +}