zelotech / services /detect.py
matheusalencar's picture
first commit
1b64148
Raw History Blame Contribute Delete
506 Bytes
def sniff_kind(path: str) -> str:
"""Detecta o tipo do arquivo pelos magic bytes, nao pela extensao.
Mais confiavel que confiar no nome/Content-Type enviado pelo cliente.
Retorna "pdf", "image" ou "unknown".
"""
with open(path, "rb") as f:
head = f.read(12)
if head[:5] == b"%PDF-":
return "pdf"
if head[:8] == b"\x89PNG\r\n\x1a\n":
return "image"
if head[:3] == b"\xff\xd8\xff": # JPEG
return "image"
return "unknown"