ColdFusion FilegetMimeType giving inconsistent results - coldfusion

I need to use the FilegetMimeType() function, and I tested it in a program called demomimetype.cfm.
<cfoutput>
<cfset reploc = '#cookie.moxlog#'>
<cfset extrepoc = ExpandPath('#reploc#')>
<cfset typegif = FilegetMimeType('#extrepoc#/brightpink1.gif')>
typegif brightpink1.gif #typegif#<br>
</cfoutput>
This program returns the result that typegif is "image/gif" which is exactly correct. When I add the lines
<cfheader name = "Content-Disposition" value="attachment;#extrepoc#/logo106">
<cfcontent type="#typegif#" file="#extrepoc#/brightpink1.gif">
I get this result:
This is what I was hoping for, although demomimetype.cfm is not what I chose to open. However, this screen does open brightpink1.gif, so I'm okay with it.
Then I tried to integrate this into my actual system using this program, which results from a submit in an earlier program:
<cfoutput>
<cfset col = form.thecol>
<cfset colarray = getPageContext().getRequest().getParameterValues('xcol')>
<cfset origval = getPageContext().getRequest().getParameterValues('#col#')>
<cfset attname = origval[1]>
<cfset extreploc = ExpandPath("#cookie.moxlog#")>
<cfset typeatt = FilegetMimeType('#extreploc#/#attname#')>
</cfoutput>
The variable "attname" will be "brightpink1.gif. When I output typeatt I get "application/octet-stream", which is not correct. And when I add the lines
<cfheader name = "Content-Disposition" value="attachment;#extreploc#/#attname#">
<cfcontent type="#typeatt#" file="#extreploc#/#attname#">
I get the wrong result -- not surprising because the mimetype is wrong:
Obviously the demo is working correctly, and the real application is not. I have tried making the filename in the demo to be a variable, and the demo goes on working. I have tried making the filename in the application specific, and it still does not work.
I don't want to use the "strict" parameter because some of the files do not have extensions, even though they are of a specific type like .jpg.
There must be some reason why the code in the demo works and the nearly identical code in the application does not. But I cannot see it. Can anyone figure out what is going on here?

Related

How to change the user image using cfldap?

I'm able to get all the values that I want from cfldap.
But when I try to update the user image I don't know how to send the correct value for the binary image attribute.
I tried getting the image variable from the cffile upload
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="image" />
Also tried using cfimage with a static image -
<cfimage action="read" source="c:\inetpub\wwwroot\test\image.png" name="anotherImage">
Or even with
<cffile action="READBINARY" file="c:\inetpub\wwwroot\test\image.png" variable="BinaryImageContent">
But in any case, when I call
<cfldap action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#obj.image#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass">
The #results.dn# is the DN from the user that I get before (Everything ok on that)
I created the #obj.image# to be able to try all types of variables
Also tried these params:
<cfset obj.test1 = BinaryImageContent />
<cfdump var="#imageGetBlob(anotherImage)#" />
<cfdump var="#toString(obj.test1)#" />
By the way, the error that I get its
One or more of the required attributes may be missing or incorrect or
you do not have permissions to execute this operation on the server.
The problem is that I'm using the domain administrator account to update that
(THIS ERROR IS SOLVED - The network guys hadn't given me this permission... now I have it).
Now what I'm using is the following:
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="imagem" />
<cfset filename = "C:\inetpub\wwwroot\test\#imagem.serverFile#">
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr2 = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr2#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
but I get this binary code
Whats strange, is that before I had a binary code like -1-41 and now, nothing similar...
and when I try to show the pic
And this is one correct image....
EDIT: The original code sample below shows how it could work if ColdFusion wouldn't have a bug (or "very unfortunate design decision") in CFLDAP.
CFLDAP encodes the parameter values you pass to it before sending them to the server. This is nice because you don't have to worry about value encoding. But... it is also not helpful because it means you can't send encoded values yourself anymore, since CF invariably encodes them again.
Bottom line: As far as LDAP is concerned, encoding a file into a hex-string is correct, but CFLDAP mangles that string before sending it to the server. Combined with the fact that CFLDAP does not accept raw binary data this means that you can't use it to update binary attributes.
The comments contain a suggestion for a 3rd-party command line tool that can easily substitute CFLDAP for this task.
You need to send an encoded string to the server as the attribute value. The encoding scheme for binary data in LDAP queries has the form of attribute=\01\02\03\ab\af\cd.
Read your image into a byte array, encode that array into a hex string and prefix every encoded byte with a backslash.
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
Also don't forget what the documentation has to say about modifyType.

Excluding items from a list in coldfusion by type

Is there a way to exclude certain items by filetype in a list in Coldfusion?
Background: I just integrated a compression tool into an existing application and ran into the problem of the person's prior code would automatically grab the file from the upload destination on the server and push it to the Network Attached Storage. The aim now is to stop their NAS migration code from moving all files to the NAS, only those which are not PDF's. What I want to do is loop through their variable that stores the names of the files uploaded, and exclude the pdf's from the list then pass the list onto the NAS code, so all non pdf's are moved and all pdf's uploaded remain on the server. Working with their code is a challenge as no one commented or documented anything and I've been trying several approaches.
<cffile action="upload" destination= "c:\uploads\" result="myfiles" nameconflict="makeunique" >
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfif Len(get.realec_transactionid)>
<cfset internalOnly=1 >
</cfif>
**This line below is what I want to loop through and exclude file names
with pdf extensions **
<cfset uploadedfilenames='#myfiles.clientFile#' >
<CFSET a_insert_time = #TimeFormat(Now(), "HH:mm:ss")#>
<CFSET a_insert_date = #DateFormat(Now(), "mm-dd-yyyy")#>
**This line calls their method from another cfc that has all the file
migration methods.**
<cfset new_file_name = #fileSys.MoveFromUploads(uploadedfilenames)#>
**Once it moves the file to the NAS, it inserts the file info into the
DB table here**
<cfquery name="addFile" datasource="#request.dsn#">
INSERT INTO upload_many (title_id, fileDate, filetime, fileupload)
VALUES('#get.title_id#', '#dateTimeStamp#', '#a_insert_time#', '#new_file_name#')
</cfquery>
<cfelse>
<cffile action="upload" destination= #ExpandPath("./uploaded_files/zip.txt")# nameconflict="overwrite" >
</cfif>
Update 6/18
Trying the recommended code helps with the issue of sorting out filetypes when tested outside of the application, but anytime its integrated into the application to operate on the variable uploadedfilenames the rest of the application fails and the multi-file upload module just throws a status 500 error and no errors are reported in the CF logs. I've found that simply trying to run a cfloop on another variable not related to anything in the code still causes it to error.
As per my understanding, you want to filter-out file names with a specific file type/extension (ex: pdf) from the main list uploadedfilenames. This is one of the easiest ways:
<cfset lFileNames = "C:\myfiles\proj\icon-img-12.png,C:\myfiles\proj\sample-file.ppt,C:\myfiles\proj\fin-doc1.docx,C:\myfiles\proj\fin-doc2.pdf,C:\myfiles\proj\invoice-temp.docx,C:\myfiles\proj\invoice-final.pdf" />
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#lFileNames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,"#fileItem#") />
</cfif>
</cfloop>
Using only List Function provided by ColdFusion this is easily done, you can test and try the code here. I would recommend you to wrap this code around a function for easy handling. Another way to do it would be to use some complex regular expression on the list (if you're looking for a more general solution, outside the context of ColdFusion).
Now, applying the solution to your problem:
<cfset uploadedfilenames='#myfiles.clientFile#' >
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#uploadedfilenames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,fileItem) />
</cfif>
</cfloop>
<cfset uploadedfilenames = lResultList />
<!--- rest of your code continues --->
The result list lResultList is copied to the original variable uploadedfilenames.
I hope I'm not misunderstanding the question, but why don't you just wrap all of that in an if-statement that reads the full file name? Whether the files are coming one by one or through a delimited list, it should be easy to work around.
<cfif !listContains(ListName, '.pdf')>
OR
<cfif FileName does not contain '.pdf'>
then
all the code you posted

How to access the URL parameter id in Coldfusion if it's after a hashtag #?

After trying for about an hour without success... (coldfusion8) a dummy question, but I*m stuck:
My URL (Jquery Mobile, no pushstate, that's why it looks like it is):
http://www.page.de/test/mem/search.cfm#/test/mem/search.cfm?id=9900000003869
If I output:
<cfdump output="e:\website\dump.txt" label="catch" var="#url#">
I get this:
catch - struct
ID: 9900000003869
But how do i access it... I'm trying forever, nothing works:
<cfdump output="e:\website\dump.txt" label="catch" var="#id#">
<cfdump output="e:\website\dump.txt" label="catch" var="#ID#">
<cfdump output="e:\website\dump.txt" label="catch" var="#url.id#">
<cfdump output="e:\website\dump.txt" label="catch" var="#url.ID#">
<cfdump output="e:\website\dump.txt" label="catch" var="#StructGetValue(url,"id")d#">
...
Thanks for helping!
Ok... This works:
URL = http://www.page.de/test/mem/search.cfm#/test/mem/search.cfm?id=9900000003869
<cfset objRequest = GetPageContext().GetRequest() />
<cfset strUrl = right( objRequest.GetRequestUrl().Append( "?" & objRequest.GetQueryString() ).ToString(), 13)>
Credit
If someone finds an easier, please post. I will check as answer.
You're trying to read this from a txt file?
Can you not simply use:
<cfdump label="catch" var="#url.id#" />
Does that work?
EDIT:
Could you try capturing and formatting what you need first then after, writing it to the file?
For example, try using:
<cfsavecontent variable="myFileContents">
<cfoutput>#url.id#</cfoutput>
</cfsavecontent>
<cffile action="Write" file="e:\website\dump.txt" output="#myFileContents#" />
I have not tested this code, but give it a go and see!
Might want to put a check on that URL variable too using isDefined()
Good luck.
Doing some research on fragment identifiers (which is a new term to me :( )prompted by Peter and Duncan's comments, I've found from wiki: http://en.wikipedia.org/wiki/Fragment_identifier
The fragment identifier functions differently than the rest of the
URI: namely, its processing is exclusively client-side with no
participation from the server — of course the server typically helps
to determine the MIME type, and the MIME type determines the
processing of fragments. When an agent (such as a Web browser)
requests a resource from a Web server, the agent sends the URI to
the server, but does not send the fragment. Instead, the agent waits
for the server to send the resource, and then the agent processes the
resource according to the document type and fragment value.
now, being your client IS sending the fragment and the url variable is accessible to you for some reason, using it is done by my original post to follow.
<cfoutput>
is generally how you output a variable or other evaluations to the screen.
<cfset myName = "Travis">
<cfoutput>Hello, my name is #myName#</cfoutput>
You can also access the variable by using it in a statement that doesn't output anywhere.
<cfset myFullName = myName & " Mak">
You can also use the variables in a query
<cfquery name = "qSomeQuery" datasource = "#application.dsn#">
select * from table where id = #url.id#
</cfquery>
However, that's the bad way to use it in a query, you should always use cfquery param.
<cfquery name = "qSomeQuery" datasource = "#application.dsn#">
select * from table where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.id#">
</cfquery>
The problem you're having in testing the variable is due to incorrect syntax.
<cfif isDefined("url.id")> verses <cfif isDefined(url.id)> a more accurate test is <cfif structKeyExists(url, "id")>
For some reason my CF server truncates everything in the url after the # but yours doesn't seem to have this problem. As your cfdump states, you can see your url variables so "accessing" the url variable is as easy as using it: #url.id# or testing it <cfif isDefined("url.id")>

preserving case for coldfusion structs when converted to JSON

Using the array notation for creating the keys in a struct used to preserve the case of the key.
<cfset var response = structNew()>
<cfset response[ "error" ] = 0>
<cfset response[ "message" ] = "">
<!--- this worked when converting this struct to a JSON
So when I actually put some data into the struct:
<cfset response.error = 1>
<cfset response.message = "There was an error inserting the record...">
I surprised when I saw this come across the firebug console:
{"MESSAGE":"There was an error inserting the record...","ERROR":1}
note, the information is what I was expecting to see.
I understand if I would have used <cfset response.error = 0>, for the case not to be preserved when converting it to a JSON, but this has worked for me in the past. Am I doing something wrong that I'm blind to or did something change in CF10?
EDIT:
Before all of the responses came back, I tried what everyone said.
<cfset response["error"] = 1>
<cfset response["message"] = "There was an error inserting the record...">
and it worked. So, I'm guessing they changed how serializeJSON works in CF10.
Good to know.
If you continue to reference the struct key with bracket notation rather than dot notation, you should have no issue with case when using serializeJSON().
After testing on CF9, however, it seems to work fine whether I use bracket- or dot-notation. This may be a difference between CF9 and 10 and, as Ray recommends, you may want to file a bug report for it.
Here's what I did in CF9:
<cfset response = structNew()>
<cfset response[ "error" ] = 0>
<cfset response[ "message" ] = "">
<!--- this worked when converting this struct to a JSON --->
<cfoutput>#serializeJSON(response)#</cfoutput><br /><br />
<!--- This outputs {"message":"","error":0} --->
<cfset response.error = 1>
<cfset response.message = "There was an error inserting the record...">
<cfoutput>#serializeJSON(response)#</cfoutput>
<!--- This outputs {"message":"There was an error inserting the record...","error":1} --->
I believe if you continue to use bracket notation it will continue to work ok. A bit more typing I guess, but should work. If this is a change from CF9 though you may want to file a bug report.

ImageNew toBase64 encoding issue with loss of quality in ColdFusion

I have been having problems with toBase64() for awhile. I am hoping someone can tell me why CF toBase64() seems to lost something i.e. in my example it reduces the quality of an image.
I have a solution (see last code example below), but I hate not understanding why and would love to solve this is CF.
If anyone would be so kind to run the code below, you would then see that after toBase64 conversion the image quality is bad. Nothing major, but it does not look as good after the encoding. If you have never noticed, then try it, you will see what I mean.
Does anyone know why, or how to solve this in CF?
<!--- EXAMPLE 1 --->
<!--- GET IMAGE - --->
<cfset image = ImageNew("test.png")>
<!--- BEFORE GOOD--->
<cfimage action="writeToBrowser" source="#image#" >
<cfset image = toBinary(toBase64(image)) />
<!--- AFTER --->
<cfimage action="writeToBrowser" source="#image#" >
<!--- Example 2 --->
<cfset image = ImageNew("test.png")>
<cfset FileWrite(expandPath('./converted.image'),toBinary(toBase64(image))) />
<!--- without any cfimage processing, the outputted file is a JPEG --->
My solution was to use a java add-on and everything seemed ok but for reasons I won't go into here not something I can do live.
image = createObject("java","it.sauronsoftware.base64.Base64").encode(image);
toBinary(image );
Sample image output of code above can be found here: http://i56.tinypic.com/29fwiq.png
First is before toBase64 second is after, you can see the image has lost a bit of quality after toBase64 function on the second output.
Update: As pointed out by Peter, the issue seems to be with the automatic output/conversion code within the ImageObject to provide the binary output for the toBase64 function to encode.
Update I have submitted this as a bug in CF 9.0.1, please vote for bug 3177303
https://bugbase.adobe.com/index.cfm?event=bug&id=3177303
use toBase64(imageGetBlob(myImg))
see: http://blog.dkferguson.com/index.cfm/2010/4/27/All-your-base64-are-not-equal
I see no one has mentioned the imageWriteBase64() function that has been in ColdFusion since version 8. I am not sure why.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-796b.html
I have been using it this week for the first time and it seems to be going great. I have not noticed any problems with quality loss.
<cfdirectory action="list" directory="#expandPath('./images')#" name="imageDir" type="file" />
<cfloop query="imageDir">
<cfset ext = listLast(imageDir.name, ".") />
<cfset name = imageDir.name />
<cfset imagePath = imageDir.directory & "/" & name />
<cfset imageFile = imageNew(imagePath) />
<cfset imageWriteBase64(imageFile,"#expandPath('./base64')#/#name#.txt",ext, true) />
</cfloop>