fix(mail): decode MIME-encoded attachment filenames during sync
- Decode =?utf-8?q?...?= filenames before sanitizing for both new and existing emails - Prevents filenames like __utf-8_q_Wire_236095209372_2Epdf_2Ehtml__ - Uses email.header.decode_header + make_header for proper decoding
This commit is contained in:
@@ -772,13 +772,19 @@ async def imap_sync_account(
|
|||||||
if existing_att_count == 0:
|
if existing_att_count == 0:
|
||||||
for att_data in attachments:
|
for att_data in attachments:
|
||||||
try:
|
try:
|
||||||
|
raw_filename = att_data["filename"] or "attachment"
|
||||||
|
try:
|
||||||
|
from email.header import decode_header, make_header
|
||||||
|
decoded_filename = str(make_header(decode_header(raw_filename)))
|
||||||
|
except Exception:
|
||||||
|
decoded_filename = raw_filename
|
||||||
storage_path = await _save_attachment_to_storage(
|
storage_path = await _save_attachment_to_storage(
|
||||||
existing_mail.id, att_data["filename"], att_data["content"]
|
existing_mail.id, decoded_filename, att_data["content"]
|
||||||
)
|
)
|
||||||
attachment = MailAttachment(
|
attachment = MailAttachment(
|
||||||
tenant_id=tenant_id,
|
tenant_id=tenant_id,
|
||||||
mail_id=existing_mail.id,
|
mail_id=existing_mail.id,
|
||||||
filename=_sanitize_filename(att_data["filename"]),
|
filename=_sanitize_filename(decoded_filename),
|
||||||
mime_type=att_data["mime_type"],
|
mime_type=att_data["mime_type"],
|
||||||
size_bytes=att_data["size"],
|
size_bytes=att_data["size"],
|
||||||
storage_path=storage_path,
|
storage_path=storage_path,
|
||||||
@@ -823,13 +829,20 @@ async def imap_sync_account(
|
|||||||
# Save attachments to storage and DB
|
# Save attachments to storage and DB
|
||||||
for att_data in attachments:
|
for att_data in attachments:
|
||||||
try:
|
try:
|
||||||
|
# Decode MIME-encoded filenames (e.g. =?utf-8?q?...?=)
|
||||||
|
raw_filename = att_data["filename"] or "attachment"
|
||||||
|
try:
|
||||||
|
from email.header import decode_header, make_header
|
||||||
|
decoded_filename = str(make_header(decode_header(raw_filename)))
|
||||||
|
except Exception:
|
||||||
|
decoded_filename = raw_filename
|
||||||
storage_path = await _save_attachment_to_storage(
|
storage_path = await _save_attachment_to_storage(
|
||||||
mail.id, att_data["filename"], att_data["content"]
|
mail.id, decoded_filename, att_data["content"]
|
||||||
)
|
)
|
||||||
attachment = MailAttachment(
|
attachment = MailAttachment(
|
||||||
tenant_id=tenant_id,
|
tenant_id=tenant_id,
|
||||||
mail_id=mail.id,
|
mail_id=mail.id,
|
||||||
filename=_sanitize_filename(att_data["filename"]),
|
filename=_sanitize_filename(decoded_filename),
|
||||||
mime_type=att_data["mime_type"],
|
mime_type=att_data["mime_type"],
|
||||||
size_bytes=att_data["size"],
|
size_bytes=att_data["size"],
|
||||||
storage_path=storage_path,
|
storage_path=storage_path,
|
||||||
|
|||||||
Reference in New Issue
Block a user