Debian Codegraph @

tree.go

raw symbols SCIP
1 package mimetype
2
3 import (
4 "sync"
5
6 "github.com/gabriel-vasile/mimetype/internal/magic"
7 )
8
9 // mimetype stores the list of MIME types in a tree structure with
10 // "application/octet-stream" at the root of the hierarchy. The hierarchy
11 // approach minimizes the number of checks that need to be done on the input
12 // and allows for more precise results once the base type of file has been
13 // identified.
14 //
15 // root is a detector which passes for any slice of bytes.
16 // When a detector passes the check, the children detectors
17 // are tried in order to find a more accurate MIME type.
18 var root = newMIME("application/octet-stream", "",
19 func([]byte, uint32) bool { return true },
20 xpm, sevenZ, zip, pdf, fdf, ole, ps, psd, p7s, ogg, png, jpg, jxl, jp2, jpx,
21 jpm, jxs, gif, webp, exe, elf, ar, tar, xar, bz2, fits, tiff, bmp, lotus, ico,
22 flac, midi, ape, musePack, amr, wav, aiff, au, mpeg, quickTime, mp4, webM,
23 avi, flv, mkv, asf, aac, voc, m3u, rmvb, gzip, class, swf, crx, ttf, woff,
24 woff2, otf, ttc, eot, wasm, shx, dbf, dcm, rar, djvu, mobi, lit, bpg, cbor,
25 sqlite3, dwg, nes, lnk, macho, qcp, icns, hdr, mrc, mdb, accdb, zstd, cab,
26 rpm, xz, lzip, torrent, cpio, tzif, xcf, pat, gbr, glb, cabIS, jxr, parquet,
27 oneNote, chm, wpd, dxf, grib, zlib, inf, hlp, fm, bufr, pyc, pcap,
28 // MP3 is late because it does a linear search in the input. That means
29 // containers that embed an MP3, for example: an mp4 file, or a zip without
30 // compression, would pass as MP3s.
31 mp3,
32 // Keep text last because it is the slowest check.
33 text,
34 )
35
36 // errMIME is returned from Detect functions when err is not nil.
37 // Detect could return root for erroneous cases, but it needs to lock mu in order to do so.
38 // errMIME is same as root but it does not require locking.
39 var errMIME = newMIME("application/octet-stream", "", func([]byte, uint32) bool { return false })
40
41 // mu guards access to the root MIME tree. Access to root must be synchronized with this lock.
42 var mu = &sync.RWMutex{}
43
44 // The list of nodes appended to the root node.
45 var (
46 xz = newMIME("application/x-xz", ".xz", magic.Xz)
47 gzip = newMIME("application/gzip", ".gz", magic.Gzip).alias(
48 "application/x-gzip", "application/x-gunzip", "application/gzipped",
49 "application/gzip-compressed", "application/x-gzip-compressed",
50 "gzip/document")
51 sevenZ = newMIME("application/x-7z-compressed", ".7z", magic.SevenZ)
52 // APK must be checked before JAR because APK is a subset of JAR.
53 // This means APK should be a child of JAR detector, but in practice,
54 // the decisive signature for JAR might be located at the end of the file
55 // and not reachable because of library readLimit.
56 zip = newMIME("application/zip", ".zip", magic.Zip, docx, pptx, xlsx, epub, apk, jar, odt, ods, odp, odg, odf, odc, sxc, kmz, visio).
57 alias("application/x-zip", "application/x-zip-compressed")
58 tar = newMIME("application/x-tar", ".tar", magic.Tar)
59 xar = newMIME("application/x-xar", ".xar", magic.Xar)
60 bz2 = newMIME("application/x-bzip2", ".bz2", magic.Bz2)
61 pdf = newMIME("application/pdf", ".pdf", magic.PDF).
62 alias("application/x-pdf")
63 fdf = newMIME("application/vnd.fdf", ".fdf", magic.Fdf)
64 xlsx = newMIME("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx", magic.Xlsx)
65 docx = newMIME("application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx", magic.Docx)
66 pptx = newMIME("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx", magic.Pptx)
67 visio = newMIME("application/vnd.ms-visio.drawing.main+xml", ".vsdx", magic.Visio)
68 epub = newMIME("application/epub+zip", ".epub", magic.Epub)
69 jar = newMIME("application/java-archive", ".jar", magic.Jar).
70 alias("application/jar", "application/jar-archive", "application/x-java-archive")
71 apk = newMIME("application/vnd.android.package-archive", ".apk", magic.APK)
72 ole = newMIME("application/x-ole-storage", "", magic.Ole, msi, msg, xls, pub, ppt, doc)
73 msi = newMIME("application/x-ms-installer", ".msi", magic.Msi).
74 alias("application/x-windows-installer", "application/x-msi")
75 doc = newMIME("application/msword", ".doc", magic.Doc).
76 alias("application/vnd.ms-word")
77 ppt = newMIME("application/vnd.ms-powerpoint", ".ppt", magic.Ppt).
78 alias("application/mspowerpoint")
79 pub = newMIME("application/vnd.ms-publisher", ".pub", magic.Pub)
80 xls = newMIME("application/vnd.ms-excel", ".xls", magic.Xls).
81 alias("application/msexcel")
82 msg = newMIME("application/vnd.ms-outlook", ".msg", magic.Msg)
83 ps = newMIME("application/postscript", ".ps", magic.Ps)
84 fits = newMIME("application/fits", ".fits", magic.Fits).alias("image/fits")
85 ogg = newMIME("application/ogg", ".ogg", magic.Ogg, oggAudio, oggVideo).
86 alias("application/x-ogg")
87 oggAudio = newMIME("audio/ogg", ".oga", magic.OggAudio)
88 oggVideo = newMIME("video/ogg", ".ogv", magic.OggVideo)
89 text = newMIME("text/plain", ".txt", magic.Text, svg, html, xml, php, js, lua, perl, python, ruby, json, ndJSON, rtf, srt, tcl, csv, tsv, vCard, iCalendar, warc, vtt, shell, netpbm, netpgm, netppm, netpam, rfc822, gedcom)
90 xml = newMIME("text/xml", ".xml", magic.XML, rss, atom, x3d, kml, xliff, collada, gml, gpx, tcx, amf, threemf, xfdf, owl2, xhtml, cdxxml).
91 alias("application/xml")
92 xhtml = newMIME("application/xhtml+xml", ".html", magic.XHTML)
93 json = newMIME("application/json", ".json", magic.JSON, geoJSON, har, gltf, cdxJSON)
94 har = newMIME("application/json", ".har", magic.HAR)
95 csv = newMIME("text/csv", ".csv", magic.CSV)
96 tsv = newMIME("text/tab-separated-values", ".tsv", magic.TSV)
97 geoJSON = newMIME("application/geo+json", ".geojson", magic.GeoJSON)
98 ndJSON = newMIME("application/x-ndjson", ".ndjson", magic.NdJSON)
99 cdxJSON = newMIME("application/vnd.cyclonedx+json", ".json", magic.CDXJSON)
100 html = newMIME("text/html", ".html", magic.HTML)
101 php = newMIME("text/x-php", ".php", magic.Php)
102 rtf = newMIME("text/rtf", ".rtf", magic.Rtf).alias("application/rtf")
103 js = newMIME("text/javascript", ".js", magic.Js).
104 alias("application/x-javascript", "application/javascript")
105 srt = newMIME("application/x-subrip", ".srt", magic.Srt).
106 alias("application/x-srt", "text/x-srt")
107 vtt = newMIME("text/vtt", ".vtt", magic.Vtt)
108 lua = newMIME("text/x-lua", ".lua", magic.Lua)
109 perl = newMIME("text/x-perl", ".pl", magic.Perl)
110 python = newMIME("text/x-python", ".py", magic.Python).
111 alias("text/x-script.python", "application/x-python")
112 pyc = newMIME("application/x-bytecode.python", ".pyc", magic.Pyc)
113 ruby = newMIME("text/x-ruby", ".rb", magic.Ruby).
114 alias("application/x-ruby")
115 shell = newMIME("text/x-shellscript", ".sh", magic.Shell).
116 alias("text/x-sh", "application/x-shellscript", "application/x-sh")
117 tcl = newMIME("text/x-tcl", ".tcl", magic.Tcl).
118 alias("application/x-tcl")
119 vCard = newMIME("text/vcard", ".vcf", magic.VCard)
120 iCalendar = newMIME("text/calendar", ".ics", magic.ICalendar)
121 svg = newMIME("image/svg+xml", ".svg", magic.Svg)
122 rss = newMIME("application/rss+xml", ".rss", magic.Rss).
123 alias("text/rss")
124 owl2 = newMIME("application/owl+xml", ".owl", magic.Owl2)
125 atom = newMIME("application/atom+xml", ".atom", magic.Atom)
126 x3d = newMIME("model/x3d+xml", ".x3d", magic.X3d)
127 kml = newMIME("application/vnd.google-earth.kml+xml", ".kml", magic.Kml)
128 kmz = newMIME("application/vnd.google-earth.kmz", ".kmz", magic.KMZ)
129 xliff = newMIME("application/x-xliff+xml", ".xlf", magic.Xliff)
130 collada = newMIME("model/vnd.collada+xml", ".dae", magic.Collada)
131 gml = newMIME("application/gml+xml", ".gml", magic.Gml)
132 gpx = newMIME("application/gpx+xml", ".gpx", magic.Gpx)
133 tcx = newMIME("application/vnd.garmin.tcx+xml", ".tcx", magic.Tcx)
134 amf = newMIME("application/x-amf", ".amf", magic.Amf)
135 threemf = newMIME("application/vnd.ms-package.3dmanufacturing-3dmodel+xml", ".3mf", magic.Threemf)
136 cdxxml = newMIME("application/vnd.cyclonedx+xml", ".xml", magic.CDXXML)
137 png = newMIME("image/png", ".png", magic.Png, apng)
138 apng = newMIME("image/apng", ".apng", magic.Apng).
139 alias("image/vnd.mozilla.apng")
140 jpg = newMIME("image/jpeg", ".jpg", magic.Jpg)
141 jxl = newMIME("image/jxl", ".jxl", magic.Jxl)
142 jp2 = newMIME("image/jp2", ".jp2", magic.Jp2)
143 jpx = newMIME("image/jpx", ".jpf", magic.Jpx)
144 jpm = newMIME("image/jpm", ".jpm", magic.Jpm).
145 alias("video/jpm")
146 jxs = newMIME("image/jxs", ".jxs", magic.Jxs)
147 xpm = newMIME("image/x-xpixmap", ".xpm", magic.Xpm)
148 bpg = newMIME("image/bpg", ".bpg", magic.Bpg)
149 gif = newMIME("image/gif", ".gif", magic.Gif)
150 webp = newMIME("image/webp", ".webp", magic.Webp)
151 tiff = newMIME("image/tiff", ".tiff", magic.Tiff)
152 bmp = newMIME("image/bmp", ".bmp", magic.Bmp).
153 alias("image/x-bmp", "image/x-ms-bmp")
154 // lotus check must be done before ico because some ico detection is a bit
155 // relaxed and some lotus files are wrongfully identified as ico otherwise.
156 lotus = newMIME("application/vnd.lotus-1-2-3", ".123", magic.Lotus123)
157 ico = newMIME("image/x-icon", ".ico", magic.Ico)
158 icns = newMIME("image/x-icns", ".icns", magic.Icns)
159 psd = newMIME("image/vnd.adobe.photoshop", ".psd", magic.Psd).
160 alias("image/x-psd", "application/photoshop")
161 heic = newMIME("image/heic", ".heic", magic.Heic)
162 heicSeq = newMIME("image/heic-sequence", ".heic", magic.HeicSequence)
163 heif = newMIME("image/heif", ".heif", magic.Heif)
164 heifSeq = newMIME("image/heif-sequence", ".heif", magic.HeifSequence)
165 hdr = newMIME("image/vnd.radiance", ".hdr", magic.Hdr)
166 avif = newMIME("image/avif", ".avif", magic.AVIF)
167 mp3 = newMIME("audio/mpeg", ".mp3", magic.MP3).
168 alias("audio/x-mpeg", "audio/mp3")
169 flac = newMIME("audio/flac", ".flac", magic.Flac)
170 midi = newMIME("audio/midi", ".midi", magic.Midi).
171 alias("audio/mid", "audio/sp-midi", "audio/x-mid", "audio/x-midi")
172 ape = newMIME("audio/ape", ".ape", magic.Ape)
173 musePack = newMIME("audio/musepack", ".mpc", magic.MusePack)
174 wav = newMIME("audio/wav", ".wav", magic.Wav).
175 alias("audio/x-wav", "audio/vnd.wave", "audio/wave")
176 aiff = newMIME("audio/aiff", ".aiff", magic.Aiff).alias("audio/x-aiff")
177 au = newMIME("audio/basic", ".au", magic.Au)
178 amr = newMIME("audio/amr", ".amr", magic.Amr).
179 alias("audio/amr-nb")
180 aac = newMIME("audio/aac", ".aac", magic.AAC)
181 voc = newMIME("audio/x-unknown", ".voc", magic.Voc)
182 aMp4 = newMIME("audio/mp4", ".mp4", magic.AMp4).
183 alias("audio/x-mp4a")
184 m4a = newMIME("audio/x-m4a", ".m4a", magic.M4a)
185 m3u = newMIME("application/vnd.apple.mpegurl", ".m3u", magic.M3U).
186 alias("audio/mpegurl", "application/x-mpegurl")
187 m4v = newMIME("video/x-m4v", ".m4v", magic.M4v)
188 mj2 = newMIME("video/mj2", ".mj2", magic.Mj2)
189 dvb = newMIME("video/vnd.dvb.file", ".dvb", magic.Dvb)
190 mp4 = newMIME("video/mp4", ".mp4", magic.Mp4, avif, threeGP, threeG2, aMp4, mqv, m4a, m4v, heic, heicSeq, heif, heifSeq, mj2, dvb)
191 webM = newMIME("video/webm", ".webm", magic.WebM).
192 alias("audio/webm")
193 mpeg = newMIME("video/mpeg", ".mpeg", magic.Mpeg)
194 quickTime = newMIME("video/quicktime", ".mov", magic.QuickTime)
195 mqv = newMIME("video/quicktime", ".mqv", magic.Mqv)
196 threeGP = newMIME("video/3gpp", ".3gp", magic.ThreeGP).
197 alias("video/3gp", "audio/3gpp")
198 threeG2 = newMIME("video/3gpp2", ".3g2", magic.ThreeG2).
199 alias("video/3g2", "audio/3gpp2")
200 avi = newMIME("video/x-msvideo", ".avi", magic.Avi).
201 alias("video/avi", "video/msvideo")
202 flv = newMIME("video/x-flv", ".flv", magic.Flv)
203 mkv = newMIME("video/matroska", ".mkv", magic.Mkv).
204 alias("video/x-matroska")
205 asf = newMIME("video/x-ms-asf", ".asf", magic.Asf).
206 alias("video/asf", "video/x-ms-wmv")
207 rmvb = newMIME("application/vnd.rn-realmedia-vbr", ".rmvb", magic.Rmvb)
208 class = newMIME("application/x-java-applet", ".class", magic.Class)
209 swf = newMIME("application/x-shockwave-flash", ".swf", magic.SWF)
210 crx = newMIME("application/x-chrome-extension", ".crx", magic.CRX)
211 ttf = newMIME("font/ttf", ".ttf", magic.Ttf).
212 alias("font/sfnt", "application/x-font-ttf", "application/font-sfnt")
213 woff = newMIME("font/woff", ".woff", magic.Woff)
214 woff2 = newMIME("font/woff2", ".woff2", magic.Woff2)
215 otf = newMIME("font/otf", ".otf", magic.Otf)
216 ttc = newMIME("font/collection", ".ttc", magic.Ttc)
217 eot = newMIME("application/vnd.ms-fontobject", ".eot", magic.Eot)
218 wasm = newMIME("application/wasm", ".wasm", magic.Wasm)
219 shp = newMIME("application/vnd.shp", ".shp", magic.Shp)
220 shx = newMIME("application/vnd.shx", ".shx", magic.Shx, shp)
221 dbf = newMIME("application/x-dbf", ".dbf", magic.Dbf)
222 exe = newMIME("application/vnd.microsoft.portable-executable", ".exe", magic.Exe)
223 elf = newMIME("application/x-elf", "", magic.Elf, elfObj, elfExe, elfLib, elfDump)
224 elfObj = newMIME("application/x-object", "", magic.ElfObj)
225 elfExe = newMIME("application/x-executable", "", magic.ElfExe)
226 elfLib = newMIME("application/x-sharedlib", ".so", magic.ElfLib)
227 elfDump = newMIME("application/x-coredump", "", magic.ElfDump)
228 ar = newMIME("application/x-archive", ".a", magic.Ar, deb).
229 alias("application/x-unix-archive")
230 deb = newMIME("application/vnd.debian.binary-package", ".deb", magic.Deb)
231 rpm = newMIME("application/x-rpm", ".rpm", magic.RPM)
232 dcm = newMIME("application/dicom", ".dcm", magic.Dcm)
233 odt = newMIME("application/vnd.oasis.opendocument.text", ".odt", magic.Odt, ott).
234 alias("application/x-vnd.oasis.opendocument.text")
235 ott = newMIME("application/vnd.oasis.opendocument.text-template", ".ott", magic.Ott).
236 alias("application/x-vnd.oasis.opendocument.text-template")
237 ods = newMIME("application/vnd.oasis.opendocument.spreadsheet", ".ods", magic.Ods, ots).
238 alias("application/x-vnd.oasis.opendocument.spreadsheet")
239 ots = newMIME("application/vnd.oasis.opendocument.spreadsheet-template", ".ots", magic.Ots).
240 alias("application/x-vnd.oasis.opendocument.spreadsheet-template")
241 odp = newMIME("application/vnd.oasis.opendocument.presentation", ".odp", magic.Odp, otp).
242 alias("application/x-vnd.oasis.opendocument.presentation")
243 otp = newMIME("application/vnd.oasis.opendocument.presentation-template", ".otp", magic.Otp).
244 alias("application/x-vnd.oasis.opendocument.presentation-template")
245 odg = newMIME("application/vnd.oasis.opendocument.graphics", ".odg", magic.Odg, otg).
246 alias("application/x-vnd.oasis.opendocument.graphics")
247 otg = newMIME("application/vnd.oasis.opendocument.graphics-template", ".otg", magic.Otg).
248 alias("application/x-vnd.oasis.opendocument.graphics-template")
249 odf = newMIME("application/vnd.oasis.opendocument.formula", ".odf", magic.Odf).
250 alias("application/x-vnd.oasis.opendocument.formula")
251 odc = newMIME("application/vnd.oasis.opendocument.chart", ".odc", magic.Odc).
252 alias("application/x-vnd.oasis.opendocument.chart")
253 sxc = newMIME("application/vnd.sun.xml.calc", ".sxc", magic.Sxc)
254 rar = newMIME("application/vnd.rar", ".rar", magic.RAR).
255 alias("application/x-rar-compressed", "application/x-rar")
256 djvu = newMIME("image/vnd.djvu", ".djvu", magic.DjVu)
257 mobi = newMIME("application/x-mobipocket-ebook", ".mobi", magic.Mobi)
258 lit = newMIME("application/x-ms-reader", ".lit", magic.Lit)
259 sqlite3 = newMIME("application/vnd.sqlite3", ".sqlite", magic.Sqlite).
260 alias("application/x-sqlite3")
261 dwg = newMIME("image/vnd.dwg", ".dwg", magic.Dwg).
262 alias("image/x-dwg", "application/acad", "application/x-acad",
263 "application/autocad_dwg", "application/dwg", "application/x-dwg",
264 "application/x-autocad", "drawing/dwg")
265 warc = newMIME("application/warc", ".warc", magic.Warc)
266 nes = newMIME("application/vnd.nintendo.snes.rom", ".nes", magic.Nes)
267 lnk = newMIME("application/x-ms-shortcut", ".lnk", magic.Lnk)
268 macho = newMIME("application/x-mach-binary", ".macho", magic.MachO)
269 qcp = newMIME("audio/qcelp", ".qcp", magic.Qcp)
270 mrc = newMIME("application/marc", ".mrc", magic.Marc)
271 mdb = newMIME("application/x-msaccess", ".mdb", magic.MsAccessMdb)
272 accdb = newMIME("application/x-msaccess", ".accdb", magic.MsAccessAce)
273 zstd = newMIME("application/zstd", ".zst", magic.Zstd)
274 cab = newMIME("application/vnd.ms-cab-compressed", ".cab", magic.Cab)
275 cabIS = newMIME("application/x-installshield", ".cab", magic.InstallShieldCab)
276 lzip = newMIME("application/lzip", ".lz", magic.Lzip).alias("application/x-lzip")
277 torrent = newMIME("application/x-bittorrent", ".torrent", magic.Torrent)
278 cpio = newMIME("application/x-cpio", ".cpio", magic.Cpio)
279 tzif = newMIME("application/tzif", "", magic.TzIf)
280 p7s = newMIME("application/pkcs7-signature", ".p7s", magic.P7s)
281 xcf = newMIME("image/x-xcf", ".xcf", magic.Xcf)
282 pat = newMIME("image/x-gimp-pat", ".pat", magic.Pat)
283 gbr = newMIME("image/x-gimp-gbr", ".gbr", magic.Gbr)
284 xfdf = newMIME("application/vnd.adobe.xfdf", ".xfdf", magic.Xfdf)
285 glb = newMIME("model/gltf-binary", ".glb", magic.GLB)
286 gltf = newMIME("model/gltf+json", ".gltf", magic.GLTF)
287 jxr = newMIME("image/jxr", ".jxr", magic.Jxr).alias("image/vnd.ms-photo")
288 parquet = newMIME("application/vnd.apache.parquet", ".parquet", magic.Par1).
289 alias("application/x-parquet")
290 netpbm = newMIME("image/x-portable-bitmap", ".pbm", magic.NetPBM)
291 netpgm = newMIME("image/x-portable-graymap", ".pgm", magic.NetPGM)
292 netppm = newMIME("image/x-portable-pixmap", ".ppm", magic.NetPPM)
293 netpam = newMIME("image/x-portable-arbitrarymap", ".pam", magic.NetPAM)
294 cbor = newMIME("application/cbor", ".cbor", magic.CBOR)
295 oneNote = newMIME("application/onenote", ".one", magic.One)
296 chm = newMIME("application/vnd.ms-htmlhelp", ".chm", magic.CHM)
297 wpd = newMIME("application/vnd.wordperfect", ".wpd", magic.WPD)
298 dxf = newMIME("image/vnd.dxf", ".dxf", magic.DXF)
299 rfc822 = newMIME("message/rfc822", ".eml", magic.RFC822)
300 grib = newMIME("application/grib", ".grb", magic.GRIB)
301 zlib = newMIME("application/zlib", "", magic.Zlib)
302 inf = newMIME("application/x-os2-inf", ".inf", magic.Inf)
303 hlp = newMIME("application/x-os2-hlp", ".hlp", magic.Hlp)
304 fm = newMIME("application/vnd.framemaker", ".fm", magic.FrameMaker)
305 bufr = newMIME("application/bufr", ".bufr", magic.BUFR)
306 gedcom = newMIME("text/vnd.familysearch.gedcom", ".ged", magic.GEDCOM)
307 pcap = newMIME("application/vnd.tcpdump.pcap", ".pcap", magic.Pcap)
308 )