MIME Type Detector

0.4.7 · active · verified Sun Apr 19

file-type-mime is a JavaScript utility designed to identify the MIME type and file extension of a given file by analyzing its binary content, typically provided as an `ArrayBuffer`. It is currently at version 0.4.7, indicating a pre-1.0 release. While specific release cadence isn't detailed, pre-1.0 versions often imply that the API might still be subject to minor breaking changes between minor versions as the library matures. This library supports a range of common file types including various image formats (BMP, GIF, JPEG, PNG, HEIC, TIFF), documents (PDF, DOCX, XLSX, PPTX, ODT), and archives (ZIP, GZ, RAR), with an optional `extra` flag to detect generic `json` and `txt` types. Its key differentiators include its lightweight nature and its focus on direct buffer analysis without external dependencies, making it suitable for both Node.js environments and browser-based file upload scenarios where direct file content analysis is required.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `file-type-mime` in a Node.js environment to parse the MIME type from a file's `ArrayBuffer`, including an example of using the `extra` option for generic text detection.

import { parse } from "file-type-mime";
import { readFileSync, existsSync } from "node:fs";
import { resolve } from "node:path";

// Create a dummy PDF file for demonstration if it doesn't exist
const dummyFilePath = resolve("./dummy.pdf");
if (!existsSync(dummyFilePath)) {
  // In a real scenario, you'd have an actual file. This is just for a runnable example.
  // For simplicity, we'll create a minimal valid PDF header.
  // This won't be a full PDF, but enough to be detected by some tools.
  const dummyPdfContent = Buffer.from('%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n2 0 obj<</Type/Pages/Count 0>>endobj\nxref\n0 3\n0000000000 65535 f\n0000000009 00000 n\n0000000057 00000 n\ntrailer<</Size 3/Root 1 0 R>>startxref\n106\n%%EOF');
  // For actual testing, replace with a real PDF or path to one.
  // require('node:fs').writeFileSync(dummyFilePath, dummyPdfContent);
  // For this runnable example, we'll just use a direct buffer.
  console.log('No dummy.pdf found. Using a synthetic buffer for demonstration.');
}

// Simulate reading a file buffer (replace with actual file path if desired)
const fileBuffer = Buffer.from('%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n2 0 obj<</Type/Pages/Count 0>>endobj\nxref\n0 3\n0000000000 65535 f\n0000000009 00000 n\n0000000057 00000 n\ntrailer<</Size 3/Root 1 0 R>>startxref\n106\n%%EOF'); // Example PDF header

const result = parse(fileBuffer.buffer); // Pass ArrayBuffer from Node.js Buffer

if (result) {
  console.log(`Detected MIME Type: ${result.mime}`);
  console.log(`Detected File Extension: ${result.ext}`);
} else {
  console.log('Could not detect file type.');
}

// Example with `extra` option for text/plain
const textBuffer = Buffer.from('This is a plain text file.');
const textResult = parse(textBuffer.buffer, { extra: true });
if (textResult) {
  console.log(`\nDetected MIME Type (with extra): ${textResult.mime}`);
} else {
  console.log('Could not detect text file type with extra option.');
}

view raw JSON →