From a273b7f5e1e827bf55c8607897149f0d4ff28e16 Mon Sep 17 00:00:00 2001 From: rustmailer Date: Sat, 7 Mar 2026 17:50:20 +0800 Subject: [PATCH] Fix eml ID conversion issue --- src/modules/indexer/manager.rs | 34 ++++++++++++++++++++++++++++----- src/modules/message/append.rs | 10 ++++------ src/modules/message/content.rs | 4 ++++ src/modules/rest/api/message.rs | 16 ++++++++-------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/modules/indexer/manager.rs b/src/modules/indexer/manager.rs index 462fa0c..2c044cb 100644 --- a/src/modules/indexer/manager.rs +++ b/src/modules/indexer/manager.rs @@ -476,9 +476,9 @@ impl EmlIndexManager { Box::new(boolean_query) } - pub async fn get(&self, account_id: u64, eid: u64) -> BichonResult>> { + pub async fn get(&self, account_id: u64, eml_id: u64) -> BichonResult>> { let searcher = self.reader.searcher(); - let query = self.envelope_query(account_id, eid); + let query = self.envelope_query(account_id, eml_id); let docs = searcher .search(query.as_ref(), &TopDocs::with_limit(1)) .map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?; @@ -510,9 +510,21 @@ impl EmlIndexManager { } pub async fn get_reader(&self, account_id: u64, eid: u64) -> BichonResult { - let data = self.get(account_id, eid).await?.ok_or_else(|| { + let envelope = duckdb()? + .get_envelope_by_id(account_id, eid)? + .ok_or_else(|| { + raise_error!( + format!( + "Email envelope not found: account_id={} id={}", + account_id, eid + ), + ErrorCode::ResourceNotFound + ) + })?; + let eml_id = create_hash(account_id, &envelope.message_id); + let data = self.get(account_id, eml_id).await?.ok_or_else(|| { raise_error!( - format!("Email not found: account_id={}, eid={}", account_id, eid), + format!("Eml not found: account_id={}, eid={}", account_id, eid), ErrorCode::ResourceNotFound ) })?; @@ -539,7 +551,19 @@ impl EmlIndexManager { eid: u64, file_name: &str, ) -> BichonResult { - let data = self.get(account_id, eid).await?.ok_or_else(|| { + let envelope = duckdb()? + .get_envelope_by_id(account_id, eid)? + .ok_or_else(|| { + raise_error!( + format!( + "Email envelope not found: account_id={} id={}", + account_id, eid + ), + ErrorCode::ResourceNotFound + ) + })?; + let eml_id = create_hash(account_id, &envelope.message_id); + let data = self.get(account_id, eml_id).await?.ok_or_else(|| { raise_error!( format!("Email not found: account_id={}, eid={}", account_id, eid), ErrorCode::ResourceNotFound diff --git a/src/modules/message/append.rs b/src/modules/message/append.rs index 0e742f5..42f0f68 100644 --- a/src/modules/message/append.rs +++ b/src/modules/message/append.rs @@ -5,6 +5,7 @@ use crate::{ error::{code::ErrorCode, BichonResult}, imap::executor::ImapExecutor, indexer::manager::{EML_INDEX_MANAGER, ENVELOPE_INDEX_MANAGER}, + utils::create_hash, }, raise_error, }; @@ -55,16 +56,13 @@ pub async fn restore_emails(account_id: u64, message_ids: Vec) -> BichonRes ErrorCode::ResourceNotFound ) })?; - + let eml_id = create_hash(account_id, &envelope.message_id); let eml = EML_INDEX_MANAGER - .get(account_id, message_id) + .get(account_id, eml_id) .await? .ok_or_else(|| { raise_error!( - format!( - "Email record not found: account_id={} id={}", - account_id, message_id - ), + format!("Eml not found: account_id={} id={}", account_id, message_id), ErrorCode::ResourceNotFound ) })?; diff --git a/src/modules/message/content.rs b/src/modules/message/content.rs index 7f15fc9..7f748d3 100644 --- a/src/modules/message/content.rs +++ b/src/modules/message/content.rs @@ -206,6 +206,10 @@ pub async fn retrieve_email_content(account_id: u64, id: u64) -> BichonResult, /// The ID of the message to download. - message_id: Path, + envelope_id: Path, context: ClientContext, ) -> ApiResult> { let account_id = account_id.0; @@ -220,12 +220,12 @@ impl MessageApi { context .require_permission(Some(account_id), Permission::DATA_RAW_DOWNLOAD) .await?; - let message_id = message_id.0; - let reader = EML_INDEX_MANAGER.get_reader(account_id, message_id).await?; + let envelope_id = envelope_id.0; + let reader = EML_INDEX_MANAGER.get_reader(account_id, envelope_id).await?; let body = Body::from_async_read(reader); let attachment = Attachment::new(body) .attachment_type(AttachmentType::Attachment) - .filename(format!("{message_id}.eml")); + .filename(format!("{envelope_id}.eml")); Ok(attachment) } @@ -250,7 +250,7 @@ impl MessageApi { /// Downloads a specific attachment from an email. Requires `name` query parameter. #[oai( - path = "/download-attachment/:account_id/:message_id", + path = "/download-attachment/:account_id/:envelope_id", method = "get", operation_id = "download_attachment" )] @@ -259,7 +259,7 @@ impl MessageApi { /// The ID of the account. account_id: Path, /// The ID of the message containing the attachment. - message_id: Path, + envelope_id: Path, /// The filename of the attachment to download. name: Query, context: ClientContext, @@ -271,7 +271,7 @@ impl MessageApi { .await?; let name = name.0.trim(); let reader = EML_INDEX_MANAGER - .get_attachment(account_id, message_id.0, name) + .get_attachment(account_id, envelope_id.0, name) .await?; let body = Body::from_async_read(reader); let attachment = Attachment::new(body)