mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-22 04:19:47 -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",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
"petname",
|
"petname",
|
||||||
|
"rand 0.8.4",
|
||||||
"serde 1.0.126",
|
"serde 1.0.126",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ serde = "1.0.126"
|
||||||
futures-util = "0.3.15"
|
futures-util = "0.3.15"
|
||||||
config = "0.11.0"
|
config = "0.11.0"
|
||||||
petname = "1.1.0"
|
petname = "1.1.0"
|
||||||
|
rand = "0.8.4"
|
||||||
|
|
||||||
[dependencies.byte-unit]
|
[dependencies.byte-unit]
|
||||||
version = "4.0.12"
|
version = "4.0.12"
|
||||||
|
|
|
@ -7,4 +7,5 @@ upload_path="./upload"
|
||||||
|
|
||||||
[paste]
|
[paste]
|
||||||
pet_names = true
|
pet_names = true
|
||||||
|
random = { enabled = false, length = 8 }
|
||||||
default_extension = "txt"
|
default_extension = "txt"
|
||||||
|
|
|
@ -31,10 +31,21 @@ pub struct ServerConfig {
|
||||||
pub struct PasteConfig {
|
pub struct PasteConfig {
|
||||||
/// Use pet names instead of original file names.
|
/// Use pet names instead of original file names.
|
||||||
pub pet_names: bool,
|
pub pet_names: bool,
|
||||||
|
/// Random string configuration.
|
||||||
|
pub random: RandomConfig,
|
||||||
/// Default file extension.
|
/// Default file extension.
|
||||||
pub default_extension: String,
|
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 {
|
impl Config {
|
||||||
/// Parses the config file and returns the values.
|
/// Parses the config file and returns the values.
|
||||||
pub fn parse(file_name: &str) -> Result<Config, ConfigError> {
|
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 crate::config::Config;
|
||||||
|
use rand::{distributions::Alphanumeric, Rng};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Result as IoResult, Write};
|
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` 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`] 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
|
/// [`default_extension`]: crate::config::PasteConfig::default_extension
|
||||||
/// [`pet_names`]: crate::config::PasteConfig::pet_names
|
/// [`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> {
|
pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult<String> {
|
||||||
if file_name == "-" {
|
if file_name == "-" {
|
||||||
file_name = "stdin";
|
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 {
|
if config.paste.pet_names {
|
||||||
path.set_file_name(petname::petname(2, "-"));
|
path.set_file_name(petname::petname(2, "-"));
|
||||||
path.set_extension(extension);
|
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 => {
|
None => {
|
||||||
if config.paste.pet_names {
|
if config.paste.pet_names {
|
||||||
path.set_file_name(petname::petname(2, "-"));
|
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);
|
path.set_extension(&config.paste.default_extension);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue