Use .jpg extension for files with image/jpeg media type

This commit is contained in:
silverpill 2023-03-04 12:39:16 +00:00
parent bd53e147ca
commit 2787efc83f
3 changed files with 33 additions and 4 deletions

View file

@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Added `registration.default_role` configuration option.
- Save emojis attached to actor objects.
### Changed
- Use .jpg extension for files with image/jpeg media type.
### Deprecated
- Deprecated `default_role_read_only_user` configuration option (replaced by `registration.default_role`).

View file

@ -19,6 +19,8 @@ members = [
default-members = [
".",
"mitra-cli",
"mitra-config",
"mitra-utils",
]
[dependencies]

View file

@ -15,10 +15,16 @@ pub fn sniff_media_type(data: &[u8]) -> Option<String> {
data.sniff_mime_type().map(|val| val.to_string())
}
pub fn get_media_type_extension(media_type: &str) -> Option<String> {
get_mime_extensions_str(media_type)
.and_then(|extensions| extensions.first())
.map(|extension| extension.to_string())
pub fn get_media_type_extension(media_type: &str) -> Option<&'static str> {
match media_type {
// Override extension provided by mime_guess
"image/jpeg" => Some("jpg"),
_ => {
get_mime_extensions_str(media_type)
.and_then(|extensions| extensions.first())
.copied()
}
}
}
pub fn write_file(data: &[u8], file_path: &Path) -> Result<(), Error> {
@ -32,3 +38,20 @@ pub fn set_file_permissions(file_path: &Path, mode: u32) -> Result<(), Error> {
set_permissions(file_path, permissions)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_media_type_extension() {
assert_eq!(
get_media_type_extension("image/png"),
Some("png"),
);
assert_eq!(
get_media_type_extension("image/jpeg"),
Some("jpg"),
);
}
}