How to get the temporary path of a file pulled with CFHTTP in Coldfusion?

1k views Asked by At

I'm using Coldfusion8 and need to fetch images from a remote server, which I'm doing like this:

 <cfhttp timeout="45" throwonerror="no" url="#variables.testFilePath#" method="get" useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" getasbinary="yes" result="variables.objGet">
 <cfset variables.objImage = ImageNew(variables.objGet.FileContent)>

I now need to save the image to Amazon S3, but the function I want to use:

 <cfset result = s3.putObject(url.b,file.serverFile,file.contentType,'300',form.cacheControl,'30',form.acl,form.storageClass,form.keyName,GetTempDirectory())>

Requires the directory where my generated image can be found in.

Question:
Is there a way to get the directory of an image file pulled with cfhttp and converted to an image using imageNew? Or do I need to save the file to disk first? I also need to resize before storing, so I might not be able to get by without saving to disk first.

Thanks for pointers!

EDIT:
I got it working like this:

<!--- getAsBinary --->
<cfhttp timeout="45" 
    throwonerror="no" 
    url="#variables.testFilePath#" 
    method="get" 
    useragent="..."         
    getasbinary="yes" 
    result="objGet">

<!--- validate --->
<cfif len(variables.testFilePath) EQ 0>
    <cfset variables.errorCount = variables.errorCount+1>
    <cfset variables.failedLoads = "FILE NOT FOUND" >
<cfelse>
    <cfif len(objGet.Filecontent) EQ 0>
        <cfset variables.errorCount = variables.errorCount+1>
        <cfset variables.failedLoads = "COULD NOT LOAD IMAGE">
    <cfelseif NOT listfindnocase(variables.allow, variables.fileExt) >
        <cfset variables.errorCount = variables.errorCount+1>
        <cfset variables.failedLoads = "WRONG FILE TYPE">
    <cfelse>
        <cftry>
            <cfscript>
                objImage = ImageNew(objGet.FileContent);
                ImageSetAntialiasing(objImage,"on");
                <!--- resize/crop --->
                variables.keyName = Session.loginid & "_S_";
            </cfscript>
            <!--- convert modified image back to binary --->
            <cfset variables.filekey = toBase64( objImage )>
            <!--- pass to s3.cfc --->
            <cfset result = s3.putObject(variables.bucketName, variables.filekey, variables.contentType, variables.httptimeout, variables.cacheControl, variables.cacheDays, variables.acl, variables.storageClass, variables.keyName, variables.imageSrc, "true" )>
            <cfcatch>
                <cfset variables.errorCount = variables.errorCount+1>
                <cfset variables.failedLoads = "NO IMAGE">
            </cfcatch>
        </cftry>
    </cfif>

I need to re-convert the cropped image to binary, because the s3.putobject will otherwise do another cffile action="readBinary" and breaks on trying to construct the image file path (the image is still in temp) right here:

<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">

While I can get the temporary file path using this trick and set uploadDir it doesn't help, because CF docs say the path must be either an absolute path starting with drive letter or slash, otherwise the www-root temp directory will be taken.

In my case the temp www-root directory was on C:/ while the temp file CFFile-Servlet was on E:/ and a relative path did not work either (file not found). So as I found no way to re-read the image from the s3.cfc, I'm now converting back to binary before calling S3.cfc. I pass another parameter (1/0) telling s3.cfc, that I'm already sending the binary image and there is no need to re-read it.

Like so:

<!--- if encoded is true, filekey already is the encoded image --->
<cfif arguments.encoded EQ "true">
    <!--- already sending binary image --->
    <cfset binaryFileData = arguments.fileKey>
<cfelse>
    <!--- Default --->
    <cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
</cfif>

I'm not sure if this is the smartest way performance wise, but it seems to work pretty smooth. Comments are welcome!

3

There are 3 answers

3
Sergey Galashyn On BEST ANSWER

I guess you could use path and file attributes instead of result. Generate some temporary path using GetTempDirectory() + CreateUUID(), fetch and then drop it. Plus it may be a bit more memory-efficient thatn fetching content to the variable, then writing to the intermediate file.

0
genericHCU On

Cfhttp result stores the data in a memory variable. ImageNew creates a 'ColdFusion' image meaning it's resident in memory only also. You'd have to save it to make it a physical file to send either in cfhttp or imagewrite, etc.

Without saving it to a physical file you must use cffile action = "writetobrowser" to send it to a browser but that ends up saving it in a temp location for the browser to access but wouldn't do you much good here I don't think.

http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_h-im_34.html http://livedocs.adobe.com/coldfusion/8/htmldocs/Images_19.html

0
Rupert Rawnsley On

AmazonS3Client has a putObject method that takes an InputStream, so you can wrap the binary data in a ByteArrayInputStream and pass it in without worrying about the backing file.

Something like this:

<cfhttp method="get" url="#local.url#" result="local.httpResult" getAsBinary="true"/>
<cfif local.httpResult.statusCode eq "200 OK">
    <cfset var fileInputStream = createObject("java", "java.io.ByteArrayInputStream" ).init(local.httpResult.fileContent) />
    <cfset var objectMetadata = createObject("java", "com.amazonaws.services.s3.model.ObjectMetadata" ).init() />
    <cfset application.S3UTIL.s3Client.putObject("some.bucket", "folder/file.ext", local.fileInputStream, local.objectMetadata)/>
</cfif>