Detect the file type of a Buffer in Luvit.io.
The file type is detected by checking the magic number of the buffer.
lit install voronianski/file-type
local http = require('http')
local fileType = require('file-type')
local url = 'http://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif'
http.get(url, function (res)
res:on('data', function (chunk)
print(fileType(chunk))
-- {ext = 'gif', mime = 'image/gif'}
end)
end)
Returns a Table
(or nil
when no match) with:
ext
- one of the supported file typesmime
- the MIME type
Type: Buffer
It only needs the first 262 bytes.
jpg
png
gif
webp
flif
cr2
tif
bmp
jxr
psd
zip
tar
rar
gz
bz2
7z
dmg
mp4
m4v
mid
mkv
webm
mov
avi
wmv
mpg
mp3
m4a
ogg
opus
flac
wav
amr
pdf
epub
exe
swf
rtf
woff
woff2
eot
ttf
otf
ico
flv
ps
xz
sqlite
nes
crx
xpi
cab
deb
ar
rpm
Z
lz
msi
SVG isn't included as it requires the whole file to be read.
PR welcome for additional commonly used file types.
MIT Licensed
Copyright (c) 2016 Dmitri Voronianski [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.