Fix bytes/string handling in email service
All checks were successful
Build And Test / publish (push) Successful in 48s
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:
@@ -22,9 +22,12 @@ from models.common import OperationResult
|
||||
from config import Settings
|
||||
|
||||
|
||||
def decode_mime_header(header: Optional[str]) -> str:
|
||||
def decode_mime_header(header) -> str:
|
||||
if not header:
|
||||
return ""
|
||||
# Handle bytes input from IMAP
|
||||
if isinstance(header, bytes):
|
||||
header = header.decode("utf-8", errors="replace")
|
||||
decoded_parts = []
|
||||
for part, encoding in decode_header(header):
|
||||
if isinstance(part, bytes):
|
||||
@@ -516,7 +519,14 @@ class EmailService:
|
||||
body_text, body_html = self._get_body(msg)
|
||||
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:
|
||||
# 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
|
||||
text = re.sub(r"<[^>]+>", "", body_html)
|
||||
text = re.sub(r"\s+", " ", text).strip()
|
||||
|
||||
Reference in New Issue
Block a user