Fix eml ID conversion issue

This commit is contained in:
rustmailer
2026-03-07 17:50:20 +08:00
parent 2cba001431
commit a273b7f5e1
4 changed files with 45 additions and 19 deletions
+29 -5
View File
@@ -476,9 +476,9 @@ impl EmlIndexManager {
Box::new(boolean_query) Box::new(boolean_query)
} }
pub async fn get(&self, account_id: u64, eid: u64) -> BichonResult<Option<Vec<u8>>> { pub async fn get(&self, account_id: u64, eml_id: u64) -> BichonResult<Option<Vec<u8>>> {
let searcher = self.reader.searcher(); 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 let docs = searcher
.search(query.as_ref(), &TopDocs::with_limit(1)) .search(query.as_ref(), &TopDocs::with_limit(1))
.map_err(|e| raise_error!(format!("{:#?}", e), ErrorCode::InternalError))?; .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<File> { pub async fn get_reader(&self, account_id: u64, eid: u64) -> BichonResult<File> {
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!( raise_error!(
format!("Email not found: account_id={}, eid={}", account_id, eid), 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!("Eml not found: account_id={}, eid={}", account_id, eid),
ErrorCode::ResourceNotFound ErrorCode::ResourceNotFound
) )
})?; })?;
@@ -539,7 +551,19 @@ impl EmlIndexManager {
eid: u64, eid: u64,
file_name: &str, file_name: &str,
) -> BichonResult<File> { ) -> BichonResult<File> {
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!( raise_error!(
format!("Email not found: account_id={}, eid={}", account_id, eid), format!("Email not found: account_id={}, eid={}", account_id, eid),
ErrorCode::ResourceNotFound ErrorCode::ResourceNotFound
+4 -6
View File
@@ -5,6 +5,7 @@ use crate::{
error::{code::ErrorCode, BichonResult}, error::{code::ErrorCode, BichonResult},
imap::executor::ImapExecutor, imap::executor::ImapExecutor,
indexer::manager::{EML_INDEX_MANAGER, ENVELOPE_INDEX_MANAGER}, indexer::manager::{EML_INDEX_MANAGER, ENVELOPE_INDEX_MANAGER},
utils::create_hash,
}, },
raise_error, raise_error,
}; };
@@ -55,16 +56,13 @@ pub async fn restore_emails(account_id: u64, message_ids: Vec<u64>) -> BichonRes
ErrorCode::ResourceNotFound ErrorCode::ResourceNotFound
) )
})?; })?;
let eml_id = create_hash(account_id, &envelope.message_id);
let eml = EML_INDEX_MANAGER let eml = EML_INDEX_MANAGER
.get(account_id, message_id) .get(account_id, eml_id)
.await? .await?
.ok_or_else(|| { .ok_or_else(|| {
raise_error!( raise_error!(
format!( format!("Eml not found: account_id={} id={}", account_id, message_id),
"Email record not found: account_id={} id={}",
account_id, message_id
),
ErrorCode::ResourceNotFound ErrorCode::ResourceNotFound
) )
})?; })?;
+4
View File
@@ -206,6 +206,10 @@ pub async fn retrieve_email_content(account_id: u64, id: u64) -> BichonResult<Fu
} }
} }
} }
//inline attachment will not be displayed in email attachment list
if inline && attachment.content_id().is_some() {
continue;
}
attachments.push(AttachmentInfo { attachments.push(AttachmentInfo {
filename, filename,
+8 -8
View File
@@ -203,7 +203,7 @@ impl MessageApi {
/// Downloads the raw EML file of a specific email. /// Downloads the raw EML file of a specific email.
#[oai( #[oai(
path = "/download-message/:account_id/:message_id", path = "/download-message/:account_id/:envelope_id",
method = "get", method = "get",
operation_id = "download_message" operation_id = "download_message"
)] )]
@@ -212,7 +212,7 @@ impl MessageApi {
/// The ID of the account. /// The ID of the account.
account_id: Path<u64>, account_id: Path<u64>,
/// The ID of the message to download. /// The ID of the message to download.
message_id: Path<u64>, envelope_id: Path<u64>,
context: ClientContext, context: ClientContext,
) -> ApiResult<Attachment<Body>> { ) -> ApiResult<Attachment<Body>> {
let account_id = account_id.0; let account_id = account_id.0;
@@ -220,12 +220,12 @@ impl MessageApi {
context context
.require_permission(Some(account_id), Permission::DATA_RAW_DOWNLOAD) .require_permission(Some(account_id), Permission::DATA_RAW_DOWNLOAD)
.await?; .await?;
let message_id = message_id.0; let envelope_id = envelope_id.0;
let reader = EML_INDEX_MANAGER.get_reader(account_id, message_id).await?; let reader = EML_INDEX_MANAGER.get_reader(account_id, envelope_id).await?;
let body = Body::from_async_read(reader); let body = Body::from_async_read(reader);
let attachment = Attachment::new(body) let attachment = Attachment::new(body)
.attachment_type(AttachmentType::Attachment) .attachment_type(AttachmentType::Attachment)
.filename(format!("{message_id}.eml")); .filename(format!("{envelope_id}.eml"));
Ok(attachment) Ok(attachment)
} }
@@ -250,7 +250,7 @@ impl MessageApi {
/// Downloads a specific attachment from an email. Requires `name` query parameter. /// Downloads a specific attachment from an email. Requires `name` query parameter.
#[oai( #[oai(
path = "/download-attachment/:account_id/:message_id", path = "/download-attachment/:account_id/:envelope_id",
method = "get", method = "get",
operation_id = "download_attachment" operation_id = "download_attachment"
)] )]
@@ -259,7 +259,7 @@ impl MessageApi {
/// The ID of the account. /// The ID of the account.
account_id: Path<u64>, account_id: Path<u64>,
/// The ID of the message containing the attachment. /// The ID of the message containing the attachment.
message_id: Path<u64>, envelope_id: Path<u64>,
/// The filename of the attachment to download. /// The filename of the attachment to download.
name: Query<String>, name: Query<String>,
context: ClientContext, context: ClientContext,
@@ -271,7 +271,7 @@ impl MessageApi {
.await?; .await?;
let name = name.0.trim(); let name = name.0.trim();
let reader = EML_INDEX_MANAGER let reader = EML_INDEX_MANAGER
.get_attachment(account_id, message_id.0, name) .get_attachment(account_id, envelope_id.0, name)
.await?; .await?;
let body = Body::from_async_read(reader); let body = Body::from_async_read(reader);
let attachment = Attachment::new(body) let attachment = Attachment::new(body)