trivial mimes
1.1.0Tiny library to detect mime types in files.
Table of Contents
About Trivial-Mimes
This is a teensy library that provides some functions to determine the mime-type of a file. As I've had a need for this kind of functionality more than once now and haven't found any suitably lightweight alternative I quickly whipped this up.
How To
(mimes:mime #p"~/something.png")
Upon loading, trivial-mimes builds a mime-type database from the local mime.types
file or a copy thereof from its own source directory. This database is a simple association of file extension to mime-type (see mime-lookup
). If the mime lookup in the database fails, it will instead try to consult the file
shell utility on unix systems (see mime-probe
). If that too doesn't work, it falls back onto a default.
System Information
Definition Index
-
TRIVIAL-MIMES
- MIMES
- ORG.TYMOONNEXT.TRIVIAL-MIMES
No documentation provided.-
EXTERNAL SPECIAL-VARIABLE *MIME-DB*
An EQUALP hash-table with file-extensions as keys and the mime-types as values.
-
EXTERNAL FUNCTION FIND-MIME.TYPES
Attempts to find a usable MIME.TYPES file. If none can be found, an error is signalled.
-
EXTERNAL FUNCTION MIME
- PATHNAME
- &OPTIONAL
- DEFAULT
- &REST
Attempts to detect the mime-type of the given pathname. First uses MIME-LOOKUP, then MIME-PROBE and lastly returns the DEFAULT if both fail.
-
EXTERNAL FUNCTION MIME-ADD
- MIME
- &REST
- FILE-EXTENSIONS
- &REST
Add MIME and FILE-EXTENSIONS associations to *MIME-DB* and *REVERSE-MIME-DB*. Makes the provided MIME and FILE-EXTENSIONS properly look-up-able with MIME, MIME-PROBE and other trivial-mimes functions.
-
EXTERNAL FUNCTION MIME-EQUAL
- M1
- M2
- &REST
Checks whether M1 and M2 are matching. In particular, checks the match of type and subtype (any of which can be asterisks), discarding any parameters there might be. (mime-equal "text/*" "text/html") T (mime-equal "text/html" "text/html;charset=utf8") T (mime-equal "*/*" "application/octet-stream") T (mime-equal "text/*" "application/octet-stream") NIL
-
EXTERNAL FUNCTION MIME-FILE-TYPE
- MIME-TYPE
- &REST
Returns a matching file-extension for the given mime-type. If the given mime-type cannot be found, NIL is returned.
-
EXTERNAL FUNCTION MIME-LOOKUP
- PATHNAME
- &REST
Attempts to get the mime-type by file extension comparison. If none can be found, NIL is returned.
-
EXTERNAL FUNCTION MIME-PROBE
- PATHNAME
- &REST
Attempts to get the mime-type through a call to the FILE shell utility. If the file does not exist or the platform is not unix, NIL is returned.
-
EXTERNAL MACRO MIME-CASE
- FILE
- &BODY
- CASES
- &REST
A case-like macro that works with MIME type of FILE. Otherwise clause is the last clause that starts with T or OTHERWISE,. Example: (mime-case #p"~/CHANGES.txt" (("application/json" "application/*") "Something opaque...") ("text/plain" "That's a plaintext file :D") (t "I don't know this type!"))