2020-09-27 03:52:13 +13:00
|
|
|
use crate::common_messages::Messages;
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
#[derive(Clone, Default)]
|
2020-09-27 03:52:13 +13:00
|
|
|
pub struct Extensions {
|
2021-12-18 07:29:37 +13:00
|
|
|
file_extensions: Vec<String>,
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Extensions {
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
/// List of allowed extensions, only files with this extensions will be checking if are duplicates
|
2020-09-27 06:51:28 +13:00
|
|
|
/// After, extensions cannot contains any dot, commas etc.
|
2020-09-27 03:52:13 +13:00
|
|
|
pub fn set_allowed_extensions(&mut self, mut allowed_extensions: String, text_messages: &mut Messages) {
|
2021-12-18 07:29:37 +13:00
|
|
|
if allowed_extensions.trim().is_empty() {
|
2020-09-27 03:52:13 +13:00
|
|
|
return;
|
|
|
|
}
|
2021-01-14 04:03:05 +13:00
|
|
|
allowed_extensions = allowed_extensions.replace("IMAGE", "jpg,kra,gif,png,bmp,tiff,hdr,svg");
|
2020-09-27 03:52:13 +13:00
|
|
|
allowed_extensions = allowed_extensions.replace("VIDEO", "mp4,flv,mkv,webm,vob,ogv,gifv,avi,mov,wmv,mpg,m4v,m4p,mpeg,3gp");
|
|
|
|
allowed_extensions = allowed_extensions.replace("MUSIC", "mp3,flac,ogg,tta,wma,webm");
|
|
|
|
allowed_extensions = allowed_extensions.replace("TEXT", "txt,doc,docx,odt,rtf");
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
let extensions: Vec<String> = allowed_extensions.split(',').map(str::trim).map(String::from).collect();
|
2020-09-27 03:52:13 +13:00
|
|
|
for mut extension in extensions {
|
2022-11-24 08:23:17 +13:00
|
|
|
if extension.is_empty() || extension.replace(['.', ' '], "").trim().is_empty() {
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-18 07:29:37 +13:00
|
|
|
if !extension.starts_with('.') {
|
2022-12-21 20:44:26 +13:00
|
|
|
extension = format!(".{extension}");
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if extension[1..].contains('.') {
|
2022-12-21 20:44:26 +13:00
|
|
|
text_messages.warnings.push(format!("{extension} is not valid extension because contains dot inside"));
|
2020-09-27 03:52:13 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-18 07:29:37 +13:00
|
|
|
if extension[1..].contains(' ') {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages
|
|
|
|
.warnings
|
2022-12-21 20:44:26 +13:00
|
|
|
.push(format!("{extension} is not valid extension because contains empty space inside"));
|
2021-12-18 07:29:37 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.file_extensions.contains(&extension) {
|
|
|
|
self.file_extensions.push(extension);
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.file_extensions.is_empty() {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages
|
|
|
|
.messages
|
|
|
|
.push("No valid extensions were provided, so allowing all extensions by default.".to_string());
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
2021-12-18 07:29:37 +13:00
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2021-12-18 07:29:37 +13:00
|
|
|
pub fn matches_filename(&self, file_name: &str) -> bool {
|
2021-12-18 07:44:29 +13:00
|
|
|
// assert_eq!(file_name, file_name.to_lowercase());
|
2021-12-18 07:29:37 +13:00
|
|
|
if !self.file_extensions.is_empty() && !self.file_extensions.iter().any(|e| file_name.ends_with(e)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2022-01-01 10:34:24 +13:00
|
|
|
pub fn using_custom_extensions(&self) -> bool {
|
|
|
|
!self.file_extensions.is_empty()
|
|
|
|
}
|
|
|
|
|
2021-12-18 07:29:37 +13:00
|
|
|
pub fn extend_allowed_extensions(&mut self, file_extensions: &[&str]) {
|
|
|
|
for extension in file_extensions {
|
|
|
|
assert!(extension.starts_with('.'));
|
2023-01-29 06:54:02 +13:00
|
|
|
self.file_extensions.push((*extension).to_string());
|
2021-12-18 07:29:37 +13:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 05:04:28 +12:00
|
|
|
|
|
|
|
pub fn validate_allowed_extensions(&mut self, file_extensions: &[&str]) {
|
|
|
|
let mut current_file_extensions = Vec::new();
|
|
|
|
|
|
|
|
for extension in file_extensions {
|
|
|
|
assert!(extension.starts_with('.'));
|
2023-01-29 06:54:02 +13:00
|
|
|
if self.file_extensions.contains(&(*extension).to_string()) {
|
|
|
|
current_file_extensions.push((*extension).to_string());
|
2022-05-24 05:04:28 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.file_extensions = current_file_extensions;
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|