Private
Public Access
1
0

core: attachment mime: prefer jpg instead of jfif

This commit is contained in:
2025-09-10 14:06:54 -07:00
parent 4e8b161d26
commit e8256a9e57
2 changed files with 36 additions and 5 deletions

View File

@@ -165,6 +165,7 @@ impl AttachmentStore {
// Check if any in-progress temporary file exists already for this attachment // Check if any in-progress temporary file exists already for this attachment
if let Some(in_progress) = Self::find_in_progress_download(&attachment, preview) { if let Some(in_progress) = Self::find_in_progress_download(&attachment, preview) {
log::warn!(target: target::ATTACHMENTS, "Temporary file already exists: {}, assuming download is in progress", in_progress.display()); log::warn!(target: target::ATTACHMENTS, "Temporary file already exists: {}, assuming download is in progress", in_progress.display());
// Treat as a non-fatal condition so we don't spam errors.
return Err(AttachmentStoreError::DownloadAlreadyInProgress.into()); return Err(AttachmentStoreError::DownloadAlreadyInProgress.into());
} }
@@ -183,8 +184,7 @@ impl AttachmentStore {
let guessed_ext = normalized_mime let guessed_ext = normalized_mime
.as_deref() .as_deref()
.and_then(|m| mime_guess::get_mime_extensions_str(m)) .and_then(|m| Attachment::preferred_extension_for_mime(m))
.and_then(|list| list.first().copied())
.unwrap_or("bin"); .unwrap_or("bin");
let final_path = attachment let final_path = attachment
@@ -327,7 +327,23 @@ impl AttachmentStore {
).await; ).await;
if let Err(e) = result { if let Err(e) = result {
log::error!(target: target::ATTACHMENTS, "Error downloading attachment {}: {}", &_guid, e); // Downgrade noise for expected cases
if let Some(kind) = e.downcast_ref::<AttachmentStoreError>() {
match kind {
AttachmentStoreError::AttachmentAlreadyDownloaded => {
log::debug!(target: target::ATTACHMENTS, "Attachment already downloaded: {}", &_guid);
}
AttachmentStoreError::DownloadAlreadyInProgress => {
// Already logged a warning where detected
log::debug!(target: target::ATTACHMENTS, "Download already in progress: {}", &_guid);
}
_ => {
log::error!(target: target::ATTACHMENTS, "Error downloading attachment {}: {}", &_guid, kind);
}
}
} else {
log::error!(target: target::ATTACHMENTS, "Error downloading attachment {}: {}", &_guid, e);
}
} }
}); });

View File

@@ -20,6 +20,22 @@ pub struct Attachment {
} }
impl Attachment { impl Attachment {
pub(crate) fn preferred_extension_for_mime(mime: &str) -> Option<&'static str> {
let normalized = mime.split(';').next().unwrap_or(mime).trim();
// Prefer common, user-friendly extensions over obscure ones
match normalized {
"image/jpeg" | "image/pjpeg" => Some("jpg"),
_ => mime_guess::get_mime_extensions_str(normalized)
.and_then(|list| {
// If jpg is one of the candidates, prefer it
if list.iter().any(|e| *e == "jpg") {
Some("jpg")
} else {
list.first().copied()
}
}),
}
}
pub fn get_path(&self) -> PathBuf { pub fn get_path(&self) -> PathBuf {
self.get_path_for_preview_scratch(false, false) self.get_path_for_preview_scratch(false, false)
} }
@@ -45,8 +61,7 @@ impl Attachment {
let ext_from_mime = self let ext_from_mime = self
.mime_type .mime_type
.as_ref() .as_ref()
.and_then(|m| mime_guess::get_mime_extensions_str(m.split(';').next().unwrap_or(m))) .and_then(|m| Self::preferred_extension_for_mime(m));
.and_then(|list| list.first().copied());
let base_ext = match ext_from_mime { let base_ext = match ext_from_mime {
Some(ext) => format!("{}.{}", kind, ext), Some(ext) => format!("{}.{}", kind, ext),