Code Monkey home page Code Monkey logo

file-type's Introduction

file-type Build Status

Detect the file type of a Buffer/Uint8Array

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

Install

$ npm install file-type

Usage

Node.js
const readChunk = require('read-chunk');
const fileType = require('file-type');
const buffer = readChunk.sync('unicorn.png', 0, 4100);

fileType(buffer);
//=> {ext: 'png', mime: 'image/png'}

Or from a remote location:

const http = require('http');
const fileType = require('file-type');
const url = 'http://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif';

http.get(url, res => {
	res.once('data', chunk => {
		res.destroy();
		console.log(fileType(chunk));
		//=> {ext: 'gif', mime: 'image/gif'}
	});
});
Browser
const xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';

xhr.onload = () => {
	fileType(new Uint8Array(this.response));
	//=> {ext: 'png', mime: 'image/png'}
};

xhr.send();

API

fileType(input)

Returns an Object with:

Or null when no match.

input

Type: Buffer Uint8Array

It only needs the first 4100 bytes. The exception is detection of docx, pptx, and xlsx which potentially requires reading the whole file.

Supported file types

SVG isn't included as it requires the whole file to be read, but you can get it here.

Pull request welcome for additional commonly used file types.

Related

Created by

License

MIT

file-type's People

Contributors

sindresorhus avatar kevva avatar mifi avatar karlhiramoto avatar borewit avatar arthurvr avatar junmer avatar t1st3 avatar hemanth avatar shinnn avatar nleclerc avatar set-killer avatar thebravyone avatar alrra avatar gillstrom avatar odilitime avatar deepak1556 avatar samverschueren avatar seanmtracey avatar sbugert avatar stomcarlo avatar svenskunganka avatar trexnix avatar villelahdenvuo avatar yagop avatar flienteen avatar magicode avatar medez avatar matthewbauer avatar lolobosse avatar

Watchers

David avatar James Cloos avatar Julie Strothman avatar Michael Knapp avatar Jonathan Golightly (Goley) 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.