PhxMediaLibrary.MimeDetector.Default (PhxMediaLibrary v0.6.1)

Copy Markdown View Source

Default MIME type detector using magic byte signatures.

Detects common file types by inspecting the first bytes of file content. Covers images, documents, audio, video, and archive formats. When content doesn't match any known signature, returns {:error, :unrecognized} so callers can fall back to extension-based detection.

Supported Formats

Images

JPEG, PNG, GIF, WebP, BMP, TIFF, ICO, AVIF, HEIC/HEIF, SVG

Documents

PDF, ZIP (and ZIP-based formats: DOCX, XLSX, PPTX), Microsoft Office legacy (DOC, XLS, PPT), RTF, XML

Audio

MP3 (ID3v2), OGG, FLAC, WAV, AIFF, AAC (ADTS), MIDI

Video

MP4/M4V/M4A (ftyp), AVI, MKV/WebM (Matroska/EBML), FLV, MOV

Archives

ZIP, GZIP, BZIP2, 7-Zip, RAR, XZ, TAR, Zstandard

Other

WASM, SQLite, ELF executables, Mach-O executables, PE executables (EXE/DLL)

Summary

Functions

Detect MIME type from binary content using magic byte signatures.

Functions

detect(content, filename)

@spec detect(binary(), String.t()) :: {:ok, String.t()} | {:error, :unrecognized}

Detect MIME type from binary content using magic byte signatures.

Inspects the first bytes of content to identify the file type. The filename parameter is currently unused but available for future heuristics (e.g. differentiating ZIP-based formats by extension).

Returns {:ok, mime_type} or {:error, :unrecognized}.