mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2024-11-21 11:59:48 -05:00
feat(paste): support blacklisting MIME types
This commit is contained in:
parent
03348cb38f
commit
e7ad855f4d
3 changed files with 19 additions and 1 deletions
|
@ -17,3 +17,8 @@ mime_override = [
|
|||
{ mime = "application/octet-stream", regex = "^.*\\.bin$" },
|
||||
{ mime = "text/plain", regex = "^.*\\.(log|txt|diff)$" },
|
||||
]
|
||||
mime_blacklist = [
|
||||
"application/x-dosexec",
|
||||
"application/java-archive",
|
||||
"application/java-vm"
|
||||
]
|
||||
|
|
|
@ -35,6 +35,8 @@ pub struct PasteConfig {
|
|||
pub default_extension: String,
|
||||
/// Media type override options.
|
||||
pub mime_override: Vec<MimeMatcher>,
|
||||
/// Media type blacklist.
|
||||
pub mime_blacklist: Vec<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
|
13
src/paste.rs
13
src/paste.rs
|
@ -48,6 +48,17 @@ impl Paste {
|
|||
/// [`default_extension`]: crate::config::PasteConfig::default_extension
|
||||
/// [`random_url.enabled`]: crate::random::RandomURLConfig::enabled
|
||||
pub fn store_file(&self, file_name: &str, config: &Config) -> IoResult<String> {
|
||||
let file_type = infer::get(&self.data);
|
||||
if let Some(file_type) = file_type {
|
||||
for mime_type in &config.paste.mime_blacklist {
|
||||
if mime_type == file_type.mime_type() {
|
||||
return Err(IoError::new(
|
||||
IoErrorKind::Other,
|
||||
String::from("this file type is not permitted"),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
let file_name = match PathBuf::from(file_name)
|
||||
.file_name()
|
||||
.map(|v| v.to_str())
|
||||
|
@ -70,7 +81,7 @@ impl Paste {
|
|||
path.set_file_name(file_name);
|
||||
}
|
||||
path.set_extension(
|
||||
infer::get(&self.data)
|
||||
file_type
|
||||
.map(|t| t.extension())
|
||||
.unwrap_or(&config.paste.default_extension),
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue