ZhuLinsen/daily_stock_analysis · error · ValueError

无法验证类型: {mime_type}

Error message

无法验证类型: {mime_type}

What it means

Error "无法验证类型: {mime_type}" thrown in ZhuLinsen/daily_stock_analysis.

Source

Thrown at src/services/image_stock_extractor.py:81

ALLOWED_MIME = frozenset({"image/jpeg", "image/png", "image/webp", "image/gif"})
MAX_SIZE_BYTES = 5 * 1024 * 1024  # 5MB
VISION_API_TIMEOUT = 60  # seconds; avoid long blocks on network/API issues

# Magic bytes for server-side MIME validation (client Content-Type can be forged)
_IMAGE_SIGNATURES = {
    "image/jpeg": [b"\xff\xd8\xff"],
    "image/png": [b"\x89PNG\r\n\x1a\n"],
    "image/gif": [b"GIF87a", b"GIF89a"],
    "image/webp": [b"RIFF"],  # bytes[8:12] must be WEBP, checked separately
}


def _verify_image_magic_bytes(image_bytes: bytes, mime_type: str) -> None:
    """Verify actual file content matches declared MIME type (rejects forged Content-Type)."""
    if len(image_bytes) < 12:
        raise ValueError("图片文件过小或损坏")
    if mime_type not in _IMAGE_SIGNATURES:
        raise ValueError(f"无法验证类型: {mime_type}")
    if mime_type == "image/webp":
        if image_bytes[:4] != b"RIFF" or image_bytes[8:12] != b"WEBP":
            raise ValueError("文件内容与声明的类型 image/webp 不匹配,可能被篡改")
        return
    for sig in _IMAGE_SIGNATURES[mime_type]:
        if image_bytes.startswith(sig):
            return
    raise ValueError(f"文件内容与声明的类型 {mime_type} 不匹配,可能被篡改")


def _normalize_code(raw: str) -> Optional[str]:
    """Normalize and validate a single stock code. A-shares & HK: 5-6 digits; US: 1-5 letters."""
    s = raw.strip().upper()
    if not s:
        return None
    # A-shares & HK: 5-6 digit codes (600519, 00700, 09988)
    if s.isdigit() and len(s) in (5, 6):
        return s

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Upload the file with a supported image MIME type (png, jpeg, webp, etc.).
  2. Check the Content-Type of the upload; correct the client to send the proper type.
  3. Convert the file to a supported image format and retry.

When it happens

Trigger: Thrown at src/services/image_stock_extractor.py:81 when the library encounters an invalid state.

Common situations: Raised when the declared MIME type of an uploaded image cannot be verified; occurs with uncommon or missing content-type headers on upload.


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/ed243953efb4576e. Report an issue: GitHub.