Giter VIP home page Giter VIP logo

puremagic's Introduction

puremagic

A pure lua module that detects the mime type of file based on their contents. Falls back to detecting based on extension for plain text files.

Tested on Lua 5.2 and Luajit 2.0. Should work with Lua 5.1 also.

Version

The current version is: 1.0.1

Usage

Basic usage:

local puremagic = require('puremagic')
local mimetype = puremagic.via_path('/path/to/file')

When dealing with temp files, you may want to pass the original filename in case the extension is needed:

local puremagic = require('puremagic')
local mimetype = puremagic.via_path('/var/tmp/xyz', 'test.xlsx')

If you have the contents of the file in memory, you can provide those plus the filename:

local content = '#!/bin/bash\n'
local puremagic = require('puremagic')
local mimetype = puremagic.via_content(content, 'test.sh')

Supported Mime Types

The following mime types are detected:

Images

File type Mime type
AI application/pdf or application/postscript
BMP image/x-ms-bmp
EPS application/postscript
GIF image/gif
ICO application/vnd.microsoft.icon
JPEG image/jpeg
JPEG 2000 image/jp2
PNG image/png
PSD image/vnd.adobe.photoshop
SVG image/svg+xml
TIFF image/tiff
webp image/webp

Video

File type Mime type
3GP video/3gpp
3GP2 video/3gpp2
AVI video/x-msvideo
FLV video/x-flv
MKV video/x-matroska
MOV video/quicktime
MP4 video/mp4
SWF application/x-shockwave-flash
Theora video/ogg
webm video/webm
WMV video/x-ms-wmv

Audio

File type Mime type
AIFF audio/x-aiff
FLAC audio/x-flac
M4A audio/mp4
MKA audio/x-matroska
MP3 audio/mpeg
Ogg Vorbis audio/vorbis
Opus audio/ogg
WAV audio/x-wav
webm audio/webm
WMA audio/x-ms-wma

Archives

File type Mime type
7zip application/x-7z-compressed
Bzip2 application/x-bzip2
Compress application/x-compress
Gzip application/x-gzip
Rar application/x-rar-compressed
Tar application/x-tar
xz application/x-xz
zip application/zip

Office Documents

File type Mime type
Excel (.xls, .xlsx) application/vnd.ms-excel
Keynote application/vnd.apple.keynote
Numbers application/vnd.apple.numbers
OpenOffice .odp application/vnd.oasis.opendocument.presentation
OpenOffice .ods application/vnd.oasis.opendocument.spreadsheet
OpenOffice .odt application/vnd.oasis.opendocument.text
Pages application/vnd.apple.pages
PDF application/pdf
Powerpoint (.ppt, .pptx) application/vnd.ms-powerpoint
Word (.doc, .docx) application/msword

Text

File type Mime type
XML application/xml
CSV text/csv
HTML text/html
RSS application/rss+xml
RTF text/rtf
.tab text/tab-separated-values
XHTML application/xhtml+xml
Unrecognized Text text/plain

Scripts

File type Mime type
Bash/Bourne text/x-shellscript
Perl application/x-perl
PHP application/x-httpd-php
Python application/x-python
Ruby application/x-ruby

Binaries

File type Mime type
DLL application/x-msdownload
Unrecognized Binary application/octet-stream

Running Tests

lua tests.lua

or

luajit tests.lua

puremagic's People

Contributors

wbond avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.