feat(paste): support using random file names

This commit is contained in:
orhun 2021-07-23 23:53:41 +03:00
parent cc7e295e88
commit fc425067f7
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
5 changed files with 34 additions and 0 deletions

1
Cargo.lock generated
View file

@ -1177,6 +1177,7 @@ dependencies = [
"futures-util",
"log",
"petname",
"rand 0.8.4",
"serde 1.0.126",
]

View file

@ -15,6 +15,7 @@ serde = "1.0.126"
futures-util = "0.3.15"
config = "0.11.0"
petname = "1.1.0"
rand = "0.8.4"
[dependencies.byte-unit]
version = "4.0.12"

View file

@ -7,4 +7,5 @@ upload_path="./upload"
[paste]
pet_names = true
random = { enabled = false, length = 8 }
default_extension = "txt"

View file

@ -31,10 +31,21 @@ pub struct ServerConfig {
pub struct PasteConfig {
/// Use pet names instead of original file names.
pub pet_names: bool,
/// Random string configuration.
pub random: RandomConfig,
/// Default file extension.
pub default_extension: String,
}
/// Random string configuration.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RandomConfig {
/// Use random strings instead of original file names.
pub enabled: bool,
/// Length of the random string to generate.
pub length: Option<usize>,
}
impl Config {
/// Parses the config file and returns the values.
pub fn parse(file_name: &str) -> Result<Config, ConfigError> {

View file

@ -1,4 +1,5 @@
use crate::config::Config;
use rand::{distributions::Alphanumeric, Rng};
use std::fs::File;
use std::io::{Result as IoResult, Write};
@ -7,9 +8,11 @@ use std::io::{Result as IoResult, Write};
/// - If `file_name` does not have an extension, it is replaced with [`default_extension`].
/// - If `file_name` is "-", it is replaced with "stdin".
/// - If [`pet_names`] is `true`, `file_name` is replaced with a pet name.
/// - If [`random.enabled`] is `true`, `file_name` is replaced with a random string.
///
/// [`default_extension`]: crate::config::PasteConfig::default_extension
/// [`pet_names`]: crate::config::PasteConfig::pet_names
/// [`random.enabled`]: crate::config::RandomConfig::enabled
pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> {
if file_name == "-" {
file_name = "stdin";
@ -20,11 +23,28 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<Stri
if config.paste.pet_names {
path.set_file_name(petname::petname(2, "-"));
path.set_extension(extension);
} else if config.paste.random.enabled {
path.set_file_name(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(config.paste.random.length.unwrap_or(8))
.map(char::from)
.collect::<String>(),
);
path.set_extension(extension);
}
}
None => {
if config.paste.pet_names {
path.set_file_name(petname::petname(2, "-"));
} else if config.paste.random.enabled {
path.set_file_name(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(config.paste.random.length.unwrap_or(8))
.map(char::from)
.collect::<String>(),
);
}
path.set_extension(&config.paste.default_extension);
}