Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the file MIME type by content?

Tags:

haskell

mime

Is there some way to determine the MIME type of a file by it's content ? Maybe with some Haskell library ?

like image 455
s9gf4ult Avatar asked Feb 23 '13 19:02

s9gf4ult


People also ask

Is MIME type same as content-type?

content_type is an alias for mimetype. Historically, this parameter was only called mimetype, but since this is actually the value included in the HTTP Content-Type header, it can also include the character set encoding, which makes it more than just a MIME type specification.

What is content MIME type?

Content Type is also known as MIME (Multipurpose internet Mail Extension)Type. It is a HTTP header that provides the description about what are you sending to the browser.

How do I specify a MIME type?

A MIME type has two parts: a type and a subtype. They are separated by a slash (/). For example, the MIME type for Microsoft Word files is application and the subtype is msword. Together, the complete MIME type is application/msword.

How to find the MIME type of a file?

Another way to get the MIME type of a file is by reading its content. We can determine the MIME type according to specific characteristics of the file content. For example, a JPG starts with the hex signature FF D8 and ends with FF D9. This is slower than the file extension approach due to the extra I/O efforts. However, it can be more reliable.

What are the MIME types in Python?

The MIME types provide the name which will be used to identify each file type. Developers many times do not know the MIME type of the file and need it to be determined by itself. Python provides a module named mimetypes that provides a list of methods that has a mapping from file extensions to MIME type and vice-versa.

What is a subtype in mime?

In MIME types, the type and subtype are case-insensitive. A subtype usually consists of a media format, such as “ xml ” or “ pdf ” in the above example. However, it can contain other content as well, such as a tree prefix or suffix, depending on the different rules in registration trees. A complete MIME type format looks like:

What are MIME types and why should Linux users care?

If you are a Linux user whose intuitions are strongly allied with web technology then the concept of MIME types should be imprinted in your DNA. MIME types help identify file formats and formatted contents during their transmission across the internet or any other user-defined network.


1 Answers

Haskell bindings to libmagic might be a solution to your problem. Here's an example.

import Magic
import System.Environment (getArgs)

main =  do
  magic <- magicOpen [MagicMime]
  (file:_) <- getArgs
  magicLoadDefault magic
  mime <- magicFile magic file
  putStrLn mime
like image 81
Jan Avatar answered Sep 21 '22 06:09

Jan