https://github.com/gabriel-vasile/mimetype
A fast Golang library for media type and file extension detection, based on magic numbers
https://github.com/gabriel-vasile/mimetype
detection go golang magic-numbers media-types mime mimetype sniffing
Last synced: about 2 months ago
JSON representation
A fast Golang library for media type and file extension detection, based on magic numbers
- Host: GitHub
- URL: https://github.com/gabriel-vasile/mimetype
- Owner: gabriel-vasile
- License: mit
- Created: 2018-07-02T07:15:29.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-05-06T11:34:12.000Z (about 2 months ago)
- Last Synced: 2025-05-07T07:00:44.885Z (about 2 months ago)
- Topics: detection, go, golang, magic-numbers, media-types, mime, mimetype, sniffing
- Language: Go
- Homepage: https://pkg.go.dev/github.com/gabriel-vasile/mimetype#pkg-overview
- Size: 23.4 MB
- Stars: 1,788
- Watchers: 18
- Forks: 168
- Open Issues: 43
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Support: supported_mimes.md
Awesome Lists containing this project
- awesome-golang-repositories - mimetype
- awesome-go-extra - mimetype - 07-02T07:15:29Z|2022-08-24T11:28:14Z| (Utilities / Fail injection)
- awesome-go - mimetype - Package for MIME type detection based on magic numbers. Stars:`1.8K`. (Utilities / Utility/Miscellaneous)
- awesome-go - mimetype - A golang library for detecting mime types and extensions based on magic numbers - ★ 42 (Utilities)
README
mimetype
A package for detecting MIME types and extensions based on magic numbers
Goroutine safe, extensible, no C bindings## Features
- fast and precise MIME type and file extension detection
- long list of [supported MIME types](supported_mimes.md)
- possibility to [extend](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-Extend) with other file formats
- common file formats are prioritized
- [text vs. binary files differentiation](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-TextVsBinary)
- safe for concurrent usage## Install
```bash
go get github.com/gabriel-vasile/mimetype
```## Usage
```go
mtype := mimetype.Detect([]byte)
// OR
mtype, err := mimetype.DetectReader(io.Reader)
// OR
mtype, err := mimetype.DetectFile("/path/to/file")
fmt.Println(mtype.String(), mtype.Extension())
```
See the [runnable Go Playground examples](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#pkg-overview).Caution: only use libraries like **mimetype** as a last resort. Content type detection
using magic numbers is slow, inaccurate, and non-standard. Most of the times
protocols have methods for specifying such metadata; e.g., `Content-Type` header
in HTTP and SMTP.## FAQ
Q: My file is in the list of [supported MIME types](supported_mimes.md) but
it is not correctly detected. What should I do?A: Some file formats (often Microsoft Office documents) keep their signatures
towards the end of the file. Try increasing the number of bytes used for detection
with:
```go
mimetype.SetLimit(1024*1024) // Set limit to 1MB.
// or
mimetype.SetLimit(0) // No limit, whole file content used.
mimetype.DetectFile("file.doc")
```
If increasing the limit does not help, please
[open an issue](https://github.com/gabriel-vasile/mimetype/issues/new?assignees=&labels=&template=mismatched-mime-type-detected.md&title=).## Structure
**mimetype** uses a 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 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](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#DetectReader)
or from a [file](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#DetectFile)
**mimetype** limits itself to reading only the header of the input.
![]()
## Benchmarks
Benchmarks for each file format are performed when a PR is open. The results can
be seen on the [workflows page](https://github.com/gabriel-vasile/mimetype/actions/workflows/benchmark.yml).
Performance improvements are welcome but correctness is prioritized.## Contributing
Contributions are unexpected but welcome. When submitting a PR for detection of
a new file format, please make sure to add a record to the list of testcases
from [mimetype_test.go](mimetype_test.go). For complex files a record can be added
in the [testdata](testdata) directory.