Skip to content

luvitrocks/file-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

file-type

Detect the file type of a Buffer in Luvit.io.

The file type is detected by checking the magic number of the buffer.

Install

lit install voronianski/file-type

Example

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)

API

fileType(buffer)

Returns a Table (or nil when no match) with:

buffer

Type: Buffer

It only needs the first 262 bytes.

Supported file types

SVG isn't included as it requires the whole file to be read.

PR welcome for additional commonly used file types.

License

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.

About

Detect the file type of a Buffer in Luvit.io

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages