{"record":{"id":"75b7ae5b57e5e64e","repo":"ZhuLinsen/daily_stock_analysis","slug":"error-75b7ae","errorCode":null,"errorMessage":"图片文件过小或损坏","messagePattern":"图片文件过小或损坏","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"src/services/image_stock_extractor.py","lineNumber":79,"sourceCode":"_FAKE_CODES = frozenset({\"CODE\", \"NAME\", \"HIGH\", \"LOW\", \"MEDIUM\", \"CONFIDENCE\", \"JSON\"})\n\nALLOWED_MIME = frozenset({\"image/jpeg\", \"image/png\", \"image/webp\", \"image/gif\"})\nMAX_SIZE_BYTES = 5 * 1024 * 1024  # 5MB\nVISION_API_TIMEOUT = 60  # seconds; avoid long blocks on network/API issues\n\n# Magic bytes for server-side MIME validation (client Content-Type can be forged)\n_IMAGE_SIGNATURES = {\n    \"image/jpeg\": [b\"\\xff\\xd8\\xff\"],\n    \"image/png\": [b\"\\x89PNG\\r\\n\\x1a\\n\"],\n    \"image/gif\": [b\"GIF87a\", b\"GIF89a\"],\n    \"image/webp\": [b\"RIFF\"],  # bytes[8:12] must be WEBP, checked separately\n}\n\n\ndef _verify_image_magic_bytes(image_bytes: bytes, mime_type: str) -> None:\n    \"\"\"Verify actual file content matches declared MIME type (rejects forged Content-Type).\"\"\"\n    if len(image_bytes) < 12:\n        raise ValueError(\"图片文件过小或损坏\")\n    if mime_type not in _IMAGE_SIGNATURES:\n        raise ValueError(f\"无法验证类型: {mime_type}\")\n    if mime_type == \"image/webp\":\n        if image_bytes[:4] != b\"RIFF\" or image_bytes[8:12] != b\"WEBP\":\n            raise ValueError(\"文件内容与声明的类型 image/webp 不匹配，可能被篡改\")\n        return\n    for sig in _IMAGE_SIGNATURES[mime_type]:\n        if image_bytes.startswith(sig):\n            return\n    raise ValueError(f\"文件内容与声明的类型 {mime_type} 不匹配，可能被篡改\")\n\n\ndef _normalize_code(raw: str) -> Optional[str]:\n    \"\"\"Normalize and validate a single stock code. A-shares & HK: 5-6 digits; US: 1-5 letters.\"\"\"\n    s = raw.strip().upper()\n    if not s:\n        return None\n    # A-shares & HK: 5-6 digit codes (600519, 00700, 09988)","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/ZhuLinsen/daily_stock_analysis/blob/5159bd72e8373d215492dff122acc9d389e219c9/src/services/image_stock_extractor.py#L61-L97","documentation":"Error \"图片文件过小或损坏\" thrown in ZhuLinsen/daily_stock_analysis.","triggerScenarios":"Thrown at src/services/image_stock_extractor.py:79 when the library encounters an invalid state.","commonSituations":"Raised when an uploaded image is too small or corrupted to be a valid picture; occurs with truncated uploads or non-image bytes sent to the extraction endpoint.","solutions":["Re-upload a valid image file; the current one is too small or corrupted.","Verify the upload completed fully and the file is not truncated.","Open the image locally to confirm it is a valid, non-empty image before uploading."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5159bd72e8373d215492dff122acc9d389e219c9","analyzedAt":"2026-08-15T01:59:36.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}