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")>
|
<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 imageDataArray = arrayNew(1)>
|
||||||
<cfset downloadedCount = 0>
|
<cfset downloadedCount = 0>
|
||||||
|
<cfset localReadCount = 0>
|
||||||
|
|
||||||
<cfloop collection="#imageUrls#" item="imgUrl">
|
<cfloop collection="#imageUrls#" item="imgUrl">
|
||||||
<cfif downloadedCount GTE 20>
|
<cfif downloadedCount GTE 20>
|
||||||
|
|
@ -157,24 +167,47 @@
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<cftry>
|
<cftry>
|
||||||
|
<cfset imgBytes = 0>
|
||||||
|
<cfset imgContent = "">
|
||||||
|
<cfset mediaType = "image/jpeg">
|
||||||
|
|
||||||
|
<!--- 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 fileExists(localPath)>
|
||||||
|
<cfset imgContent = fileReadBinary(localPath)>
|
||||||
|
<cfset imgBytes = len(imgContent)>
|
||||||
|
|
||||||
|
<!--- 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 url="#imgUrl#" method="GET" timeout="10" result="imgResult" getasbinary="yes">
|
||||||
</cfhttp>
|
</cfhttp>
|
||||||
|
|
||||||
<cfif findNoCase("200", imgResult.statusCode) AND isBinary(imgResult.fileContent)>
|
<cfif findNoCase("200", imgResult.statusCode) AND isBinary(imgResult.fileContent)>
|
||||||
<!--- Check content type --->
|
|
||||||
<cfset contentType = structKeyExists(imgResult.responseHeader, "Content-Type") ? imgResult.responseHeader["Content-Type"] : "">
|
<cfset contentType = structKeyExists(imgResult.responseHeader, "Content-Type") ? imgResult.responseHeader["Content-Type"] : "">
|
||||||
|
|
||||||
<cfif reFindNoCase("image/(jpeg|jpg|png|gif|webp)", contentType)>
|
<cfif reFindNoCase("image/(jpeg|jpg|png|gif|webp)", contentType)>
|
||||||
<!--- Check image size (skip tiny images) --->
|
<cfset imgContent = imgResult.fileContent>
|
||||||
<cfset imgBytes = len(imgResult.fileContent)>
|
<cfset imgBytes = len(imgContent)>
|
||||||
|
|
||||||
<cfif imgBytes GT 5000>
|
|
||||||
<cfset base64Content = toBase64(imgResult.fileContent)>
|
|
||||||
|
|
||||||
<cfset mediaType = "image/jpeg">
|
|
||||||
<cfif findNoCase("png", contentType)><cfset mediaType = "image/png"></cfif>
|
<cfif findNoCase("png", contentType)><cfset mediaType = "image/png"></cfif>
|
||||||
<cfif findNoCase("gif", contentType)><cfset mediaType = "image/gif"></cfif>
|
<cfif findNoCase("gif", contentType)><cfset mediaType = "image/gif"></cfif>
|
||||||
<cfif findNoCase("webp", contentType)><cfset mediaType = "image/webp"></cfif>
|
<cfif findNoCase("webp", contentType)><cfset mediaType = "image/webp"></cfif>
|
||||||
|
</cfif>
|
||||||
|
</cfif>
|
||||||
|
</cfif>
|
||||||
|
|
||||||
|
<!--- Process the image if we got valid content --->
|
||||||
|
<cfif imgBytes GT 5000>
|
||||||
|
<cfset base64Content = toBase64(imgContent)>
|
||||||
|
|
||||||
<cfset imgSource = structNew()>
|
<cfset imgSource = structNew()>
|
||||||
<cfset imgSource["type"] = "base64">
|
<cfset imgSource["type"] = "base64">
|
||||||
|
|
@ -189,15 +222,13 @@
|
||||||
<cfset arrayAppend(imageDataArray, imgStruct)>
|
<cfset arrayAppend(imageDataArray, imgStruct)>
|
||||||
<cfset downloadedCount = downloadedCount + 1>
|
<cfset downloadedCount = downloadedCount + 1>
|
||||||
</cfif>
|
</cfif>
|
||||||
</cfif>
|
|
||||||
</cfif>
|
|
||||||
<cfcatch>
|
<cfcatch>
|
||||||
<!--- Skip failed downloads --->
|
<!--- Skip failed downloads --->
|
||||||
</cfcatch>
|
</cfcatch>
|
||||||
</cftry>
|
</cftry>
|
||||||
</cfloop>
|
</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.) --->
|
<!--- Look for embedded JSON data (Next.js __NEXT_DATA__, Toast state, etc.) --->
|
||||||
<cfset embeddedJsonData = "">
|
<cfset embeddedJsonData = "">
|
||||||
|
|
|
||||||
Reference in a new issue