GithubHelp home page GithubHelp logo

stream-file-type's Introduction

Stream File Type

Get the file type by inspecting a stream.

Usage

const fs = require('fs')
const FileType = require('stream-file-type')

const input = fs.createReadStream('cat.jpg')
const detector = new FileType()

// Listen for event...
detector.on('file-type', (fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

// ...or get a Promise
detector.fileTypePromise().then((fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

input.pipe(detector).resume()

API

new FileType() => DuplexStream

Returns a new DuplexStream that will detect the file type of the content passing thru. All the data is passed as-is right thru the stream, and can be further piped to another destination.

When enough bytes have come thru to determine the file type (currently 4100) the event file-type will be emitted with the result of the detection. The result will either be null or an object with ext and mime.

FileType#fileTypePromise() => Promise

Returns a Promise of the detected file type. If the file-type event has already been emitted, the promise will be resolved with the result, otherwise the promise will be resolved when the file-type is detected, or rejected if an error occurs.

stream-file-type's People

Contributors

linusu 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.