Fix image media type detection from content, not extension

Claude API rejects images when the declared media type doesn't
match the actual content. Now detects JPEG/PNG/GIF/WebP from
base64 magic bytes instead of trusting file extensions or
Content-Type headers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-03-03 14:33:39 -08:00
parent 03c675336e
commit 983ba7c2e4

View file

@ -916,14 +916,13 @@
<cfset imgPath = zipImageFiles[imgIdx]> <cfset imgPath = zipImageFiles[imgIdx]>
<cftry> <cftry>
<cfset imgContent = fileReadBinary(imgPath)> <cfset imgContent = fileReadBinary(imgPath)>
<cfset imgExt = lCase(listLast(imgPath, "."))>
<cfset mediaType = "image/jpeg">
<cfif imgExt EQ "png"><cfset mediaType = "image/png">
<cfelseif imgExt EQ "gif"><cfset mediaType = "image/gif">
<cfelseif imgExt EQ "webp"><cfset mediaType = "image/webp">
</cfif>
<cfset base64Img = toBase64(imgContent)> <cfset base64Img = toBase64(imgContent)>
<!--- Detect actual media type from content, not extension --->
<cfset mediaType = "image/jpeg">
<cfif left(base64Img, 4) EQ "iVBO"><cfset mediaType = "image/png">
<cfelseif left(base64Img, 6) EQ "R0lGOD"><cfset mediaType = "image/gif">
<cfelseif left(base64Img, 5) EQ "UklGR"><cfset mediaType = "image/webp">
</cfif>
<cfset arrayAppend(response.steps, "Analyzing image: " & listLast(imgPath, "/\"))> <cfset arrayAppend(response.steps, "Analyzing image: " & listLast(imgPath, "/\"))>
<!--- Build Claude request for business info extraction ---> <!--- Build Claude request for business info extraction --->
@ -1195,6 +1194,12 @@
<!--- Process the image if we got valid content ---> <!--- Process the image if we got valid content --->
<cfif imgBytes GT 5000> <cfif imgBytes GT 5000>
<cfset base64Content = toBase64(imgContent)> <cfset base64Content = toBase64(imgContent)>
<!--- Detect actual media type from content, not extension/headers --->
<cfset mediaType = "image/jpeg">
<cfif left(base64Content, 4) EQ "iVBO"><cfset mediaType = "image/png">
<cfelseif left(base64Content, 6) EQ "R0lGOD"><cfset mediaType = "image/gif">
<cfelseif left(base64Content, 5) EQ "UklGR"><cfset mediaType = "image/webp">
</cfif>
<cfset imgSource = structNew()> <cfset imgSource = structNew()>
<cfset imgSource["type"] = "base64"> <cfset imgSource["type"] = "base64">