feat(downloads): a clip or an edit can be a GIF, shared as a picture

A clip and an editor range can come out as a silent GIF (long side
320/480/640, 12 fps, at most 30 s, one palette per GIF); one over 30 MB
is not kept. A shared picture is served inline under the preview's
sandbox, and a GIF's link card is the GIF itself up to 15 MB, the still
for Facebook's crawler.
This commit is contained in:
2026-11-11 01:50:55 +01:00
parent fe38c07e7e
commit c221452a00
24 changed files with 885 additions and 58 deletions
+28 -5
View File
@@ -23,6 +23,7 @@ from app.downloads import adapters
from app.downloads import clip as clipmod
from app.downloads import credentials
from app.downloads import edit as editmod
from app.downloads import gif as gifmod
from app.downloads import folders, formats, mediakind, quota, storage
from app.downloads.status import HOLDING, JobKind, JobStatus
from app.models import DISPLAY_TEXT_MAX, User
@@ -42,14 +43,18 @@ from app.userscope import owned_by, owned_row
# Every reason an edit is refused; the UI names each one (`downloads.errors.edit.<reason>`, held by
# the frontend's backendIds contract, which reads this Literal).
EditReason = Literal["source_not_ready", "not_media", "empty_edit"]
EditReason = Literal[
"source_not_ready", "not_media", "empty_edit", "bad_gif", "gif_range", "gif_too_long"
]
class EditError(Exception):
"""Bad edit request (empty spec / source not ready / not media); `reason` names the sentence."""
"""Bad edit request (empty spec / source not ready / not media / a GIF it cannot make); `reason`
names the sentence, `params` the numbers it names (a cap travels with its refusal)."""
def __init__(self, reason: EditReason):
def __init__(self, reason: EditReason, **params: float):
self.reason: EditReason = reason
self.params = params
super().__init__(reason)
@@ -255,6 +260,18 @@ def clip_title(source_title: str | None, display_name: str | None) -> str:
return f"{base} (clip)"[:DISPLAY_TEXT_MAX]
def _check_gif_edit(spec: dict) -> None:
"""A GIF edit is one marked range with an end, at most `gif.MAX_S` long, of a size `gif` offers
(a size it does not offer was dropped by `normalize_edit_spec`). Raises EditError."""
if not spec.get("gif"):
raise EditError("bad_gif")
trim = spec.get("trim") or {}
if spec.get("segments") or trim.get("end_s") is None:
raise EditError("gif_range")
if trim["end_s"] - trim.get("start_s", 0.0) > gifmod.MAX_S:
raise EditError("gif_too_long", limit_s=gifmod.MAX_S)
def enqueue_edit(
db: Session,
user_id: int,
@@ -280,6 +297,8 @@ def enqueue_edit(
spec = editmod.normalize_edit_spec(edit_spec)
if not spec:
raise EditError("empty_edit")
if "gif" in (edit_spec or {}):
_check_gif_edit(spec)
quota.check_enqueue(db, user_id)
@@ -294,7 +313,10 @@ def enqueue_edit(
asset.duration_s = editmod.clip_duration(spec, src_asset.duration_s)
# A clip inherits its source's kind (a video edit stays video, an audio trim stays audio);
# the ffmpeg edit worker doesn't re-probe codecs, so set it here where the source is known.
asset.media_kind = src_asset.media_kind or mediakind.VIDEO
# A GIF is a picture whatever it was cut from (downloads/gif).
asset.media_kind = (
mediakind.FILE if spec.get("gif") else src_asset.media_kind or mediakind.VIDEO
)
job = DownloadJob(
user_id=user_id,
@@ -384,7 +406,8 @@ def enqueue_clip(
asset.uploader = facts["uploader"]
asset.thumbnail_url = facts["thumbnail_url"]
asset.duration_s = round(clipmod.duration(spec))
asset.media_kind = mediakind.VIDEO
# A GIF is a picture: no player and no editor, an image preview (downloads/gif).
asset.media_kind = mediakind.FILE if spec.get("gif") else mediakind.VIDEO
job = DownloadJob(
user_id=user_id,