Is there some way to determine the MIME type of a file by it's content ? Maybe with some Haskell library ?
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.
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.
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.
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.
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.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With