Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email-service): Attachment filetype guessing logic improvement (#628)

Evan Hutnik25745e3d5888
diff --git a/rust/cloud-storage/email_db_client/src/parse/service_to_db.rs b/rust/cloud-storage/email_db_client/src/parse/service_to_db.rs
index d2141c0c4a..6bd93aaf94 100644
--- a/rust/cloud-storage/email_db_client/src/parse/service_to_db.rs
+++ b/rust/cloud-storage/email_db_client/src/parse/service_to_db.rs
@@ -124,18 +124,30 @@ pub fn map_service_attachments_to_db(
service_attachments: &mut [service::attachment::Attachment],
message_db_id: Uuid,
) -> Vec<attachment::Attachment> {
+ // store extension in filename as lowercase
service_attachments
.iter_mut()
- .map(|service_attachment| attachment::Attachment {
- id: macro_uuid::generate_uuid_v7(),
- message_id: message_db_id,
- provider_attachment_id: service_attachment.provider_id.clone(),
- filename: service_attachment.filename.clone(),
- mime_type: service_attachment.mime_type.clone(),
- size_bytes: service_attachment.size_bytes,
- content_id: service_attachment.content_id.clone(),
- sfs_id: service_attachment.sfs_id,
- created_at: Utc::now(),
+ .map(|service_attachment| {
+ let filename = service_attachment.filename.clone().map(|f| {
+ if let Some((base, ext)) = f.rsplit_once('.')
+ && !ext.is_empty()
+ {
+ return format!("{}.{}", base, ext.to_lowercase());
+ }
+ f
+ });
+
+ attachment::Attachment {
+ id: macro_uuid::generate_uuid_v7(),
+ message_id: message_db_id,
+ provider_attachment_id: service_attachment.provider_id.clone(),
+ filename,
+ mime_type: service_attachment.mime_type.clone(),
+ size_bytes: service_attachment.size_bytes,
+ content_id: service_attachment.content_id.clone(),
+ sfs_id: service_attachment.sfs_id,
+ created_at: Utc::now(),
+ }
})
.collect()
}
diff --git a/rust/cloud-storage/email_service/src/util/upload_attachment.rs b/rust/cloud-storage/email_service/src/util/upload_attachment.rs
index bcbe083d76..68fdb6b868 100644
--- a/rust/cloud-storage/email_service/src/util/upload_attachment.rs
+++ b/rust/cloud-storage/email_service/src/util/upload_attachment.rs
@@ -176,21 +176,42 @@ fn calculate_hashes(data: &[u8]) -> (String, String) {
/// Determines the file name (without extension) and file type (extension) from the payload.
fn determine_file_metadata(p: &AttachmentUploadMetadata) -> anyhow::Result<(String, String)> {
// documents must have a file name to be inserted into Document table.
- let file_name = p
+ let original_file_name = p
.filename
.as_deref()
.context("attachment filename is missing")?;
- let file_name = file_name.split('.').next().unwrap_or(file_name).to_string();
-
- let file_type = mime_guess::get_mime_extensions_str(&p.mime_type)
- .and_then(|exts| exts.first().map(|s| s.to_string()))
- .ok_or_else(|| {
- anyhow!(
- "Failed to determine file extension from mime type: {}",
- p.mime_type
- )
- })?;
+ let file_name = original_file_name
+ .split('.')
+ .next()
+ .unwrap_or(original_file_name)
+ .to_string();
+
+ let file_type = match original_file_name
+ .rsplit_once('.')
+ .map(|(_, ext)| ext.trim())
+ .filter(|ext| !ext.is_empty())
+ {
+ // if it's a heic, the mime_type can sometimes be heif. hardcode file_type to match file name
+ Some(ext) if ext.eq_ignore_ascii_case("heic") => "heic".to_string(),
+ _ => mime_guess::get_mime_extensions_str(&p.mime_type)
+ .and_then(|exts| exts.first().map(|s| s.to_string()))
+ .or_else(|| {
+ // if mime_guess fails, use everything after the last '.' in the original filename
+ original_file_name
+ .rsplit_once('.')
+ .map(|(_, ext)| ext.trim())
+ .filter(|ext| !ext.is_empty())
+ .map(|ext| ext.to_string())
+ })
+ .ok_or_else(|| {
+ anyhow!(
+ "Failed to determine file extension from mime type ({}) or filename ({})",
+ p.mime_type,
+ original_file_name
+ )
+ })?,
+ };
Ok((file_name, file_type))
}
diff --git a/rust/cloud-storage/gmail_client/src/parse/payload.rs b/rust/cloud-storage/gmail_client/src/parse/payload.rs
index f0a6183761..79610684ed 100644
--- a/rust/cloud-storage/gmail_client/src/parse/payload.rs
+++ b/rust/cloud-storage/gmail_client/src/parse/payload.rs
@@ -126,9 +126,21 @@ pub fn parse_gmail_payload(
has_attachments_flag = true;
let content_id = find_header(&part.headers, "Content-ID").map(String::from);
+ // extension in filename as lowercase
+ let filename = Some(part.filename.clone())
+ .filter(|s| !s.is_empty())
+ .map(|f| {
+ if let Some((base, ext)) = f.rsplit_once('.')
+ && !ext.is_empty()
+ {
+ return format!("{}.{}", base, ext.to_lowercase());
+ }
+ f
+ });
+
attachments.push(AttachmentMetadataIntermediate {
provider_attachment_id: body.attachment_id.clone(),
- filename: Some(part.filename.clone()).filter(|s| !s.is_empty()),
+ filename,
mime_type: Some(part.mime_type.clone()),
size_bytes: Some(body.size),
content_id,