Debian Codegraph @

README.md

raw symbols SCIP
1 <h1 align="center">
2 mimetype
3 </h1>
4
5 <h4 align="center">
6 A package for detecting MIME types and extensions based on magic numbers
7 </h4>
8 <h6 align="center">
9 Goroutine safe, extensible, no C bindings
10 </h6>
11
12 <p align="center">
13 <a href="https://pkg.go.dev/github.com/gabriel-vasile/mimetype">
14 <img alt="Go Reference" src="https://pkg.go.dev/badge/github.com/gabriel-vasile/mimetype.svg">
15 </a>
16 <a href="https://goreportcard.com/report/github.com/gabriel-vasile/mimetype">
17 <img alt="Go report card" src="https://goreportcard.com/badge/github.com/gabriel-vasile/mimetype">
18 </a>
19 <a href="LICENSE">
20 <img alt="License" src="https://img.shields.io/badge/License-MIT-green.svg">
21 </a>
22 </p>
23
24 ## Features
25 - fast and precise MIME type and file extension detection
26 - long list of [supported MIME types](supported_mimes.md)
27 - possibility to [extend](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-Extend) with other file formats
28 - common file formats are prioritized
29 - [text vs. binary files differentiation](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#example-package-TextVsBinary)
30 - no external dependencies
31 - safe for concurrent usage
32
33 ## Install
34 ```bash
35 go get github.com/gabriel-vasile/mimetype
36 ```
37
38 ## Usage
39 ```go
40 mtype := mimetype.Detect([]byte)
41 // OR
42 mtype, err := mimetype.DetectReader(io.Reader)
43 // OR
44 mtype, err := mimetype.DetectFile("/path/to/file")
45 fmt.Println(mtype.String(), mtype.Extension())
46 ```
47 See the [runnable Go Playground examples](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#pkg-overview).
48
49 Caution: only use libraries like **mimetype** as a last resort. Content type detection
50 using magic numbers is slow, inaccurate, and non-standard. Most of the times
51 protocols have methods for specifying such metadata; e.g., `Content-Type` header
52 in HTTP and SMTP.
53
54 ## FAQ
55 Q: My file is in the list of [supported MIME types](supported_mimes.md) but
56 it is not correctly detected. What should I do?
57
58 A: Some file formats (often Microsoft Office documents) keep their signatures
59 towards the end of the file. Try increasing the number of bytes used for detection
60 with:
61 ```go
62 mimetype.SetLimit(1024*1024) // Set limit to 1MB.
63 // or
64 mimetype.SetLimit(0) // No limit, whole file content used.
65 mimetype.DetectFile("file.doc")
66 ```
67 If increasing the limit does not help, please
68 [open an issue](https://github.com/gabriel-vasile/mimetype/issues/new?assignees=&labels=&template=mismatched-mime-type-detected.md&title=).
69
70 ## Tests
71 In addition to unit tests,
72 [mimetype_tests](https://github.com/gabriel-vasile/mimetype_tests) compares the
73 library with [libmagic](https://en.wikipedia.org/wiki/File_(command))
74 for around 50 000 sample files. Check the latest comparison results
75 [here](https://github.com/gabriel-vasile/mimetype_tests/actions).
76
77 ## Benchmarks
78 Benchmarks are performed when a PR is open. The results can be seen on the
79 [workflows page](https://github.com/gabriel-vasile/mimetype/actions/workflows/benchmark.yml).
80 Performance improvements are welcome but correctness is prioritized.
81
82 ## Structure
83 **mimetype** uses a hierarchical structure to keep the MIME type detection logic.
84 This reduces the number of calls needed for detecting the file type. The reason
85 behind this choice is that there are file formats used as containers for other
86 file formats. For example, Microsoft Office files are just zip archives,
87 containing specific metadata files. Once a file has been identified as a
88 zip, there is no need to check if it is a text file, but it is worth checking if
89 it is an Microsoft Office file.
90
91 To prevent loading entire files into memory, when detecting from a
92 [reader](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#DetectReader)
93 or from a [file](https://pkg.go.dev/github.com/gabriel-vasile/mimetype#DetectFile)
94 **mimetype** limits itself to reading only the header of the input.
95 <div align="center">
96 <img alt="how project is structured" src="https://raw.githubusercontent.com/gabriel-vasile/mimetype/master/testdata/gif.gif" width="88%">
97 </div>
98
99 ## Contributing
100 Contributions are never expected but very much welcome.
101 [mimetype_tests](https://github.com/gabriel-vasile/mimetype_tests/actions/workflows/test.yml)
102 shows which file formats are most often misidentified and can help prioritise.
103 When submitting a PR for detection of a new file format, please make sure to
104 add a record to the list of testcases in [mimetype_test.go](mimetype_test.go).
105 For complex files a record can be added in the [testdata](testdata) directory.