{"record":{"id":"ba81e870a04f4e3d","repo":"siyuan-note/siyuan","slug":"unsupported-or-invalid-image","errorCode":null,"errorMessage":"unsupported or invalid image: ","messagePattern":"unsupported or invalid image: ","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/openai.go","lineNumber":730,"sourceCode":"func PrepareModelImage(data []byte, maxBytes, maxPixels, maxEdge int) (PreparedImage, error) {\n\tif len(data) == 0 {\n\t\treturn PreparedImage{}, errors.New(\"image data is empty\")\n\t}\n\tif maxBytes > 0 && len(data) > maxBytes {\n\t\treturn PreparedImage{}, fmt.Errorf(\"image exceeds size limit: %d bytes\", maxBytes)\n\t}\n\tmimeType := mimetype.Detect(data).String()\n\tif strings.Contains(mimeType, \"svg\") || bytes.Contains(bytes.ToLower(data[:min(len(data), 512)]), []byte(\"<svg\")) {\n\t\treturn PreparedImage{}, errors.New(\"SVG images are not accepted by multimodal models\")\n\t}\n\tswitch mimeType {\n\tcase \"image/gif\", \"image/jpeg\", \"image/png\", \"image/webp\":\n\tdefault:\n\t\treturn PreparedImage{}, fmt.Errorf(\"unsupported image type: %s\", mimeType)\n\t}\n\tconfig, _, err := image.DecodeConfig(bytes.NewReader(data))\n\tif err != nil {\n\t\treturn PreparedImage{}, errors.New(\"unsupported or invalid image: \" + err.Error())\n\t}\n\tif config.Width < 1 || config.Height < 1 || maxPixels > 0 && int64(config.Width)*int64(config.Height) > int64(maxPixels) {\n\t\treturn PreparedImage{}, fmt.Errorf(\"image exceeds pixel limit: %d\", maxPixels)\n\t}\n\n\tdecoded, err := imaging.Decode(bytes.NewReader(data), imaging.AutoOrientation(true))\n\tif err != nil {\n\t\treturn PreparedImage{}, errors.New(\"decode image failed: \" + err.Error())\n\t}\n\tbounds := decoded.Bounds()\n\tneedsResize := maxEdge > 0 && (bounds.Dx() > maxEdge || bounds.Dy() > maxEdge)\n\tif !needsResize && bounds.Dx() == config.Width && bounds.Dy() == config.Height && mimeType != \"image/gif\" {\n\t\treturn PreparedImage{\n\t\t\tData:       data,\n\t\t\tMIMEType:   mimeType,\n\t\t\tWidth:      bounds.Dx(),\n\t\t\tHeight:     bounds.Dy(),\n\t\t\tSourceSize: len(data),","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/afa823b6b4e4f183511e0bc0a3be93caa94c7c97/kernel/util/openai.go#L712-L748","documentation":"Thrown by util.PrepareModelImage (kernel/util/openai.go:730) when the MIME type passed the whitelist but Go's image.DecodeConfig cannot parse the image header. The magic bytes look right, yet the file structure is broken or uses a variant the registered decoders (stdlib gif/jpeg/png plus golang.org/x/image/webp) cannot read. The underlying stdlib error text is appended.","triggerScenarios":"Truncated download or partially copied asset (valid signature, cut off before IHDR/VP8 header); PNG with corrupt IHDR; extended/animated WebP that x/image/webp rejects; header fields encoding zero or absurd dimensions.","commonSituations":"Assets damaged by interrupted sync or copy-on-write races, web-clipped images cut off mid-download, WebP files from encoders producing animation or feature chunks Go cannot parse.","solutions":["Re-export or re-download the original image and retry","Verify integrity out-of-band (file, pngcheck -v, magick identify) and replace corrupt assets","Round-trip convert with a tolerant tool (magick broken.png fixed.png) which often rebuilds a valid header","Catch this error per-image in batch loops and skip the bad asset instead of failing the run"],"exampleFix":"// before\nprepared, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)\n\n// after\nif _, _, cfgErr := image.DecodeConfig(bytes.NewReader(data)); cfgErr != nil {\n    data = reencodeExternally(data) // magick in -out out.png rebuilds headers\n}\nprepared, err := util.PrepareModelImage(data, maxBytes, maxPixels, maxEdge)","handlingStrategy":"validation","validationCode":"if _, _, err := image.DecodeConfig(bytes.NewReader(data)); err != nil {\n    // header already broken: re-export or skip before calling PrepareModelImage\n    data = reencodeExternally(data)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.HasPrefix(err.Error(), \"unsupported or invalid image:\") {\n    // asset bytes are corrupt past magic bytes: re-export source or skip asset\n}","preventionTips":["Verify synced assets complete (size/checksum) before analysis","Run pngcheck or magick identify on suspicious assets","Treat header-decode failure as permanent for that file; re-export, do not retry unchanged"],"tags":["go","image","corruption","decode","multimodal"],"backgroundTag":"image-decode-failed","analyzedSha":"afa823b6b4e4f183511e0bc0a3be93caa94c7c97","analyzedAt":"2026-08-18T17:04:10.865Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}