Skip to content

Bounded media-type detection

openmed.multimodal.media_type provides a dependency-free preflight check for uploaded or in-memory clinical media. It inspects at most the first 132 bytes and does not use filenames, paths, or extensions.

Supported signatures:

  • PDF
  • PNG and JPEG
  • little-endian and big-endian TIFF
  • GIF (GIF87a and GIF89a)
  • WebP with RIFF and WEBP markers
  • BMP with a complete 14-byte file header and zero reserved fields
  • DICOM Part 10 with the DICM marker at byte offset 128
  • WAV with RIFF and WAVE markers
from openmed.multimodal.media_type import (
    MediaTypeStatus,
    detect_media_type,
    validate_media_type,
)

prefix = b"%PDF-1.7\n"

assert detect_media_type(prefix) == "application/pdf"
assert validate_media_type(prefix, "application/pdf") is MediaTypeStatus.MATCH
assert validate_media_type(prefix, "image/png") is MediaTypeStatus.MISMATCH

Truncated, ambiguous, and unsupported prefixes return None from detect_media_type() and MediaTypeStatus.UNKNOWN from validate_media_type(). Invalid declared types fail closed with a value-free error.

The DICOM Part 10 marker takes precedence over signatures in its arbitrary 128-byte preamble, including GIF and BMP look-alikes.

This preflight check does not fully validate a file, scan for malware, decompress content, or replace format-specific parsers. Callers should pass only the bounded prefix and should not log source bytes.