feat(avatar): understand BOXIM image messages
This commit is contained in:
@@ -236,11 +236,40 @@ async def _analyze_uploaded_image(
|
||||
max_bytes = max(1024, int(os.getenv("CHAT_IMAGE_MAX_BYTES", str(8 * 1024 * 1024))))
|
||||
content = await file.read(max_bytes + 1)
|
||||
filename = os.path.basename(file.filename or "图片")[:255]
|
||||
try:
|
||||
return _analyze_image_bytes(
|
||||
db,
|
||||
avatar,
|
||||
content,
|
||||
filename=filename,
|
||||
mime_type=file.content_type or "",
|
||||
uploader_kind=uploader_kind,
|
||||
)
|
||||
except ImageValidationError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
except InsufficientTokensError:
|
||||
raise
|
||||
except RuntimeError as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
finally:
|
||||
content = b""
|
||||
|
||||
|
||||
def _analyze_image_bytes(
|
||||
db: Session,
|
||||
avatar: Avatar,
|
||||
content: bytes,
|
||||
*,
|
||||
filename: str,
|
||||
mime_type: str,
|
||||
uploader_kind: str,
|
||||
) -> ChatAttachment:
|
||||
"""Analyze image bytes from either HTTP upload or BOXIM without persisting raw data."""
|
||||
attachment = ChatAttachment(
|
||||
avatar_id=avatar.id,
|
||||
uploader_kind=uploader_kind,
|
||||
filename=filename,
|
||||
mime_type=(file.content_type or "")[:100],
|
||||
mime_type=(mime_type or "")[:100],
|
||||
file_size=len(content),
|
||||
status="processing",
|
||||
expires_at=_attachment_expiry(),
|
||||
@@ -312,7 +341,7 @@ async def _analyze_uploaded_image(
|
||||
attachment.status = "failed"
|
||||
attachment.warning = str(exc)
|
||||
db.commit()
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
raise
|
||||
except InsufficientTokensError:
|
||||
attachment.status = "failed"
|
||||
attachment.warning = "积分余额不足"
|
||||
@@ -328,9 +357,7 @@ async def _analyze_uploaded_image(
|
||||
avatar.id,
|
||||
type(exc).__name__,
|
||||
)
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
finally:
|
||||
content = b""
|
||||
raise
|
||||
|
||||
|
||||
def _normalize_question(value: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user