postkitty-api / app /uploads.py
mertkayacs's picture
deploy with engine 0880b2f6b494fde9171c86c9f7ed13ef49b271ae
d1abf4f verified
Raw History Blame Contribute Delete
8.62 kB
"""What an upload is, decided from its first bytes and then confirmed by Pillow or ffprobe.
Only real image and video containers get through. Playlists, concat scripts and SVG are text that can
make ffmpeg or a browser read local files or fetch URLs, so nothing text-based is accepted. Files are
stored under random names; the client's file name is kept only as a cleaned-up label.
"""
from __future__ import annotations
import json
import secrets
import shutil
import subprocess
from dataclasses import dataclass
from pathlib import Path
from typing import BinaryIO
from PIL import Image
from .config import MB, Limits
from .errors import ApiError
# ISO-BMFF brands of still photos (HEIC, AVIF). They share the container with MP4 but are not video.
_STILL_BRANDS = {b"heic", b"heix", b"heim", b"heis", b"hevc", b"hevx", b"mif1", b"msf1", b"avif", b"avis"}
_PILLOW_FORMAT = {"jpg": "JPEG", "png": "PNG", "gif": "GIF", "webp": "WEBP"}
_FFPROBE_FORMAT = {"mp4": "mov", "mov": "mov", "mkv": "matroska", "webm": "matroska"}
_TYPES_HINT = "Upload a JPEG, PNG, WebP or GIF photo, or an MP4, MOV, MKV or WebM video."
@dataclass
class Upload:
path: Path
name: str
kind: str
width: int
height: int
duration: float | None = None
def sniff(head: bytes) -> tuple[str, str] | None:
"""``(kind, extension)`` when the first bytes are an accepted signature, else ``None``."""
if head.startswith(b"\xff\xd8\xff"):
return "image", "jpg"
if head.startswith(b"\x89PNG\r\n\x1a\n"):
return "image", "png"
if head[:6] in (b"GIF87a", b"GIF89a"):
return "image", "gif"
if head[:4] == b"RIFF" and head[8:12] == b"WEBP":
return "image", "webp"
if head[4:8] == b"ftyp" and head[8:12] not in _STILL_BRANDS:
return "video", "mov" if head[8:12] == b"qt " else "mp4"
if head[4:8] in (b"moov", b"mdat", b"wide"):
return "video", "mov"
if head[:4] == b"\x1a\x45\xdf\xa3":
return "video", "webm" if b"webm" in head[:64] else "mkv"
return None
def kind_of(source: BinaryIO) -> str | None:
"""``image`` or ``video`` from the first bytes of an upload, leaving the stream at its start."""
found = sniff(source.read(64))
source.seek(0)
return found[0] if found else None
def label(name: str | None) -> str:
"""A display name made from the client's file name. It is never used as a path."""
base = (name or "").replace("\\", "/").rsplit("/", 1)[-1]
clean = "".join(char if char.isalnum() or char in "._- " else "_" for char in base).strip(" .")
return clean[:80] or "upload"
def store(source: BinaryIO, name: str | None, folder: Path, limits: Limits) -> Upload:
"""Check one uploaded file and write it into ``folder`` under a random name."""
shown = label(name)
head = source.read(64)
found = sniff(head)
if found is None:
if head[4:8] == b"ftyp":
raise ApiError(415, "unsupported_type", f"{shown} is a HEIC or AVIF photo, which this service does not read.",
"Export the photo as JPEG or PNG and upload that.")
raise ApiError(415, "unsupported_type", f"{shown} is not a photo or video this service accepts.", _TYPES_HINT)
kind, extension = found
size = source.seek(0, 2)
source.seek(0)
cap = limits.photo_bytes if kind == "image" else limits.video_bytes
if size > cap:
what = "photo" if kind == "image" else "video"
raise ApiError(413, "file_too_large",
f"{shown} is {size / MB:.1f} MB, and a {what} can be at most {cap // MB} MB.",
f"Make the {what} smaller and upload it again.")
folder.mkdir(parents=True, exist_ok=True)
path = folder / f"{secrets.token_hex(8)}.{extension}"
with path.open("wb") as out:
shutil.copyfileobj(source, out)
try:
if kind == "image":
return _confirm_image(path, extension, shown, limits)
return _confirm_video(path, extension, shown, limits)
except ApiError:
path.unlink(missing_ok=True)
raise
def store_captions(source: BinaryIO, name: str | None, folder: Path, limits: Limits) -> Upload:
"""Check an uploaded caption file (SRT or WebVTT, UTF-8 text) and write it under a random name."""
shown = label(name)
data = source.read(limits.captions_bytes + 1)
if len(data) > limits.captions_bytes:
raise ApiError(413, "file_too_large", f"{shown} is larger than {limits.captions_bytes // MB} MB, the most a "
"caption file can be.", "Send the captions of this clip only.")
try:
text = data.decode("utf-8-sig")
except UnicodeDecodeError:
text = None
# Caption files are plain text. A control character means this is some other kind of file.
if text is None or any(ord(char) < 32 and char not in "\t\n\r" for char in text):
raise ApiError(422, "captions_not_text", f"{shown} is not UTF-8 text.",
"Save the captions as a UTF-8 .srt or .vtt file and upload that.")
folder.mkdir(parents=True, exist_ok=True)
path = folder / f"{secrets.token_hex(8)}.{'vtt' if text.lstrip().startswith('WEBVTT') else 'srt'}"
path.write_bytes(data)
return Upload(path, shown, "captions", 0, 0)
def _confirm_image(path: Path, extension: str, shown: str, limits: Limits) -> Upload:
try:
# Only the decoder for the sniffed format may open the file, and only the header is read here.
with Image.open(path, formats=[_PILLOW_FORMAT[extension]]) as image:
width, height = image.size
animated = bool(getattr(image, "is_animated", False))
except Image.DecompressionBombError:
width, height, animated = limits.image_pixels + 1, 1, False
except Exception as exc:
raise ApiError(422, "unreadable_file", f"{shown} could not be read as a {_PILLOW_FORMAT[extension]} image.",
"Export it again as JPEG or PNG and upload the new file.") from exc
if width * height > limits.image_pixels:
raise ApiError(422, "image_too_large",
f"{shown} has more than {limits.image_pixels // 1_000_000} megapixels, which is the limit.",
"Resize the photo and upload it again.")
if animated:
raise ApiError(422, "animated_image", f"{shown} is animated, and only still photos are accepted.",
"Export one frame as JPEG or PNG, or upload the clip as a video.")
return Upload(path, shown, "image", width, height)
def _confirm_video(path: Path, extension: str, shown: str, limits: Limits) -> Upload:
# The demuxer is forced and only the file protocol is allowed, so the content cannot pick another
# format or make ffprobe open anything else.
command = ["ffprobe", "-v", "error", "-protocol_whitelist", "file", "-f", _FFPROBE_FORMAT[extension],
"-print_format", "json", "-show_format", "-show_streams", str(path)]
data: dict = {}
try:
done = subprocess.run(command, capture_output=True, text=True, timeout=30, check=False)
if done.returncode == 0:
data = json.loads(done.stdout or "{}")
except (subprocess.TimeoutExpired, json.JSONDecodeError):
pass
video = next((stream for stream in data.get("streams") or []
if stream.get("codec_type") == "video"
and not (stream.get("disposition") or {}).get("attached_pic")), None)
try:
width, height = int(video["width"]), int(video["height"])
duration = float((data.get("format") or {}).get("duration") or video.get("duration"))
except (TypeError, KeyError, ValueError):
width = height = 0
duration = 0.0
if width <= 0 or height <= 0 or duration <= 0:
raise ApiError(422, "unreadable_file", f"{shown} could not be read as a video.",
"Export it again as MP4 (H.264) and upload the new file.")
if max(width, height) > limits.video_side:
raise ApiError(422, "video_too_large",
f"{shown} is {width}x{height}, and the longer side can be at most {limits.video_side} pixels.",
"Export the video at 1080p or 4K and upload it again.")
if duration > limits.video_seconds:
raise ApiError(422, "video_too_long",
f"{shown} runs {duration:.0f} seconds, and the limit is {limits.video_seconds:.0f}.",
f"Trim the clip to {limits.video_seconds:.0f} seconds or less and upload it again.")
return Upload(path, shown, "video", width, height, round(duration, 3))