feat(paste): make pet names configurable

This commit is contained in:
orhun 2021-07-24 00:01:16 +03:00
parent fc425067f7
commit f67e611781
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 29 additions and 12 deletions

View file

@ -6,6 +6,6 @@ upload_path="./upload"
#auth_token="" # OOPS_SERVER__AUTH_TOKEN= #auth_token="" # OOPS_SERVER__AUTH_TOKEN=
[paste] [paste]
pet_names = true pet_names = { enabled = true, words = 2, separator = "-" }
random = { enabled = false, length = 8 } random = { enabled = false, length = 8 }
default_extension = "txt" default_extension = "txt"

View file

@ -29,21 +29,32 @@ pub struct ServerConfig {
/// Paste configuration. /// Paste configuration.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PasteConfig { pub struct PasteConfig {
/// Use pet names instead of original file names. /// Pet names configuration.
pub pet_names: bool, pub pet_names: PetNamesConfig,
/// Random string configuration. /// Random string configuration.
pub random: RandomConfig, pub random: RandomConfig,
/// Default file extension. /// Default file extension.
pub default_extension: String, pub default_extension: String,
} }
/// Pet names configuration.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PetNamesConfig {
/// Use pet names instead of original file names.
pub enabled: bool,
/// Count of words that pet name will include.
pub words: u8,
/// Separator between the words.
pub separator: String,
}
/// Random string configuration. /// Random string configuration.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct RandomConfig { pub struct RandomConfig {
/// Use random strings instead of original file names. /// Use random strings instead of original file names.
pub enabled: bool, pub enabled: bool,
/// Length of the random string to generate. /// Length of the random string to generate.
pub length: Option<usize>, pub length: usize,
} }
impl Config { impl Config {

View file

@ -7,11 +7,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` does not have an extension, it is replaced with [`default_extension`].
/// - If `file_name` is "-", it is replaced with "stdin". /// - If `file_name` is "-", it is replaced with "stdin".
/// - If [`pet_names`] is `true`, `file_name` is replaced with a pet name. /// - If [`pet_names.enabled`] is `true`, `file_name` is replaced with a pet name.
/// - If [`random.enabled`] is `true`, `file_name` is replaced with a random string. /// - If [`random.enabled`] is `true`, `file_name` is replaced with a random string.
/// ///
/// [`default_extension`]: crate::config::PasteConfig::default_extension /// [`default_extension`]: crate::config::PasteConfig::default_extension
/// [`pet_names`]: crate::config::PasteConfig::pet_names /// [`pet_names.enabled`]: crate::config::PetNamesConfig::enabled
/// [`random.enabled`]: crate::config::RandomConfig::enabled /// [`random.enabled`]: crate::config::RandomConfig::enabled
pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> { pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> {
if file_name == "-" { if file_name == "-" {
@ -20,14 +20,17 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<Stri
let mut path = config.server.upload_path.join(file_name); let mut path = config.server.upload_path.join(file_name);
match path.clone().extension() { match path.clone().extension() {
Some(extension) => { Some(extension) => {
if config.paste.pet_names { if config.paste.pet_names.enabled {
path.set_file_name(petname::petname(2, "-")); path.set_file_name(petname::petname(
config.paste.pet_names.words,
&config.paste.pet_names.separator,
));
path.set_extension(extension); path.set_extension(extension);
} else if config.paste.random.enabled { } else if config.paste.random.enabled {
path.set_file_name( path.set_file_name(
rand::thread_rng() rand::thread_rng()
.sample_iter(&Alphanumeric) .sample_iter(&Alphanumeric)
.take(config.paste.random.length.unwrap_or(8)) .take(config.paste.random.length)
.map(char::from) .map(char::from)
.collect::<String>(), .collect::<String>(),
); );
@ -35,13 +38,16 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<Stri
} }
} }
None => { None => {
if config.paste.pet_names { if config.paste.pet_names.enabled {
path.set_file_name(petname::petname(2, "-")); path.set_file_name(petname::petname(
config.paste.pet_names.words,
&config.paste.pet_names.separator,
));
} else if config.paste.random.enabled { } else if config.paste.random.enabled {
path.set_file_name( path.set_file_name(
rand::thread_rng() rand::thread_rng()
.sample_iter(&Alphanumeric) .sample_iter(&Alphanumeric)
.take(config.paste.random.length.unwrap_or(8)) .take(config.paste.random.length)
.map(char::from) .map(char::from)
.collect::<String>(), .collect::<String>(),
); );