Read images from disk for local ZIP uploads
When scanning extracted ZIP content from /temp/menu-import/, read images directly from the filesystem instead of re-downloading via HTTP. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
3eee356ac0
commit
06ca5462c2
1 changed files with 57 additions and 26 deletions
|
|
@ -147,9 +147,19 @@
|
|||
|
||||
<cfset arrayAppend(response.steps, "Found #structCount(imageUrls)# unique images")>
|
||||
|
||||
<!--- Download images (limit to 20) --->
|
||||
<!--- Check if we're scanning a local temp URL (ZIP upload) --->
|
||||
<cfset isLocalScan = isDefined("targetUrl") AND findNoCase("/temp/menu-import/", targetUrl)>
|
||||
<cfset localBasePath = "">
|
||||
<cfif isLocalScan>
|
||||
<!--- Extract the folder path from URL for local file reads --->
|
||||
<cfset localBasePath = expandPath(reReplaceNoCase(targetUrl, "https?://[^/]+(/temp/menu-import/[^/]+/).*", "\1"))>
|
||||
<cfset arrayAppend(response.steps, "Local scan detected, base path: " & localBasePath)>
|
||||
</cfif>
|
||||
|
||||
<!--- Download/read images (limit to 20) --->
|
||||
<cfset imageDataArray = arrayNew(1)>
|
||||
<cfset downloadedCount = 0>
|
||||
<cfset localReadCount = 0>
|
||||
|
||||
<cfloop collection="#imageUrls#" item="imgUrl">
|
||||
<cfif downloadedCount GTE 20>
|
||||
|
|
@ -157,47 +167,68 @@
|
|||
</cfif>
|
||||
|
||||
<cftry>
|
||||
<cfhttp url="#imgUrl#" method="GET" timeout="10" result="imgResult" getasbinary="yes">
|
||||
</cfhttp>
|
||||
<cfset imgBytes = 0>
|
||||
<cfset imgContent = "">
|
||||
<cfset mediaType = "image/jpeg">
|
||||
|
||||
<cfif findNoCase("200", imgResult.statusCode) AND isBinary(imgResult.fileContent)>
|
||||
<!--- Check content type --->
|
||||
<cfset contentType = structKeyExists(imgResult.responseHeader, "Content-Type") ? imgResult.responseHeader["Content-Type"] : "">
|
||||
<!--- Check if this is a local file we can read directly --->
|
||||
<cfif isLocalScan AND findNoCase("/temp/menu-import/", imgUrl)>
|
||||
<!--- Convert URL to local path --->
|
||||
<cfset localPath = expandPath(reReplaceNoCase(imgUrl, "https?://[^/]+(/temp/menu-import/.*)", "\1"))>
|
||||
|
||||
<cfif reFindNoCase("image/(jpeg|jpg|png|gif|webp)", contentType)>
|
||||
<!--- Check image size (skip tiny images) --->
|
||||
<cfset imgBytes = len(imgResult.fileContent)>
|
||||
<cfif fileExists(localPath)>
|
||||
<cfset imgContent = fileReadBinary(localPath)>
|
||||
<cfset imgBytes = len(imgContent)>
|
||||
|
||||
<cfif imgBytes GT 5000>
|
||||
<cfset base64Content = toBase64(imgResult.fileContent)>
|
||||
<!--- Determine media type from extension --->
|
||||
<cfset ext = lCase(listLast(localPath, "."))>
|
||||
<cfif ext EQ "png"><cfset mediaType = "image/png">
|
||||
<cfelseif ext EQ "gif"><cfset mediaType = "image/gif">
|
||||
<cfelseif ext EQ "webp"><cfset mediaType = "image/webp">
|
||||
</cfif>
|
||||
<cfset localReadCount = localReadCount + 1>
|
||||
</cfif>
|
||||
<cfelse>
|
||||
<!--- Fetch remote image via HTTP --->
|
||||
<cfhttp url="#imgUrl#" method="GET" timeout="10" result="imgResult" getasbinary="yes">
|
||||
</cfhttp>
|
||||
|
||||
<cfset mediaType = "image/jpeg">
|
||||
<cfif findNoCase("200", imgResult.statusCode) AND isBinary(imgResult.fileContent)>
|
||||
<cfset contentType = structKeyExists(imgResult.responseHeader, "Content-Type") ? imgResult.responseHeader["Content-Type"] : "">
|
||||
<cfif reFindNoCase("image/(jpeg|jpg|png|gif|webp)", contentType)>
|
||||
<cfset imgContent = imgResult.fileContent>
|
||||
<cfset imgBytes = len(imgContent)>
|
||||
<cfif findNoCase("png", contentType)><cfset mediaType = "image/png"></cfif>
|
||||
<cfif findNoCase("gif", contentType)><cfset mediaType = "image/gif"></cfif>
|
||||
<cfif findNoCase("webp", contentType)><cfset mediaType = "image/webp"></cfif>
|
||||
|
||||
<cfset imgSource = structNew()>
|
||||
<cfset imgSource["type"] = "base64">
|
||||
<cfset imgSource["media_type"] = mediaType>
|
||||
<cfset imgSource["data"] = base64Content>
|
||||
|
||||
<cfset imgStruct = structNew()>
|
||||
<cfset imgStruct["type"] = "image">
|
||||
<cfset imgStruct["source"] = imgSource>
|
||||
<cfset imgStruct["url"] = imgUrl>
|
||||
|
||||
<cfset arrayAppend(imageDataArray, imgStruct)>
|
||||
<cfset downloadedCount = downloadedCount + 1>
|
||||
</cfif>
|
||||
</cfif>
|
||||
</cfif>
|
||||
|
||||
<!--- Process the image if we got valid content --->
|
||||
<cfif imgBytes GT 5000>
|
||||
<cfset base64Content = toBase64(imgContent)>
|
||||
|
||||
<cfset imgSource = structNew()>
|
||||
<cfset imgSource["type"] = "base64">
|
||||
<cfset imgSource["media_type"] = mediaType>
|
||||
<cfset imgSource["data"] = base64Content>
|
||||
|
||||
<cfset imgStruct = structNew()>
|
||||
<cfset imgStruct["type"] = "image">
|
||||
<cfset imgStruct["source"] = imgSource>
|
||||
<cfset imgStruct["url"] = imgUrl>
|
||||
|
||||
<cfset arrayAppend(imageDataArray, imgStruct)>
|
||||
<cfset downloadedCount = downloadedCount + 1>
|
||||
</cfif>
|
||||
<cfcatch>
|
||||
<!--- Skip failed downloads --->
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
</cfloop>
|
||||
|
||||
<cfset arrayAppend(response.steps, "Downloaded #arrayLen(imageDataArray)# valid images")>
|
||||
<cfset arrayAppend(response.steps, "Loaded #arrayLen(imageDataArray)# valid images (#localReadCount# from local disk)")>
|
||||
|
||||
<!--- Look for embedded JSON data (Next.js __NEXT_DATA__, Toast state, etc.) --->
|
||||
<cfset embeddedJsonData = "">
|
||||
|
|
|
|||
Reference in a new issue