You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
9.8 KiB
205 lines
9.8 KiB
package lib
|
|
|
|
import (
|
|
"github.com/gabriel-vasile/mimetype"
|
|
"path/filepath"
|
|
"sync"
|
|
)
|
|
|
|
var mimeDetector = map[string]string{}
|
|
var mu sync.RWMutex
|
|
|
|
var (
|
|
css = addMIME("text/css", ".css")
|
|
xz = addMIME("application/x-xz", ".xz")
|
|
gzip = addMIME("application/gzip", ".gz")
|
|
sevenZ = addMIME("application/x-7z-compressed", ".7z")
|
|
zipZ = addMIME("application/zip", ".zip")
|
|
tar = addMIME("application/x-tar", ".tar")
|
|
xar = addMIME("application/x-xar", ".xar")
|
|
bz2 = addMIME("application/x-bzip2", ".bz2")
|
|
pdf = addMIME("application/pdf", ".pdf")
|
|
fdf = addMIME("application/vnd.fdf", ".fdf")
|
|
xlsx = addMIME("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx")
|
|
docx = addMIME("application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".docx")
|
|
pptx = addMIME("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx")
|
|
epub = addMIME("application/epub+zip", ".epub")
|
|
jar = addMIME("application/jar", ".jar")
|
|
ole = addMIME("application/x-ole-storage", "")
|
|
msi = addMIME("application/x-ms-installer", ".msi")
|
|
aaf = addMIME("application/octet-stream", ".aaf")
|
|
doc = addMIME("application/msword", ".doc")
|
|
ppt = addMIME("application/vnd.ms-powerpoint", ".ppt")
|
|
pub = addMIME("application/vnd.ms-publisher", ".pub")
|
|
xls = addMIME("application/vnd.ms-excel", ".xls")
|
|
msg = addMIME("application/vnd.ms-outlook", ".msg")
|
|
ps = addMIME("application/postscript", ".ps")
|
|
fits = addMIME("application/fits", ".fits")
|
|
ogg = addMIME("application/ogg", ".ogg")
|
|
oggAudio = addMIME("audio/ogg", ".oga")
|
|
oggVideo = addMIME("video/ogg", ".ogv")
|
|
text = addMIME("text/plain", ".txt")
|
|
xml = addMIME("text/xml", ".xml")
|
|
jsonZ = addMIME("application/json", ".json")
|
|
har = addMIME("application/json", ".har")
|
|
csv = addMIME("text/csv", ".csv")
|
|
tsv = addMIME("text/tab-separated-values", ".tsv")
|
|
geoJSON = addMIME("application/geo+json", ".geojson")
|
|
ndJSON = addMIME("application/x-ndjson", ".ndjson")
|
|
html = addMIME("text/html", ".html")
|
|
php = addMIME("text/x-php", ".php")
|
|
rtf = addMIME("text/rtf", ".rtf")
|
|
js = addMIME("application/javascript", ".js")
|
|
srt = addMIME("application/x-subrip", ".srt")
|
|
vtt = addMIME("text/vtt", ".vtt")
|
|
lua = addMIME("text/x-lua", ".lua")
|
|
perl = addMIME("text/x-perl", ".pl")
|
|
python = addMIME("text/x-python", ".py")
|
|
tcl = addMIME("text/x-tcl", ".tcl")
|
|
vCard = addMIME("text/vcard", ".vcf")
|
|
iCalendar = addMIME("text/calendar", ".ics")
|
|
svg = addMIME("image/svg+xml", ".svg")
|
|
rss = addMIME("application/rss+xml", ".rss")
|
|
owl2 = addMIME("application/owl+xml", ".owl")
|
|
atom = addMIME("application/atom+xml", ".atom")
|
|
x3d = addMIME("model/x3d+xml", ".x3d")
|
|
kml = addMIME("application/vnd.google-earth.kml+xml", ".kml")
|
|
xliff = addMIME("application/x-xliff+xml", ".xlf")
|
|
collada = addMIME("model/vnd.collada+xml", ".dae")
|
|
gml = addMIME("application/gml+xml", ".gml")
|
|
gpx = addMIME("application/gpx+xml", ".gpx")
|
|
tcx = addMIME("application/vnd.garmin.tcx+xml", ".tcx")
|
|
amf = addMIME("application/x-amf", ".amf")
|
|
threemf = addMIME("application/vnd.ms-package.3dmanufacturing-3dmodel+xml", ".3mf")
|
|
png = addMIME("image/png", ".png")
|
|
apng = addMIME("image/vnd.mozilla.apng", ".png")
|
|
jpg = addMIME("image/jpeg", ".jpg")
|
|
jxl = addMIME("image/jxl", ".jxl")
|
|
jp2 = addMIME("image/jp2", ".jp2")
|
|
jpx = addMIME("image/jpx", ".jpf")
|
|
jpm = addMIME("image/jpm", ".jpm")
|
|
xpm = addMIME("image/x-xpixmap", ".xpm")
|
|
bpg = addMIME("image/bpg", ".bpg")
|
|
gif = addMIME("image/gif", ".gif")
|
|
webp = addMIME("image/webp", ".webp")
|
|
tiff = addMIME("image/tiff", ".tiff")
|
|
bmp = addMIME("image/bmp", ".bmp")
|
|
ico = addMIME("image/x-icon", ".ico")
|
|
icns = addMIME("image/x-icns", ".icns")
|
|
psd = addMIME("image/vnd.adobe.photoshop", ".psd")
|
|
heic = addMIME("image/heic", ".heic")
|
|
heicSeq = addMIME("image/heic-sequence", ".heic")
|
|
heif = addMIME("image/heif", ".heif")
|
|
heifSeq = addMIME("image/heif-sequence", ".heif")
|
|
hdr = addMIME("image/vnd.radiance", ".hdr")
|
|
avif = addMIME("image/avif", ".avif")
|
|
mp3 = addMIME("audio/mpeg", ".mp3")
|
|
flac = addMIME("audio/flac", ".flac")
|
|
midi = addMIME("audio/midi", ".midi")
|
|
ape = addMIME("audio/ape", ".ape")
|
|
musePack = addMIME("audio/musepack", ".mpc")
|
|
wav = addMIME("audio/wav", ".wav")
|
|
aiff = addMIME("audio/aiff", ".aiff")
|
|
au = addMIME("audio/basic", ".au")
|
|
amr = addMIME("audio/amr", ".amr")
|
|
aac = addMIME("audio/aac", ".aac")
|
|
voc = addMIME("audio/x-unknown", ".voc")
|
|
aMp4 = addMIME("audio/mp4", ".mp4")
|
|
m4a = addMIME("audio/x-m4a", ".m4a")
|
|
m3u = addMIME("application/vnd.apple.mpegurl", ".m3u")
|
|
m4v = addMIME("video/x-m4v", ".m4v")
|
|
mp4 = addMIME("video/mp4", ".mp4")
|
|
webM = addMIME("video/webm", ".webm")
|
|
mpeg = addMIME("video/mpeg", ".mpeg")
|
|
quickTime = addMIME("video/quicktime", ".mov")
|
|
mqv = addMIME("video/quicktime", ".mqv")
|
|
threeGP = addMIME("video/3gpp", ".3gp")
|
|
threeG2 = addMIME("video/3gpp2", ".3g2")
|
|
avi = addMIME("video/x-msvideo", ".avi")
|
|
flv = addMIME("video/x-flv", ".flv")
|
|
mkv = addMIME("video/x-matroska", ".mkv")
|
|
asf = addMIME("video/x-ms-asf", ".asf")
|
|
rmvb = addMIME("application/vnd.rn-realmedia-vbr", ".rmvb")
|
|
class = addMIME("application/x-java-applet", ".class")
|
|
swf = addMIME("application/x-shockwave-flash", ".swf")
|
|
crx = addMIME("application/x-chrome-extension", ".crx")
|
|
ttf = addMIME("font/ttf", ".ttf")
|
|
woff = addMIME("font/woff", ".woff")
|
|
woff2 = addMIME("font/woff2", ".woff2")
|
|
otf = addMIME("font/otf", ".otf")
|
|
ttc = addMIME("font/collection", ".ttc")
|
|
eot = addMIME("application/vnd.ms-fontobject", ".eot")
|
|
wasm = addMIME("application/wasm", ".wasm")
|
|
shp = addMIME("application/vnd.shp", ".shp")
|
|
shx = addMIME("application/vnd.shx", ".shx")
|
|
dbf = addMIME("application/x-dbf", ".dbf")
|
|
exe = addMIME("application/vnd.microsoft.portable-executable", ".exe")
|
|
elf = addMIME("application/x-elf", "")
|
|
elfObj = addMIME("application/x-object", "")
|
|
elfExe = addMIME("application/x-executable", "")
|
|
elfLib = addMIME("application/x-sharedlib", ".so")
|
|
elfDump = addMIME("application/x-coredump", "")
|
|
ar = addMIME("application/x-archive", ".a")
|
|
deb = addMIME("application/vnd.debian.binary-package", ".deb")
|
|
rpm = addMIME("application/x-rpm", ".rpm")
|
|
dcm = addMIME("application/dicom", ".dcm")
|
|
odt = addMIME("application/vnd.oasis.opendocument.text", ".odt")
|
|
ott = addMIME("application/vnd.oasis.opendocument.text-template", ".ott")
|
|
ods = addMIME("application/vnd.oasis.opendocument.spreadsheet", ".ods")
|
|
ots = addMIME("application/vnd.oasis.opendocument.spreadsheet-template", ".ots")
|
|
odp = addMIME("application/vnd.oasis.opendocument.presentation", ".odp")
|
|
otp = addMIME("application/vnd.oasis.opendocument.presentation-template", ".otp")
|
|
odg = addMIME("application/vnd.oasis.opendocument.graphics", ".odg")
|
|
otg = addMIME("application/vnd.oasis.opendocument.graphics-template", ".otg")
|
|
odf = addMIME("application/vnd.oasis.opendocument.formula", ".odf")
|
|
odc = addMIME("application/vnd.oasis.opendocument.chart", ".odc")
|
|
sxc = addMIME("application/vnd.sun.xml.calc", ".sxc")
|
|
rar = addMIME("application/x-rar-compressed", ".rar")
|
|
djvu = addMIME("image/vnd.djvu", ".djvu")
|
|
mobi = addMIME("application/x-mobipocket-ebook", ".mobi")
|
|
lit = addMIME("application/x-ms-reader", ".lit")
|
|
sqlite3 = addMIME("application/vnd.sqlite3", ".sqlite")
|
|
dwg = addMIME("image/vnd.dwg", ".dwg")
|
|
warc = addMIME("application/warc", ".warc")
|
|
nes = addMIME("application/vnd.nintendo.snes.rom", ".nes")
|
|
lnk = addMIME("application/x-ms-shortcut", ".lnk")
|
|
macho = addMIME("application/x-mach-binary", ".macho")
|
|
qcp = addMIME("audio/qcelp", ".qcp")
|
|
mrc = addMIME("application/marc", ".mrc")
|
|
mdb = addMIME("application/x-msaccess", ".mdb")
|
|
accdb = addMIME("application/x-msaccess", ".accdb")
|
|
zstd = addMIME("application/zstd", ".zst")
|
|
cab = addMIME("application/vnd.ms-cab-compressed", ".cab")
|
|
cabIS = addMIME("application/x-installshield", ".cab")
|
|
lzip = addMIME("application/lzip", ".lz")
|
|
torrent = addMIME("application/x-bittorrent", ".torrent")
|
|
cpio = addMIME("application/x-cpio", ".cpio")
|
|
tzif = addMIME("application/tzif", "")
|
|
p7s = addMIME("application/pkcs7-signature", ".p7s")
|
|
xcf = addMIME("image/x-xcf", ".xcf")
|
|
pat = addMIME("image/x-gimp-pat", ".pat")
|
|
gbr = addMIME("image/x-gimp-gbr", ".gbr")
|
|
xfdf = addMIME("application/vnd.adobe.xfdf", ".xfdf")
|
|
glb = addMIME("model/gltf-binary", ".glb")
|
|
)
|
|
|
|
// addMIME наполняем мапку значениями
|
|
func addMIME(mimeType, ext string) error {
|
|
mimeDetector[ext] = mimeType
|
|
return nil
|
|
}
|
|
|
|
func detectMIME(data []byte, file string) (mimeType string) {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
// определяем по расширения, если нашли - возвращаем
|
|
if v, found := mimeDetector[filepath.Ext(file)]; found {
|
|
return v
|
|
}
|
|
|
|
// если не нашли или расширение пустое - пытаемся определить по содержимому файла (через сторонний пакет)
|
|
mimeType = mimetype.Detect(data).String() // - работает не быстро (по мануалу), но как доп.определялка можно включить
|
|
|
|
return mimeType
|
|
}
|
|
|