Files
stork/stork/audio.py
T
ilia 480e5b6ce6
CI / secret-scan (pull_request) Successful in 33s
CI / python-ci (pull_request) Successful in 1m8s
Harden Stork for public demo and family durability
Read-only demo boards, per-IP rate limits, opaque voter sessions,
session leave, upload sniffing, path containment, and retire /v1.
2026-08-07 20:55:21 -04:00

33 lines
984 B
Python

"""Audio upload sniffing helpers."""
from __future__ import annotations
def sniff_audio(data: bytes) -> str | None:
"""Return a normalized content-type if magic bytes match a known container."""
if len(data) < 12:
return None
if data[:4] == b"\x1a\x45\xdf\xa3":
return "audio/webm"
if data[:4] == b"OggS":
return "audio/ogg"
if data[:4] == b"RIFF" and data[8:12] == b"WAVE":
return "audio/wav"
if data[:3] == b"ID3" or (data[0] == 0xFF and (data[1] & 0xE0) == 0xE0):
return "audio/mpeg"
if data[4:8] == b"ftyp":
return "audio/mp4"
return None
def ext_for_content_type(content_type: str) -> str:
if "ogg" in content_type:
return "ogg"
if "mp4" in content_type or "m4a" in content_type or "aac" in content_type:
return "m4a"
if "mpeg" in content_type or content_type == "audio/mp3":
return "mp3"
if "wav" in content_type:
return "wav"
return "webm"