Socket
Book a DemoInstallSign in
Socket

github.com/gabriel-vasile/mimetype

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gabriel-vasile/mimetype

Source
Go
Version
v1.4.10
Version published
Created
Source

mimetype

A package for detecting MIME types and extensions based on magic numbers

Goroutine safe, extensible, no C bindings

Go Reference Go report card License

Features

  • fast and precise MIME type and file extension detection
  • long list of supported MIME types
  • possibility to extend with other file formats
  • common file formats are prioritized
  • text vs. binary files differentiation
  • no external dependencies
  • safe for concurrent usage

Install

go get github.com/gabriel-vasile/mimetype

Usage

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.

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 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:

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.

Tests

In addition to unit tests, mimetype_tests compares the library with the Unix file utility for around 50 000 sample files. Check the latest comparison results here.

Benchmarks

Benchmarks for each file format are performed when a PR is open. The results can be seen on the workflows page. Performance improvements are welcome but correctness is prioritized.

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 or from a file mimetype limits itself to reading only the header of the input.

how project is structured

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. For complex files a record can be added in the testdata directory.

FAQs

Package last updated on 24 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts