Giter Club home page Giter Club 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, fileType.minimumBytes);

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, response => {
	response.on('readable', () => {
		const chunk = response.read(fileType.minimumBytes);
		response.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 .minimumBytes bytes. The exception is detection of docx, pptx, and xlsx which potentially requires reading the whole file.

fileType.minimumBytes

Type: number

The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.

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

alexanderlperez avatar alrra avatar amichal avatar arthurflachs avatar arthurvr avatar bjornstar avatar borewit avatar brandonsmith avatar deepak1556 avatar drimer avatar fasterthanlime avatar forivall avatar gillstrom avatar hemanth avatar jacor84 avatar junmer avatar karlhiramoto avatar kevva avatar lolobosse avatar m1k1o avatar matthewbauer avatar mifi avatar nleclerc avatar odilitime avatar paraboul avatar set-killer avatar shinnn avatar sindresorhus avatar t1st3 avatar thebravyone avatar

Watchers

 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.