{"record":{"id":"fb735c436b3bab2b","repo":"jamiepine/voicebox","slug":"invalid-image-file-str-e","errorCode":null,"errorMessage":"Invalid image file: {str(e)}","messagePattern":"Invalid image file: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"backend/services/profiles.py","lineNumber":649,"sourceCode":") -> VoiceProfileResponse:\n    \"\"\"\n    Upload and process avatar image for a profile.\n\n    Args:\n        profile_id: Profile ID\n        image_path: Path to uploaded image file\n        db: Database session\n\n    Returns:\n        Updated profile\n    \"\"\"\n    profile = db.query(DBVoiceProfile).filter_by(id=profile_id).first()\n    if not profile:\n        raise ValueError(f\"Profile {profile_id} not found\")\n\n    is_valid, error_msg = validate_image(image_path)\n    if not is_valid:\n        raise ValueError(error_msg)\n\n    if profile.avatar_path:\n        old_avatar = config.resolve_storage_path(profile.avatar_path)\n        if old_avatar is not None and old_avatar.exists():\n            old_avatar.unlink()\n\n    # Determine file extension from uploaded file\n    from PIL import Image\n\n    with Image.open(image_path) as img:\n        # Normalize JPEG variants (MPO is multi-picture format from some cameras)\n        img_format = img.format\n        if img_format in (\"MPO\", \"JPG\"):\n            img_format = \"JPEG\"\n\n        ext_map = {\"PNG\": \".png\", \"JPEG\": \".jpg\", \"WEBP\": \".webp\"}\n        ext = ext_map.get(img_format, \".png\")\n","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/jamiepine/voicebox/blob/51f49dea198384b4eb6087b72c17057c6eb1c1cd/backend/services/profiles.py#L631-L667","documentation":"Returned by validate_image() when PIL.Image.open() or img.load() throws — the file could not be decoded as any image. The original exception string is embedded in the message. upload_avatar() re-raises it as a ValueError. Causes range from truncated/corrupt files to non-image files with an image extension.","triggerScenarios":"Uploading a truncated download (partial JPEG); a non-image file renamed to .jpg/.png; a zero-byte file; an image in a format PIL cannot decode without extra plugins (AVIF, HEIC without pillow-heif); a file corrupted in transit.","commonSituations":"Interrupted downloads; files renamed to spoof an extension; server-side buffer/truncation during multipart upload; clients sending the wrong file handle.","solutions":["Re-save or re-download the source image so it is a complete, valid file.","Open the file locally with an image viewer or PIL to confirm it decodes before uploading.","Install required PIL plugins for the source format (e.g. pip install pillow-heif for HEIC).","Verify the multipart upload completes and that no proxy truncates the body."],"exampleFix":"// before: file is truncated/corrupt -> 'Invalid image file: ...'\n// after: validate locally first\nfrom PIL import Image\ntry:\n    with Image.open('avatar.jpg') as im:\n        im.load()  // raises if truncated\nexcept Exception:\n    # re-export or re-download the source\n    ...","handlingStrategy":"validation","validationCode":"from PIL import Image\n\ndef image_loads_cleanly(path: str) -> bool:\n    try:\n        with Image.open(path) as img:\n            img.load()\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    profile = await upload_avatar(profile_id, image_path, db)\nexcept ValueError as e:\n    if 'Invalid image file' in str(e):\n        raise HTTPException(422, str(e))\n    raise","preventionTips":["Open/decode test the file locally with PIL before uploading.","Ensure multipart uploads complete without truncation (proxy/buffer limits).","Install required PIL plugins (e.g. pillow-heif) if you expect those source formats."],"tags":["avatar","image","corrupt-file","validation","voicebox"],"backgroundTag":null,"analyzedSha":"51f49dea198384b4eb6087b72c17057c6eb1c1cd","analyzedAt":"2026-08-12T16:51:42.824Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}