get-typeandmime
Version:
npm package for generating its file-type on basis of magic numbers!!
68 lines (39 loc) • 1.7 kB
Markdown
> Detect the File type of a Buffer/Uint8Array/ArrayBuffer
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
This package is for detecting binary-based file formats, not text-based formats like `.txt`, `.csv`, `.svg`, etc.
```sh
npm i get-typeandmime
```
**This package is a ES5 module package.But can be used with both ES5 and ES6 as well**
Determine file type from a file:
```js
const {getFileType} = require("get-typeandmime")
const file = "../.path/file.extension"
console.log(getFileType(file));
```
Determine file type from a Buffer, which may be a portion of the beginning of a file:
This can also be used to read from a remote location:
```js
const {getStreamFileType} = require("get-typeandmime")
const remoteFile = "https://static.remove.bg/sample-gallery/graphics/bird-thumbnail.jpg";
(async()=>{
console.log("Remote File Type:",await getStreamFileType(remoteFile));
})()
```
- `signs` - Buffer code of [supported file types](
- `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type)
Or `Unsupported file type` when there is no match.
Type: `Buffer | Uint8Array | ArrayBuffer`
A buffer representing file data. It works best if the buffer contains the entire file. It may work with a smaller portion as well.
file: `string`
The file path to parse.
*[Pull requests](.github/pull_request_template.md) are welcome for additional commonly used file types.*
- [Pratap Shantanu](https://github.com/180896-cse)