mirror of
https://github.com/timvisee/ffsend.git
synced 2025-10-03 09:39:15 +02:00
Generate random name when uploading unnamed archives
Fixes https://github.com/timvisee/ffsend/issues/109
This commit is contained in:
parent
5e2b5eb1c2
commit
a565453949
4 changed files with 29 additions and 22 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -510,6 +510,7 @@ dependencies = [
|
||||||
"pbr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"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)",
|
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -118,6 +118,7 @@ pathdiff = "0.1"
|
||||||
pbr = "1"
|
pbr = "1"
|
||||||
prettytable-rs = "0.8"
|
prettytable-rs = "0.8"
|
||||||
qr2term = { version = "0.1", optional = true }
|
qr2term = { version = "0.1", optional = true }
|
||||||
|
rand = "0.7"
|
||||||
regex = "1.3"
|
regex = "1.3"
|
||||||
rpassword = "4"
|
rpassword = "4"
|
||||||
serde = "1"
|
serde = "1"
|
||||||
|
|
|
@ -36,7 +36,7 @@ use crate::urlshorten;
|
||||||
use crate::util::set_clipboard;
|
use crate::util::set_clipboard;
|
||||||
use crate::util::{
|
use crate::util::{
|
||||||
format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit, quit_error_msg,
|
format_bytes, open_url, print_error, print_error_msg, prompt_yes, quit, quit_error_msg,
|
||||||
ErrorHintsBuilder,
|
rand_alphanum_string, ErrorHintsBuilder,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A file upload action.
|
/// A file upload action.
|
||||||
|
@ -141,19 +141,8 @@ impl<'a> Upload<'a> {
|
||||||
|
|
||||||
// Select the file name to use if not set
|
// Select the file name to use if not set
|
||||||
if file_name.is_none() {
|
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
|
// Derive name from given file
|
||||||
|
if paths.len() == 1 {
|
||||||
file_name = Some(
|
file_name = Some(
|
||||||
path.canonicalize()
|
path.canonicalize()
|
||||||
.map_err(|err| ArchiveError::FileName(Some(err)))?
|
.map_err(|err| ArchiveError::FileName(Some(err)))?
|
||||||
|
@ -163,6 +152,10 @@ impl<'a> Upload<'a> {
|
||||||
.map(|s| s.to_owned())
|
.map(|s| s.to_owned())
|
||||||
.ok_or(ArchiveError::FileName(None))?,
|
.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
|
// Get the current working directory, including working directory as highest possible root, canonicalize it
|
||||||
|
|
12
src/util.rs
12
src/util.rs
|
@ -17,6 +17,7 @@ use std::fmt::{Debug, Display};
|
||||||
#[cfg(feature = "clipboard-bin")]
|
#[cfg(feature = "clipboard-bin")]
|
||||||
use std::io::ErrorKind as IoErrorKind;
|
use std::io::ErrorKind as IoErrorKind;
|
||||||
use std::io::{stderr, stdin, Error as IoError, Write};
|
use std::io::{stderr, stdin, Error as IoError, Write};
|
||||||
|
use std::iter;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::{exit, ExitStatus};
|
use std::process::{exit, ExitStatus};
|
||||||
|
@ -39,6 +40,8 @@ use ffsend_api::{
|
||||||
reqwest,
|
reqwest,
|
||||||
url::Url,
|
url::Url,
|
||||||
};
|
};
|
||||||
|
use rand::distributions::Alphanumeric;
|
||||||
|
use rand::{thread_rng, Rng};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rpassword::prompt_password_stderr;
|
use rpassword::prompt_password_stderr;
|
||||||
#[cfg(feature = "clipboard-bin")]
|
#[cfg(feature = "clipboard-bin")]
|
||||||
|
@ -1120,3 +1123,12 @@ impl From<ResponseError> for FollowError {
|
||||||
FollowError::Response(err)
|
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue