mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 11:59:48 -05:00
feat(paste): make pet names configurable
This commit is contained in:
parent
fc425067f7
commit
f67e611781
3 changed files with 29 additions and 12 deletions
|
@ -6,6 +6,6 @@ upload_path="./upload"
|
|||
#auth_token="" # OOPS_SERVER__AUTH_TOKEN=
|
||||
|
||||
[paste]
|
||||
pet_names = true
|
||||
pet_names = { enabled = true, words = 2, separator = "-" }
|
||||
random = { enabled = false, length = 8 }
|
||||
default_extension = "txt"
|
||||
|
|
|
@ -29,21 +29,32 @@ pub struct ServerConfig {
|
|||
/// Paste configuration.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PasteConfig {
|
||||
/// Use pet names instead of original file names.
|
||||
pub pet_names: bool,
|
||||
/// Pet names configuration.
|
||||
pub pet_names: PetNamesConfig,
|
||||
/// Random string configuration.
|
||||
pub random: RandomConfig,
|
||||
/// Default file extension.
|
||||
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.
|
||||
#[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>,
|
||||
pub length: usize,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
|
22
src/file.rs
22
src/file.rs
|
@ -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` 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.
|
||||
///
|
||||
/// [`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
|
||||
pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> {
|
||||
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);
|
||||
match path.clone().extension() {
|
||||
Some(extension) => {
|
||||
if config.paste.pet_names {
|
||||
path.set_file_name(petname::petname(2, "-"));
|
||||
if config.paste.pet_names.enabled {
|
||||
path.set_file_name(petname::petname(
|
||||
config.paste.pet_names.words,
|
||||
&config.paste.pet_names.separator,
|
||||
));
|
||||
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))
|
||||
.take(config.paste.random.length)
|
||||
.map(char::from)
|
||||
.collect::<String>(),
|
||||
);
|
||||
|
@ -35,13 +38,16 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<Stri
|
|||
}
|
||||
}
|
||||
None => {
|
||||
if config.paste.pet_names {
|
||||
path.set_file_name(petname::petname(2, "-"));
|
||||
if config.paste.pet_names.enabled {
|
||||
path.set_file_name(petname::petname(
|
||||
config.paste.pet_names.words,
|
||||
&config.paste.pet_names.separator,
|
||||
));
|
||||
} else if config.paste.random.enabled {
|
||||
path.set_file_name(
|
||||
rand::thread_rng()
|
||||
.sample_iter(&Alphanumeric)
|
||||
.take(config.paste.random.length.unwrap_or(8))
|
||||
.take(config.paste.random.length)
|
||||
.map(char::from)
|
||||
.collect::<String>(),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue