Fix bytes/string handling in email service
All checks were successful
Build And Test / publish (push) Successful in 48s

IMAP data can be bytes; handle both in decode_mime_header and _get_text_snippet.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-30 16:18:51 -08:00
parent c2a4a1b0d0
commit ada760cc43

View File

@@ -22,9 +22,12 @@ from models.common import OperationResult
from config import Settings from config import Settings
def decode_mime_header(header: Optional[str]) -> str: def decode_mime_header(header) -> str:
if not header: if not header:
return "" return ""
# Handle bytes input from IMAP
if isinstance(header, bytes):
header = header.decode("utf-8", errors="replace")
decoded_parts = [] decoded_parts = []
for part, encoding in decode_header(header): for part, encoding in decode_header(header):
if isinstance(part, bytes): if isinstance(part, bytes):
@@ -516,7 +519,14 @@ class EmailService:
body_text, body_html = self._get_body(msg) body_text, body_html = self._get_body(msg)
text = body_text or "" text = body_text or ""
# Ensure text is a string
if isinstance(text, bytes):
text = text.decode("utf-8", errors="replace")
if not text and body_html: if not text and body_html:
# Ensure body_html is a string before regex
if isinstance(body_html, bytes):
body_html = body_html.decode("utf-8", errors="replace")
# Strip HTML tags for snippet # Strip HTML tags for snippet
text = re.sub(r"<[^>]+>", "", body_html) text = re.sub(r"<[^>]+>", "", body_html)
text = re.sub(r"\s+", " ", text).strip() text = re.sub(r"\s+", " ", text).strip()