Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a file mimetype in coldfusion that's already uploaded on the server

I am attempting to detect the file type of a library of files on our webserver as we are implementing code that is designed to stream files to the browser securely. Previously, the files were being stored and presented to users via a direct href.

I have attempted to do this 3 different ways, all on my local machine (which is NOT a simulated production environment):

  1. Setting a variable to be the value of what is returned from the function getPageContext().getServletContext().getMimeType(). This detects some but not all mime types for files.

  2. Creating an object from coldfusion.util.MimeTypeUtils and calling function guessMimeType(). This also detects some but not all mime types for files.

  3. A cffile action="read" on files in the library. This is the solution my boss recommended, as he has used this on files with cffile action="upload" from a form (and says it works), but when I use it, the cffile structure is always blank.

Ideally, I want to retrieve the mime type of every file located on the server with 100% accuracy. The code I have written has detected approximately 99% of the files on my copy of the repo, leaving about 30 that it can't identify. Included in these are MS office files with the new -x extension, and tgz compressed files.

I am wondering if there is there a sure-fire way to detect the mime-types of any given file that exists on a server by using CF code to look at it, and will the code that's being used work on a production server where very few applications are installed? It is my understanding that the first function I referenced uses the mime-type library of the OS, and the 2nd uses a predetermined list in the java object for mime-types. Searching on Google and SO has not produced anything that tells me that CF can accurately detect file mime types on it's own, nor have I seen anything that says this can't be done.

Edit: This is on a CF8 environment.

like image 386
K_Cruz Avatar asked Sep 19 '10 23:09

K_Cruz


People also ask

How do I find the MIME type of a file?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

Where is MIME type stored in a file?

All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop. org.

What is the MIME type of a file?

A media type (also known as a MIME type) is a two-part identifier for file formats and format contents transmitted on the Internet. The Internet Assigned Numbers Authority (IANA) is the official authority for the standardization and publication of these classifications.

Can MIME type be faked?

Context. used with (i.e. popular names of modules): any other relevant information: Formidable detects MIME-type according to the file extension and not by the real content. Which means that user can fake the file MIME by changing file's extension and as a result to upload to the server not allowed file types.


2 Answers

There will not be a 100% guaranteed sure-fire way because mime types are arbitrary mappings.

You will need to use somebody's mappings, whether its the OS or the JVM.

It will be your responsibility to fill in any blanks that either the OS or the JVM has in mappings, and keep that up to date.

But, I will always be able to create some file, give it an extension of .xyzzy, and you'll not be able to find out the 'mime-type' of it.

like image 123
Edward M Smith Avatar answered Oct 19 '22 07:10

Edward M Smith


I know this is a very old question, but the answers offered were from the era of CF9 and earlier. To clarify for those using CF10 and above, there is a filegetmimetype function now.

And note that it does NOT just look at the file extension, but by default also assesses some of the content to make sure the content matches the file extension and mime type that would imply. See the "strict" argument in the function.

like image 39
charlie arehart Avatar answered Oct 19 '22 05:10

charlie arehart