go get github.com/gabriel-vasile/mimetype
There are quick examples and GoDoc for full reference.
In v1.x the detect functions no longer return the MIME type and extension as
strings. Instead they return a MIME
struct. To get the string value of the MIME and the extension, call the
String()
and the Extension()
methods.
In order to play better with the stdlib mime
package, v1.x file extensions
include the leading dot, as in ".html".
In v1.x the text/plain
MIME type is text/plain; charset=utf-8
.
See supported mimes for the list of detected MIME types. If support is needed for a specific file format, please open an issue.
mimetype uses an hierarchical structure to keep the MIME type detection logic. This reduces the number of calls needed for detecting the file type. The reason behind this choice is that there are file formats used as containers for other file formats. For example, Microsoft Office files are just zip archives, containing specific metadata files. Once a file a file has been identified as a zip, there is no need to check if it is a text file, but it is worth checking if it is an Microsoft Office file.
To prevent loading entire files into memory, when detecting from a reader or from a file mimetype limits itself to reading only the first 3072 bytes from the input.
See CONTRIBUTING.md.