Gathering detailed insights and metrics for mimetics
Gathering detailed insights and metrics for mimetics
mimetics
Mimetics determines the file type, MIME type, and media type of a given file using magic numbers and content analysis to detect the most likely file type and then maps that to the appropriate MIME and media type.
Mimetics identifies file types based on magic bytes, patterns, and other unique file attributes. It provides an intuitive API for both synchronous and asynchronous file type detection, supporting various file formats including text, image, audio, video, and archive types.
Installation
To add Mimetics to your project, use:
1npm install mimetics
Usage
Mimetics allows you to detect file types from file buffers, file names, and even File objects in browser environments. You can also extend its functionality by adding custom definitions.
Basic Example
1const Mimetics = require('mimetics')
2const fs = require('fs')
34// Load a buffer from a file (e.g., sample.txt)5const buffer = fs.readFileSync('sample.txt')
6const mimetics = new Mimetics()
78// Synchronous file type detection9const fileType = mimetics.parse(buffer)
10console.log(fileType) // Output: { tag: 'text', type: 'text', ext: 'txt', mime: 'text/plain' }1112// Asynchronous file type detection (for ZIP files, etc.)13mimetics.parseAsync(buffer).then(fileType => {
14console.log(fileType)
15})
Adding Custom Definitions
You can extend Mimetics with custom definitions for additional file types.
12const Mimetics = require('mimetics')
34const customDefinitions = [
5 {
6tag: 'custom',
7type: 'myfiletype',
8ext: 'myft',
9mime: 'application/x-myfiletype',
10magic: [0x12, 0x34, 0x56],
11pattern: /^MYFILE/i,
12 }
13]
1415const mimetics = new Mimetics()
16mimetics.addDefinitions(customDefinitions)
1718const buffer = /* load a buffer for a custom file type */19const fileType = mimetics.parse(buffer)
20console.log(fileType) // Output should match custom definition