Generate random name when uploading unnamed archives

Fixes https://github.com/timvisee/ffsend/issues/109
This commit is contained in:
timvisee 2020-03-31 02:12:13 +02:00
parent 5e2b5eb1c2
commit a565453949
No known key found for this signature in database
GPG key ID: B8DB720BC383E172
4 changed files with 29 additions and 22 deletions

1
Cargo.lock generated
View file

@ -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)",

View file

@ -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"

View file

@ -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

View file

@ -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<ResponseError> 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()
}