<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">

<cfset response = structNew()>
<cfset response["OK"] = false>

<cftry>
    <!--- Step 1: Check form fields --->
    <cfset response["step"] = "checking form fields">
    <cfset formFields = form.keyArray()>
    <cfset response["formFields"] = formFields>

    <!--- Step 2: Find file fields --->
    <cfset response["step"] = "finding file fields">
    <cfset uploadedFiles = arrayNew(1)>
    <cfloop array="#formFields#" index="fieldName">
        <cfif reFindNoCase("^file[0-9]+$", fieldName)>
            <cfset arrayAppend(uploadedFiles, fieldName)>
        </cfif>
    </cfloop>
    <cfset response["uploadedFiles"] = uploadedFiles>

    <cfif arrayLen(uploadedFiles) EQ 0>
        <cfthrow message="No files found">
    </cfif>

    <!--- Step 3: Create temp directory --->
    <cfset response["step"] = "creating temp dir">
    <cfset uploadDir = getTempDirectory() & "test_uploads/">
    <cfif NOT directoryExists(uploadDir)>
        <cfdirectory action="create" directory="#uploadDir#">
    </cfif>
    <cfset response["uploadDir"] = uploadDir>

    <!--- Step 4: Upload first file --->
    <cfset response["step"] = "uploading file">
    <cfset fieldName = uploadedFiles[1]>
    <cffile action="upload" destination="#uploadDir#" filefield="#fieldName#"
            accept="image/jpeg,image/png,image/gif,image/webp,application/pdf"
            nameconflict="makeunique" result="uploadResult">
    <cfset response["uploadResult"] = uploadResult.fileWasSaved>
    <cfset response["serverFile"] = uploadResult.serverFile>
    <cfset response["serverFileExt"] = uploadResult.serverFileExt>

    <!--- Step 5: Read the file --->
    <cfset response["step"] = "reading file">
    <cfset filePath = uploadResult.serverDirectory & "/" & uploadResult.serverFile>
    <cfset response["filePath"] = filePath>
    <cfset response["fileExists"] = fileExists(filePath)>

    <!--- Step 6: Try image operations --->
    <cfset fileExt = lCase(uploadResult.serverFileExt)>
    <cfif listFindNoCase("jpg,jpeg,png,gif,webp", fileExt)>
        <cfset response["step"] = "reading image">
        <cfimage action="read" source="#filePath#" name="img">
        <cfset response["imageWidth"] = img.width>
        <cfset response["imageHeight"] = img.height>

        <cfset response["step"] = "converting to base64">
        <cffile action="readbinary" file="#filePath#" variable="fileContent">
        <cfset base64Content = toBase64(fileContent)>
        <cfset response["base64Length"] = len(base64Content)>
    </cfif>

    <!--- Step 7: Cleanup --->
    <cfset response["step"] = "cleanup">
    <cffile action="delete" file="#filePath#">

    <cfset response["OK"] = true>
    <cfset response["step"] = "complete">

    <cfcatch type="any">
        <cfset response["MESSAGE"] = cfcatch.message>
        <cfif len(cfcatch.detail)>
            <cfset response["DETAIL"] = cfcatch.detail>
        </cfif>
        <cfif structKeyExists(cfcatch, "tagContext") AND arrayLen(cfcatch.tagContext) GT 0>
            <cfset response["DEBUG_LINE"] = cfcatch.tagContext[1].line>
            <cfset response["DEBUG_TEMPLATE"] = cfcatch.tagContext[1].template>
        </cfif>
    </cfcatch>
</cftry>

<cfoutput>#serializeJSON(response)#</cfoutput>
