🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] > https://github.com/gabriel-vasile/mimetype ## 概述 * 检测文件真是类型,**不会因为修改后缀名而变化** * 快速、精确的 MIME 类型和文件扩展名检测 * [支持的 MIME 类型](https://github.com/gabriel-vasile/mimetype/blob/master/supported_mimes.md)的长列表[](https://github.com/gabriel-vasile/mimetype/blob/master/supported_mimes.md) * 可以[扩展](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-Extend)其他文件格式 * 通用文件格式优先 * [文本文件与二进制文件的区别](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-TextVsBinary) * 并发使用安全 ## 示例 ### hello world ``` 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()) ``` ### 设置白名单 ``` package main import ( "fmt" "github.com/gabriel-vasile/mimetype" ) func main() { testBytes := []byte("This random text has a MIME type of text/plain; charset=utf-8.") allowed := []string{"text/plain", "application/zip", "application/pdf"} mtype := mimetype.Detect(testBytes) if mimetype.EqualsAny(mtype.String(), allowed...) { fmt.Printf("%s is allowed\n", mtype) } else { fmt.Printf("%s is now allowed\n", mtype) } } ```