mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 20:09:48 -05:00
feat(paste): support using random file names
This commit is contained in:
parent
cc7e295e88
commit
fc425067f7
5 changed files with 34 additions and 0 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1177,6 +1177,7 @@ dependencies = [
|
|||
"futures-util",
|
||||
"log",
|
||||
"petname",
|
||||
"rand 0.8.4",
|
||||
"serde 1.0.126",
|
||||
]
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -7,4 +7,5 @@ upload_path="./upload"
|
|||
|
||||
[paste]
|
||||
pet_names = true
|
||||
random = { enabled = false, length = 8 }
|
||||
default_extension = "txt"
|
||||
|
|
|
@ -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> {
|
||||
|
|
20
src/file.rs
20
src/file.rs
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue