百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mimetype

> 编程语言
开源

一个基于魔数的媒体类型和文件扩展名检测 Go 包

2.0K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个基于魔数的媒体类型和文件扩展名检测 Go 包

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) - no external dependencies - 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=). ## Tests In addition to unit tests, [mimetype_tests](https://github.com/gabriel-vasile/mimetype_tests) compares the library with [libmagic](https://en.wikipedia.org/wiki/File_(command)) for around 50 000 sample files. Check the latest comparison results [here](https://github.com/gabriel-vasile/mimetype_tests/actions). ## Benchmarks Benchmarks 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. ## 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. ## Contributing Contributions are never expected but very much welcome. [mimetype_tests](https://github.com/gabriel-vasile/mimetype_tests/actions/workflows/test.yml) shows which file formats are most often misidentified and can help prioritise. When submitting a PR for detection of a new file format, please make sure to add a record to the list of testcases in [mimetype_test.go](mimetype_test.go). For complex files a record can be added in the [testdata](testdata) directory. Code contributions must respect following rules: - code must be test covered - code must be formatted using the `gofmt` tool - exported names must be documented **Important**: By submitting a pull request, you agree to allow the project owner to license your work under the same license as that used by the project.

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •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
  • •code must be test covered
  • •code must be formatted using the gofmt tool
  • •exported names must be documented

> 标签

Godetectiongogolangmagic-numbers

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言