Know thy files
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.
To add Mimetics to your project, use:
npm install mimeticsMimetics 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.
constMimetics=require('mimetics')constfs=require('fs')// Load a buffer from a file (e.g., sample.txt)constbuffer=fs.readFileSync('sample.txt')constmimetics=newMimetics()// Synchronous file type detectionconstfileType=mimetics.parse(buffer)console.log(fileType)// Output: { tag: 'text', type: 'text', ext: 'txt', mime: 'text/plain' }// Asynchronous file type detection (for ZIP files, etc.)mimetics.parseAsync(buffer).then(fileType=>{console.log(fileType)})You can extend Mimetics with custom definitions for additional file types.
constMimetics=require('mimetics')constcustomDefinitions=[{tag: 'custom',type: 'myfiletype',ext: 'myft',mime: 'application/x-myfiletype',magic: [0x12,0x34,0x56],pattern: /^MYFILE/i,}]constmimetics=newMimetics()mimetics.addDefinitions(customDefinitions)constbuffer=/* load a buffer for a custom file type */constfileType=mimetics.parse(buffer)console.log(fileType)// Output should match custom definitionconstMimetics=require('mimetics')constfileInput=document.querySelector('#fileInput')fileInput.addEventListener('change',async(event)=>{constfile=event.target.files[0]constmimetics=newMimetics()constfileType=awaitmimetics.fromFile(file)console.log(fileType)// Output: file type information})Mimetics currently supports a variety of formats, including but not limited to:
- Text: Plain text (
txt), Markdown (md), LaTeX (tex), RTF (rtf) - Image: JPEG (
jpg,jpeg), PNG (png), GIF (gif), BMP (bmp), ICON (ico), WebP (webp), PDF (pdf), SVG (svg) - Audio: MP3 (
mp3), OGG (ogg), WAV (wav) - Video: MP4 (
mp4), QuickTime (mov), AVI (avi), MKV (mkv), WebM (webm), FLV (flv) - Archive: ZIP (
zip), RAR (rar), GZIP (gz), 7ZIP (7z) - Ebook: EPUB (
epub)
Synchronously parses a buffer to identify the file type.
Parameters:
buffer(Uint8Array | ArrayBuffer): The file buffer to parse.name(string, optional): The file name, which can help in detection.
Returns: File type object or
nullif no match is found.
Asynchronously parses a buffer to identify the file type, with support for ZIP archive analysis.
Parameters:
buffer(Uint8Array | ArrayBuffer): The file buffer to parse.name(string, optional): The file name, which can assist in detection.
Returns: A promise resolving to a file type object or
nullif no match is found.
Determines file type based on the file name extension.
Parameters:
filePath(string): The file path or name.
Returns: File type object based on the file extension or
nullif no match is found.
Asynchronously determines the file type from a File object (for browser use).
Parameters:
file(File): File object to analyze.
Returns: A promise resolving to a file type object.
Adds custom file definitions to the existing set.
- Parameters:
definitions(Array): Array of custom file definitions to add, with each object containing properties liketag,type,ext,mime,magic, andpattern.For more detailed API documentation, see the API reference and the comments in the code.
Contributions are welcome. Submit a Pull Request or open an Issue to discuss any changes. Please read contributing.md for details on our code of conduct, and the process for submitting merge requests to us.
Mimetics includes a test suite built with testr.
To run the test, first clone the respository:
git clone https://github.com/basedwon/mimetics.git
Install the dependencies, then run
npm test:npm install npm testIf you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!
Bitcoin (BTC):
1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdFMonero (XMR):
46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQMimetics is MIT licensed.