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:
+44
-11
@@ -14,8 +14,9 @@ from pathlib import Path
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import settings
|
||||
from app.downloads import gif as gifmod
|
||||
from app.downloads import links as linksmod
|
||||
from app.downloads import storage
|
||||
from app.downloads import preview, storage
|
||||
from app.downloads.models import DownloadJob, DownloadLink, FileFolder, MediaAsset
|
||||
|
||||
_OG_BLOCK = re.compile(r"<!--OG:START-->.*?<!--OG:END-->", re.DOTALL)
|
||||
@@ -71,8 +72,33 @@ def _jpeg_size(path: Path) -> tuple[int, int] | None:
|
||||
return None
|
||||
|
||||
|
||||
def _watch_tags(token: str, db: Session) -> str | None:
|
||||
"""Build the OG/Twitter meta block for a watch token, or None to keep the generic card."""
|
||||
# Facebook's crawler (Messenger's previews): it never showed a GIF `og:image` and did show a JPEG one
|
||||
# (measured 2026-11-11), so it is given the still. Matched on its documented agent names.
|
||||
_FACEBOOK_AGENTS = ("facebookexternalhit", "facebot")
|
||||
|
||||
|
||||
def _is_facebook(user_agent: str | None) -> bool:
|
||||
agent = (user_agent or "").lower()
|
||||
return any(name in agent for name in _FACEBOOK_AGENTS)
|
||||
|
||||
|
||||
def _moving_image(
|
||||
token: str, asset: MediaAsset, user_agent: str | None
|
||||
) -> tuple[str, tuple[int, int] | None] | None:
|
||||
"""The shared GIF itself as the card's picture, and its size from its header — when a chat can
|
||||
move it: not for Facebook's crawler, and not over `gif.PREVIEW_MAX_BYTES` (Teams moved 15 MB and
|
||||
showed nothing at 22). None: the card takes the still, as any other file's does."""
|
||||
if preview.kind(asset.media_kind, asset.container) != "image" or asset.container != "gif":
|
||||
return None
|
||||
if _is_facebook(user_agent) or (asset.size_bytes or 0) > gifmod.PREVIEW_MAX_BYTES:
|
||||
return None
|
||||
path = storage.safe_abs_path(settings.files_root, asset.rel_path)
|
||||
return f"{settings.app_base}/api/public/watch/{token}/file", gifmod.size(path) if path else None
|
||||
|
||||
|
||||
def _watch_tags(token: str, db: Session, user_agent: str | None = None) -> str | None:
|
||||
"""Build the OG/Twitter meta block for a watch token, or None to keep the generic card.
|
||||
`user_agent` is the crawler's: a GIF's card is the GIF for a chat that moves it (`_moving_image`)."""
|
||||
link = linksmod.find(db, token)
|
||||
if link is None or linksmod.is_expired(link):
|
||||
return None
|
||||
@@ -112,7 +138,12 @@ def _watch_tags(token: str, db: Session) -> str | None:
|
||||
# non-password links, so the poster needs no grant.
|
||||
yt_id_match = re.search(r"i\.ytimg\.com/vi/([\w-]+)/", asset.thumbnail_url or "")
|
||||
image_dims: tuple[int, int] | None = None
|
||||
if yt_id_match:
|
||||
image_type = "image/jpeg"
|
||||
moving = _moving_image(token, asset, user_agent)
|
||||
if moving:
|
||||
image, image_dims = moving
|
||||
image_type = "image/gif"
|
||||
elif yt_id_match:
|
||||
image: str | None = f"https://i.ytimg.com/vi/{yt_id_match.group(1)}/hqdefault.jpg"
|
||||
image_dims = (480, 360)
|
||||
elif asset.poster_path:
|
||||
@@ -133,12 +164,12 @@ def _watch_tags(token: str, db: Session) -> str | None:
|
||||
if image:
|
||||
# A real thumbnail → a large image card; without one, a plain (text) card is served.
|
||||
tags.append(_tag("og:image", image))
|
||||
# Declare the image is a public HTTPS JPEG with known dimensions, so FB (which fetches
|
||||
# og:image asynchronously on first scrape) includes it instead of unfurling title-only. For a
|
||||
# poster we read the real dimensions from its JPEG header; the ytimg hqdefault is a fixed 480×360.
|
||||
# Declare the image's type and known dimensions, so FB (which fetches og:image
|
||||
# asynchronously on first scrape) includes it instead of unfurling title-only. For a poster
|
||||
# we read the real dimensions from its JPEG header; the ytimg hqdefault is a fixed 480×360.
|
||||
if image.startswith("https://"):
|
||||
tags.append(_tag("og:image:secure_url", image))
|
||||
tags.append(_tag("og:image:type", "image/jpeg"))
|
||||
tags.append(_tag("og:image:type", image_type))
|
||||
if image_dims is None and image.endswith("poster.jpg") and asset.poster_path:
|
||||
# safe_abs_path returns None if the poster file is missing (gc'd) or escapes the root —
|
||||
# guard it, else _jpeg_size(None) would raise an uncaught AttributeError on this public page.
|
||||
@@ -178,11 +209,13 @@ def _folder_tags(db: Session, link: DownloadLink, url: str) -> str | None:
|
||||
)
|
||||
|
||||
|
||||
def render_watch_html(token: str, page_html: Path, db: Session) -> str | None:
|
||||
def render_watch_html(
|
||||
token: str, page_html: Path, db: Session, user_agent: str | None = None
|
||||
) -> str | None:
|
||||
"""Return the watch page (watch.html) with the OG block replaced for this token, or None to
|
||||
serve the generic page unchanged (invalid/expired/not-ready link, or a template without the
|
||||
markers)."""
|
||||
tags = _watch_tags(token, db)
|
||||
markers). `user_agent` is the fetcher's, which picks a GIF's card picture."""
|
||||
tags = _watch_tags(token, db, user_agent)
|
||||
if tags is None:
|
||||
return None
|
||||
text = page_html.read_text(encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user