mime
This module provides functions that allow easy mime type detection from files. It offers support for detecting file type based on name or file headers and it is completely extensible so that you can add declarations for your own custom file types.
See defined functions for example.
Functions
- mime.detect_from_name(name: string)
- Detects the mimetype of a file based on the extension defined in it’s path. return string
- For popular files such as Jpeg and Pngs, calling this method directly is more efficient and provides a faster lookup.
Example,
import mime echo mime.detect_from_name('myimage.png')
- mime.detect_from_header(file: file)
- Detects the mimetype of a file based on it’s file header.
When multiple file formats share very similar or shadowing file headers (such as the relationship between Zip files and Docx files), this method will perform an extension before returning it’s result.
return string- For dealing with files without extension, or where the accuracy of the file extension cannot be trusted, this method provides a more efficient lookup.
- This method may produce slightly more rigorous results
- This method requires that the file must be opened in binary mode
Example,
import mime var f = file('my_file.ext', 'rb') echo mime.detect_from_header(f)
- mime.detect(file: file)
- Performs mimetype detection on a file.
this method is capable of detecting file mimetypes even in the abscence of an extension.
If the file is opened in binary mode, it first attempt the more accurate header check. If the header check returns a generic result (i.e. application/octet-stream), it performs an extension lookup.
return string- this method gives the best result, but slightly slower than a direct lookup of name or header.
Example,
import mime var f = file('myfile', 'rb') # using 'rb' here for two reasons: # 1. Our file has no extension, so extension based detection is impossible # 2. We want more accuracy by having Mime check file headers echo mime.detect(f)
- mime.extend(extension: string, format: MimeFormat)
- Extends the mime module with support for files with the given extension as defined in the given format. return bool
- the extension MUST start with
.
Example,
%> import mime %> mime.detect_from_name('myfile.ppk') 'application/octet-stream' %> mime.extend('.ppk', mime.MimeFormat('file/ppk')) true %> mime.detect_from_name('myfile.ppk') 'file/ppk'
- the extension MUST start with
Classes
class MimeFormat
Mime format representation class.
class MimeFormat methods
- MimeFormat(mimetype: string [, header: list | bytes])
- constructor
- only the first 16 bytes of a file header will be used.