| 1 | # filetype [](https://travis-ci.org/h2non/filetype) [](https://godoc.org/github.com/h2non/filetype) [](http://goreportcard.com/report/h2non/filetype) [](https://github.com/h2non/gentleman) |
| 2 | |
| 3 | Small and dependency free [Go](https://golang.org) package to infer file and MIME type checking the [magic numbers](<https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files>) signature. |
| 4 | |
| 5 | For SVG file type checking, see [go-is-svg](https://github.com/h2non/go-is-svg) package. Python port: [filetype.py](https://github.com/h2non/filetype.py). |
| 6 | |
| 7 | ## Features |
| 8 | |
| 9 | - Supports a [wide range](#supported-types) of file types |
| 10 | - Provides file extension and proper MIME type |
| 11 | - File discovery by extension or MIME type |
| 12 | - File discovery by class (image, video, audio...) |
| 13 | - Provides a bunch of helpers and file matching shortcuts |
| 14 | - [Pluggable](#add-additional-file-type-matchers): add custom new types and matchers |
| 15 | - Simple and semantic API |
| 16 | - [Blazing fast](#benchmarks), even processing large files |
| 17 | - Only first 262 bytes representing the max file header is required, so you can just [pass a slice](#file-header) |
| 18 | - Dependency free (just Go code, no C compilation needed) |
| 19 | - Cross-platform file recognition |
| 20 | |
| 21 | ## Installation |
| 22 | |
| 23 | ```bash |
| 24 | go get github.com/h2non/filetype |
| 25 | ``` |
| 26 | |
| 27 | ## API |
| 28 | |
| 29 | See [Godoc](https://godoc.org/github.com/h2non/filetype) reference. |
| 30 | |
| 31 | ### Subpackages |
| 32 | |
| 33 | - [`github.com/h2non/filetype/types`](https://godoc.org/github.com/h2non/filetype/types) |
| 34 | - [`github.com/h2non/filetype/matchers`](https://godoc.org/github.com/h2non/filetype/matchers) |
| 35 | |
| 36 | ## Examples |
| 37 | |
| 38 | #### Simple file type checking |
| 39 | |
| 40 | ```go |
| 41 | package main |
| 42 | |
| 43 | import ( |
| 44 | "fmt" |
| 45 | "io/ioutil" |
| 46 | |
| 47 | "github.com/h2non/filetype" |
| 48 | ) |
| 49 | |
| 50 | func main() { |
| 51 | buf, _ := ioutil.ReadFile("sample.jpg") |
| 52 | |
| 53 | kind, _ := filetype.Match(buf) |
| 54 | if kind == filetype.Unknown { |
| 55 | fmt.Println("Unknown file type") |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | fmt.Printf("File type: %s. MIME: %s\n", kind.Extension, kind.MIME.Value) |
| 60 | } |
| 61 | ``` |
| 62 | |
| 63 | #### Check type class |
| 64 | |
| 65 | ```go |
| 66 | package main |
| 67 | |
| 68 | import ( |
| 69 | "fmt" |
| 70 | "io/ioutil" |
| 71 | |
| 72 | "github.com/h2non/filetype" |
| 73 | ) |
| 74 | |
| 75 | func main() { |
| 76 | buf, _ := ioutil.ReadFile("sample.jpg") |
| 77 | |
| 78 | if filetype.IsImage(buf) { |
| 79 | fmt.Println("File is an image") |
| 80 | } else { |
| 81 | fmt.Println("Not an image") |
| 82 | } |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | #### Supported type |
| 87 | |
| 88 | ```go |
| 89 | package main |
| 90 | |
| 91 | import ( |
| 92 | "fmt" |
| 93 | |
| 94 | "github.com/h2non/filetype" |
| 95 | ) |
| 96 | |
| 97 | func main() { |
| 98 | // Check if file is supported by extension |
| 99 | if filetype.IsSupported("jpg") { |
| 100 | fmt.Println("Extension supported") |
| 101 | } else { |
| 102 | fmt.Println("Extension not supported") |
| 103 | } |
| 104 | |
| 105 | // Check if file is supported by extension |
| 106 | if filetype.IsMIMESupported("image/jpeg") { |
| 107 | fmt.Println("MIME type supported") |
| 108 | } else { |
| 109 | fmt.Println("MIME type not supported") |
| 110 | } |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | #### File header |
| 115 | |
| 116 | ```go |
| 117 | package main |
| 118 | |
| 119 | import ( |
| 120 | "fmt" |
| 121 | "io/ioutil" |
| 122 | |
| 123 | "github.com/h2non/filetype" |
| 124 | ) |
| 125 | |
| 126 | func main() { |
| 127 | // Open a file descriptor |
| 128 | file, _ := os.Open("movie.mp4") |
| 129 | |
| 130 | // We only have to pass the file header = first 261 bytes |
| 131 | head := make([]byte, 261) |
| 132 | file.Read(head) |
| 133 | |
| 134 | if filetype.IsImage(head) { |
| 135 | fmt.Println("File is an image") |
| 136 | } else { |
| 137 | fmt.Println("Not an image") |
| 138 | } |
| 139 | } |
| 140 | ``` |
| 141 | |
| 142 | #### Add additional file type matchers |
| 143 | |
| 144 | ```go |
| 145 | package main |
| 146 | |
| 147 | import ( |
| 148 | "fmt" |
| 149 | |
| 150 | "github.com/h2non/filetype" |
| 151 | ) |
| 152 | |
| 153 | var fooType = filetype.NewType("foo", "foo/foo") |
| 154 | |
| 155 | func fooMatcher(buf []byte) bool { |
| 156 | return len(buf) > 1 && buf[0] == 0x01 && buf[1] == 0x02 |
| 157 | } |
| 158 | |
| 159 | func main() { |
| 160 | // Register the new matcher and its type |
| 161 | filetype.AddMatcher(fooType, fooMatcher) |
| 162 | |
| 163 | // Check if the new type is supported by extension |
| 164 | if filetype.IsSupported("foo") { |
| 165 | fmt.Println("New supported type: foo") |
| 166 | } |
| 167 | |
| 168 | // Check if the new type is supported by MIME |
| 169 | if filetype.IsMIMESupported("foo/foo") { |
| 170 | fmt.Println("New supported MIME type: foo/foo") |
| 171 | } |
| 172 | |
| 173 | // Try to match the file |
| 174 | fooFile := []byte{0x01, 0x02} |
| 175 | kind, _ := filetype.Match(fooFile) |
| 176 | if kind == filetype.Unknown { |
| 177 | fmt.Println("Unknown file type") |
| 178 | } else { |
| 179 | fmt.Printf("File type matched: %s\n", kind.Extension) |
| 180 | } |
| 181 | } |
| 182 | ``` |
| 183 | |
| 184 | ## Supported types |
| 185 | |
| 186 | #### Image |
| 187 | |
| 188 | - **jpg** - `image/jpeg` |
| 189 | - **png** - `image/png` |
| 190 | - **gif** - `image/gif` |
| 191 | - **webp** - `image/webp` |
| 192 | - **cr2** - `image/x-canon-cr2` |
| 193 | - **tif** - `image/tiff` |
| 194 | - **bmp** - `image/bmp` |
| 195 | - **heif** - `image/heif` |
| 196 | - **jxr** - `image/vnd.ms-photo` |
| 197 | - **psd** - `image/vnd.adobe.photoshop` |
| 198 | - **ico** - `image/vnd.microsoft.icon` |
| 199 | - **dwg** - `image/vnd.dwg` |
| 200 | |
| 201 | #### Video |
| 202 | |
| 203 | - **mp4** - `video/mp4` |
| 204 | - **m4v** - `video/x-m4v` |
| 205 | - **mkv** - `video/x-matroska` |
| 206 | - **webm** - `video/webm` |
| 207 | - **mov** - `video/quicktime` |
| 208 | - **avi** - `video/x-msvideo` |
| 209 | - **wmv** - `video/x-ms-wmv` |
| 210 | - **mpg** - `video/mpeg` |
| 211 | - **flv** - `video/x-flv` |
| 212 | - **3gp** - `video/3gpp` |
| 213 | |
| 214 | #### Audio |
| 215 | |
| 216 | - **mid** - `audio/midi` |
| 217 | - **mp3** - `audio/mpeg` |
| 218 | - **m4a** - `audio/m4a` |
| 219 | - **ogg** - `audio/ogg` |
| 220 | - **flac** - `audio/x-flac` |
| 221 | - **wav** - `audio/x-wav` |
| 222 | - **amr** - `audio/amr` |
| 223 | - **aac** - `audio/aac` |
| 224 | - **aiff** - `audio/x-aiff` |
| 225 | |
| 226 | #### Archive |
| 227 | |
| 228 | - **epub** - `application/epub+zip` |
| 229 | - **zip** - `application/zip` |
| 230 | - **tar** - `application/x-tar` |
| 231 | - **rar** - `application/vnd.rar` |
| 232 | - **gz** - `application/gzip` |
| 233 | - **bz2** - `application/x-bzip2` |
| 234 | - **7z** - `application/x-7z-compressed` |
| 235 | - **xz** - `application/x-xz` |
| 236 | - **zstd** - `application/zstd` |
| 237 | - **pdf** - `application/pdf` |
| 238 | - **exe** - `application/vnd.microsoft.portable-executable` |
| 239 | - **swf** - `application/x-shockwave-flash` |
| 240 | - **rtf** - `application/rtf` |
| 241 | - **iso** - `application/x-iso9660-image` |
| 242 | - **eot** - `application/octet-stream` |
| 243 | - **ps** - `application/postscript` |
| 244 | - **sqlite** - `application/vnd.sqlite3` |
| 245 | - **nes** - `application/x-nintendo-nes-rom` |
| 246 | - **crx** - `application/x-google-chrome-extension` |
| 247 | - **cab** - `application/vnd.ms-cab-compressed` |
| 248 | - **deb** - `application/vnd.debian.binary-package` |
| 249 | - **ar** - `application/x-unix-archive` |
| 250 | - **Z** - `application/x-compress` |
| 251 | - **lz** - `application/x-lzip` |
| 252 | - **rpm** - `application/x-rpm` |
| 253 | - **elf** - `application/x-executable` |
| 254 | - **dcm** - `application/dicom` |
| 255 | |
| 256 | #### Documents |
| 257 | |
| 258 | - **doc** - `application/msword` |
| 259 | - **docx** - `application/vnd.openxmlformats-officedocument.wordprocessingml.document` |
| 260 | - **xls** - `application/vnd.ms-excel` |
| 261 | - **xlsx** - `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` |
| 262 | - **ppt** - `application/vnd.ms-powerpoint` |
| 263 | - **pptx** - `application/vnd.openxmlformats-officedocument.presentationml.presentation` |
| 264 | |
| 265 | #### Font |
| 266 | |
| 267 | - **woff** - `application/font-woff` |
| 268 | - **woff2** - `application/font-woff` |
| 269 | - **ttf** - `application/font-sfnt` |
| 270 | - **otf** - `application/font-sfnt` |
| 271 | |
| 272 | #### Application |
| 273 | |
| 274 | - **wasm** - `application/wasm` |
| 275 | - **dex** - `application/vnd.android.dex` |
| 276 | - **dey** - `application/vnd.android.dey` |
| 277 | |
| 278 | ## Benchmarks |
| 279 | |
| 280 | Measured using [real files](https://github.com/h2non/filetype/tree/master/fixtures). |
| 281 | |
| 282 | Environment: OSX x64 i7 2.7 Ghz |
| 283 | |
| 284 | ```bash |
| 285 | BenchmarkMatchTar-8 1000000 1083 ns/op |
| 286 | BenchmarkMatchZip-8 1000000 1162 ns/op |
| 287 | BenchmarkMatchJpeg-8 1000000 1280 ns/op |
| 288 | BenchmarkMatchGif-8 1000000 1315 ns/op |
| 289 | BenchmarkMatchPng-8 1000000 1121 ns/op |
| 290 | ``` |
| 291 | |
| 292 | ## License |
| 293 | |
| 294 | MIT - Tomas Aparicio |