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) Handle null attachment filename during upload (#609)

Evan Hutnikd7d7f9f36141
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-8a72f7a40f3e48b76158dd067cdff99f4c525078a5bf5a2fe810ee39cbf977d8.json b/rust/cloud-storage/email_db_client/.sqlx/query-b3cccb591b27205ca6ed2b1575606e62d2f22b14691b9c428438b9452cae83da.json
similarity index 92%
rename from rust/cloud-storage/email_db_client/.sqlx/query-8a72f7a40f3e48b76158dd067cdff99f4c525078a5bf5a2fe810ee39cbf977d8.json
rename to rust/cloud-storage/email_db_client/.sqlx/query-b3cccb591b27205ca6ed2b1575606e62d2f22b14691b9c428438b9452cae83da.json
index d5d8e6d3bf..f4e2b838a6 100644
--- a/rust/cloud-storage/email_db_client/.sqlx/query-8a72f7a40f3e48b76158dd067cdff99f4c525078a5bf5a2fe810ee39cbf977d8.json
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-b3cccb591b27205ca6ed2b1575606e62d2f22b14691b9c428438b9452cae83da.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "\n SELECT\n a.id AS attachment_db_id,\n m.provider_id as \"email_provider_id!\",\n a.provider_attachment_id as \"provider_attachment_id!\",\n a.filename as \"filename!\",\n a.mime_type as \"mime_type!\",\n m.internal_date_ts as \"internal_date_ts!\",\n m.id as message_db_id,\n m.thread_id as thread_db_id,\n from_contact.email_address as sender_email,\n m.subject as subject\n FROM email_attachments a\n JOIN email_messages m ON a.message_id = m.id\n JOIN email_contacts from_contact ON m.from_contact_id = from_contact.id\n JOIN email_threads t ON m.thread_id = t.id\n WHERE a.id = $1 AND t.link_id = $2\n ",
+ "query": "\n SELECT\n a.id AS attachment_db_id,\n m.provider_id as \"email_provider_id!\",\n a.provider_attachment_id as \"provider_attachment_id!\",\n a.filename as \"filename?\",\n a.mime_type as \"mime_type!\",\n m.internal_date_ts as \"internal_date_ts!\",\n m.id as message_db_id,\n m.thread_id as thread_db_id,\n from_contact.email_address as sender_email,\n m.subject as subject\n FROM email_attachments a\n JOIN email_messages m ON a.message_id = m.id\n JOIN email_contacts from_contact ON m.from_contact_id = from_contact.id\n JOIN email_threads t ON m.thread_id = t.id\n WHERE a.id = $1 AND t.link_id = $2\n ",
"describe": {
"columns": [
{
@@ -20,7 +20,7 @@
},
{
"ordinal": 3,
- "name": "filename!",
+ "name": "filename?",
"type_info": "Varchar"
},
{
@@ -73,5 +73,5 @@
true
]
},
- "hash": "8a72f7a40f3e48b76158dd067cdff99f4c525078a5bf5a2fe810ee39cbf977d8"
+ "hash": "b3cccb591b27205ca6ed2b1575606e62d2f22b14691b9c428438b9452cae83da"
}
diff --git a/rust/cloud-storage/email_db_client/fixtures/fetch_insertable_attachments_for_new_email.sql b/rust/cloud-storage/email_db_client/fixtures/fetch_insertable_attachments_for_new_email.sql
index 936f9151fe..7cc9024a4b 100644
--- a/rust/cloud-storage/email_db_client/fixtures/fetch_insertable_attachments_for_new_email.sql
+++ b/rust/cloud-storage/email_db_client/fixtures/fetch_insertable_attachments_for_new_email.sql
@@ -181,6 +181,12 @@ VALUES ('00000000-0000-0000-0000-0000001a0102',
'image.jpg',
'image/jpeg',
NOW()),
+ ('00000000-0000-0000-0000-0000001a0104',
+ '00000000-0000-0000-0000-0000000e0101',
+ 'provider-att-104',
+ null,
+ 'application/pdf',
+ NOW()),
('00000000-0000-0000-0000-0000001a0103',
'00000000-0000-0000-0000-0000000e0101',
'provider-att-103',
diff --git a/rust/cloud-storage/email_db_client/fixtures/fetch_thread_attachments_for_backfill.sql b/rust/cloud-storage/email_db_client/fixtures/fetch_thread_attachments_for_backfill.sql
index 1bc9856309..46a9bcae52 100644
--- a/rust/cloud-storage/email_db_client/fixtures/fetch_thread_attachments_for_backfill.sql
+++ b/rust/cloud-storage/email_db_client/fixtures/fetch_thread_attachments_for_backfill.sql
@@ -172,6 +172,12 @@ VALUES ('00000000-0000-0000-0000-0000001a0102',
'archive.zip',
'application/zip',
NOW()),
+ ('00000000-0000-0000-0000-0000001a0105',
+ '00000000-0000-0000-0000-0000000b0001',
+ 'provider-att-105',
+ null,
+ 'application/pdf',
+ NOW()),
('00000000-0000-0000-0000-0000001a0104',
'00000000-0000-0000-0000-0000000b0001',
'provider-att-104',
diff --git a/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs b/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs
index 47ef94830a..6547f417ba 100644
--- a/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs
+++ b/rust/cloud-storage/email_db_client/src/attachments/provider/upload.rs
@@ -39,6 +39,7 @@ pub async fn thread_document_atts_for_backfill(
JOIN email_messages m ON a.message_id = m.id
JOIN email_contacts from_contact ON m.from_contact_id = from_contact.id
WHERE m.thread_id = $1
+ AND a.filename IS NOT NULL
-- attachment mime type filters injected below
{}
AND EXISTS ( -- only fetch if at least one message in the thread meets any of the criteria
@@ -253,6 +254,7 @@ pub async fn new_email_document_atts(
JOIN email_contacts from_contact ON m.from_contact_id = from_contact.id
LEFT JOIN document_email de ON de.email_attachment_id = a.id
WHERE m.provider_id = $1
+ AND a.filename IS NOT NULL
-- attachment mime type filters injected below
{}
AND de.email_attachment_id IS NULL
@@ -446,7 +448,7 @@ pub async fn fetch_attachment_upload_metadata_by_id(
a.id AS attachment_db_id,
m.provider_id as "email_provider_id!",
a.provider_attachment_id as "provider_attachment_id!",
- a.filename as "filename!",
+ a.filename as "filename?",
a.mime_type as "mime_type!",
m.internal_date_ts as "internal_date_ts!",
m.id as message_db_id,
diff --git a/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs b/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs
index 39ed24b390..5c97e64da8 100644
--- a/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs
+++ b/rust/cloud-storage/email_db_client/src/attachments/provider/upload/test.rs
@@ -16,9 +16,10 @@ async fn thread_attachments_for_backfill_condition_1(pool: Pool<Postgres>) -> Re
let thread_id = Uuid::parse_str("00000000-0000-0000-0000-000000000101")?;
let res = thread_document_atts_for_backfill(&pool, thread_id).await?;
- // Only the application/pdf attachment should be included
+ // Only the application/pdf attachment should be included - others have wrong file type or
+ // null filename
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "sent_doc.pdf");
+ assert_eq!(res[0].filename, Some("sent_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
Ok(())
@@ -39,7 +40,7 @@ async fn thread_attachments_for_backfill_condition_2(pool: Pool<Postgres>) -> Re
let res = thread_document_atts_for_backfill(&pool, thread_id).await?;
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "important_doc.pdf");
+ assert_eq!(res[0].filename, Some("important_doc.pdf".to_string()));
Ok(())
}
@@ -59,7 +60,7 @@ async fn thread_attachments_for_backfill_condition_3(pool: Pool<Postgres>) -> Re
let res = thread_document_atts_for_backfill(&pool, thread_id).await?;
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "same_domain_doc.pdf");
+ assert_eq!(res[0].filename, Some("same_domain_doc.pdf".to_string()));
Ok(())
}
@@ -98,7 +99,7 @@ async fn thread_attachments_for_backfill_condition_4(pool: Pool<Postgres>) -> Re
let res = thread_document_atts_for_backfill(&pool, thread_id).await?;
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "docusign_doc.pdf");
+ assert_eq!(res[0].filename, Some("docusign_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
Ok(())
@@ -126,7 +127,10 @@ async fn job_attachments_for_backfill_includes_previously_contacted_participants
assert_eq!(res.len(), 3);
// Check that the correct attachments are returned
- let filenames: Vec<&str> = res.iter().map(|a| a.filename.as_str()).collect();
+ let filenames: Vec<&str> = res
+ .iter()
+ .map(|a| a.filename.as_ref().map(|s| s.as_str()).unwrap())
+ .collect();
assert!(filenames.contains(&"valid_document.pdf"));
assert!(filenames.contains(&"mixed_thread_doc.pdf"));
assert!(filenames.contains(&"also_included_doc.docx"));
@@ -153,7 +157,7 @@ async fn insertable_attachments_condition_1_user_sent_message(pool: Pool<Postgre
// Should return 1 attachment (sent_message_doc.pdf)
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "sent_message_doc.pdf");
+ assert_eq!(res[0].filename, Some("sent_message_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
assert_eq!(res[0].email_provider_id, "target-msg-101");
@@ -176,7 +180,7 @@ async fn insertable_attachments_condition_2_important_label(pool: Pool<Postgres>
// Should return 1 attachment (important_doc.pdf)
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "important_doc.pdf");
+ assert_eq!(res[0].filename, Some("important_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
assert_eq!(res[0].email_provider_id, "target-msg-201");
@@ -199,7 +203,7 @@ async fn insertable_attachments_condition_3_same_domain(pool: Pool<Postgres>) ->
// Should return 1 attachment (same_domain_doc.pdf)
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "same_domain_doc.pdf");
+ assert_eq!(res[0].filename, Some("same_domain_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
assert_eq!(res[0].email_provider_id, "target-msg-301");
@@ -222,7 +226,10 @@ async fn insertable_attachments_condition_4_whitelisted_domain(pool: Pool<Postgr
// Should return 1 attachment (whitelisted_domain_doc.pdf)
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "whitelisted_domain_doc.pdf");
+ assert_eq!(
+ res[0].filename,
+ Some("whitelisted_domain_doc.pdf".to_string())
+ );
assert_eq!(res[0].mime_type, "application/pdf");
assert_eq!(res[0].email_provider_id, "target-msg-801");
@@ -248,7 +255,10 @@ async fn insertable_attachments_condition_5_previously_contacted(
// Should return 1 attachment (previously_contacted_doc.pdf)
// This should be found by the second query (condition 4)
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "previously_contacted_doc.pdf");
+ assert_eq!(
+ res[0].filename,
+ Some("previously_contacted_doc.pdf".to_string())
+ );
assert_eq!(res[0].mime_type, "application/pdf");
assert_eq!(res[0].email_provider_id, "target-msg-401");
@@ -311,7 +321,7 @@ async fn insertable_attachments_filters_mime_types(pool: Pool<Postgres>) -> Resu
// Should only return PDF, not image or zip
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "sent_message_doc.pdf");
+ assert_eq!(res[0].filename, Some("sent_message_doc.pdf".to_string()));
assert_eq!(res[0].mime_type, "application/pdf");
// Verify no filtered mime types are present
@@ -370,7 +380,7 @@ async fn insertable_attachments_returns_first_query_when_available(
// Should return result from first query
assert_eq!(res.len(), 1);
- assert_eq!(res[0].filename, "sent_message_doc.pdf");
+ assert_eq!(res[0].filename, Some("sent_message_doc.pdf".to_string()));
Ok(())
}
@@ -392,7 +402,10 @@ async fn thread_media_for_backfill_includes_non_inline_images_and_videos(
// Should NOT include document.pdf (filtered out)
assert_eq!(res.len(), 2);
- let filenames: Vec<&str> = res.iter().map(|a| a.filename.as_str()).collect();
+ let filenames: Vec<&str> = res
+ .iter()
+ .map(|a| a.filename.as_ref().map(|s| s.as_str()).unwrap())
+ .collect();
assert!(filenames.contains(&"photo.jpg"));
assert!(filenames.contains(&"video.mp4"));
assert!(!filenames.contains(&"document.pdf"));
@@ -441,7 +454,10 @@ async fn thread_media_for_backfill_handles_multiple_media_types(
// Should return 3 attachments: PNG, GIF, MOV
assert_eq!(res.len(), 3);
- let filenames: Vec<&str> = res.iter().map(|a| a.filename.as_str()).collect();
+ let filenames: Vec<&str> = res
+ .iter()
+ .map(|a| a.filename.as_ref().map(|s| s.as_str()).unwrap())
+ .collect();
assert!(filenames.contains(&"screenshot.png"));
assert!(filenames.contains(&"animation.gif"));
assert!(filenames.contains(&"clip.mov"));
@@ -485,7 +501,10 @@ async fn new_email_media_includes_non_inline_attachments(pool: Pool<Postgres>) -
// Should return 2 attachments: new_photo.jpg and new_video.mp4
assert_eq!(res.len(), 2);
- let filenames: Vec<&str> = res.iter().map(|a| a.filename.as_str()).collect();
+ let filenames: Vec<&str> = res
+ .iter()
+ .map(|a| a.filename.as_ref().map(|s| s.as_str()).unwrap())
+ .collect();
assert!(filenames.contains(&"new_photo.jpg"));
assert!(filenames.contains(&"new_video.mp4"));
@@ -522,9 +541,9 @@ async fn new_email_media_filters_inline_images(pool: Pool<Postgres>) -> Result<(
// Should return 2 attachment: attachment_image.png
// Should include inline_image.png (has content_id)
assert_eq!(res.len(), 2);
- assert_eq!(res[0].filename, "inline_image.png");
+ assert_eq!(res[0].filename, Some("inline_image.png".to_string()));
assert_eq!(res[0].mime_type, "image/png");
- assert_eq!(res[1].filename, "attachment_image.png");
+ assert_eq!(res[1].filename, Some("attachment_image.png".to_string()));
assert_eq!(res[1].mime_type, "image/png");
Ok(())
diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs
index ecdb3ef677..1fa91ea8a4 100644
--- a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs
+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/process.rs
@@ -65,20 +65,20 @@ pub async fn process_macro_id(
match processor.upload(link_id, &attachment).await {
Ok(_) => {
success_count.fetch_add(1, Ordering::Relaxed);
- println!("Successfully uploaded '{}' (index: {}) for {}", attachment.filename, index, macro_id);
+ println!("Successfully uploaded '{}' (index: {}) for {}", attachment.filename.unwrap_or("N/A".to_string()), index, macro_id);
}
Err(e) => {
// ignore weird file types. annoying game of whack a mole
if e.to_string().contains("file extension") {
println!(
"Skipping '{}' (index: {}) for {} due to unsupported mime type {}",
- attachment.filename, index, macro_id, attachment.mime_type
+ attachment.filename.unwrap_or("N/A".to_string()), index, macro_id, attachment.mime_type
);
return;
}
panic!(
"Failed to upload attachment - filename: {}, provider_attachment_id: {}, provider_message_id: {}, index: {}, macro_id: {}, error: {:?}",
- attachment.filename,
+ attachment.filename.unwrap_or("N/A".to_string()),
attachment.provider_attachment_id,
attachment.email_provider_id,
index,
diff --git a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs
index d661b218c4..316b7a21c3 100644
--- a/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs
+++ b/rust/cloud-storage/email_service/src/bin/backfill_gmail_attachments/upload.rs
@@ -34,7 +34,7 @@ impl AttachmentProcessor {
}
/// Orchestrates the full upload process for a single attachment.
- #[instrument(skip(self), fields(file_name = %attachment.filename, mime_type = %attachment.mime_type))]
+ #[instrument(skip(self), fields(file_name = ?attachment.filename, mime_type = %attachment.mime_type))]
pub async fn upload(
&self,
link_id: Uuid,
@@ -104,11 +104,14 @@ impl AttachmentProcessor {
attachment: &AttachmentUploadMetadata,
sha256_hex: &str,
) -> anyhow::Result<(String, String)> {
+ // we filter out null filenames in the db query, but set to default just in case
let file_name = attachment
.filename
+ .clone()
+ .unwrap_or_default()
.split('.')
.next()
- .unwrap_or(&attachment.filename)
+ .unwrap_or(&attachment.filename.clone().unwrap_or_default())
.to_string();
let file_type = mime_guess::get_mime_extensions_str(&attachment.mime_type)
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 ed0911c80b..bcbe083d76 100644
--- a/rust/cloud-storage/email_service/src/util/upload_attachment.rs
+++ b/rust/cloud-storage/email_service/src/util/upload_attachment.rs
@@ -175,12 +175,13 @@ 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
.filename
- .split('.')
- .next()
- .unwrap_or(&p.filename)
- .to_string();
+ .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()))
diff --git a/rust/cloud-storage/models_email/src/email/service/attachment.rs b/rust/cloud-storage/models_email/src/email/service/attachment.rs
index 80dc69a5ff..56b75bdf3e 100644
--- a/rust/cloud-storage/models_email/src/email/service/attachment.rs
+++ b/rust/cloud-storage/models_email/src/email/service/attachment.rs
@@ -65,7 +65,7 @@ pub struct AttachmentUploadMetadata {
pub email_provider_id: String,
pub provider_attachment_id: String,
pub mime_type: String,
- pub filename: String,
+ pub filename: Option<String>,
pub internal_date_ts: DateTime<Utc>,
pub message_db_id: Uuid,
pub thread_db_id: Uuid,