Fix published paths for transcoded media

This commit is contained in:
Abel Luck 2026-03-31 14:14:46 +02:00
parent 3f33994cdc
commit 89d462e280
9 changed files with 956 additions and 114 deletions

View file

@ -2,7 +2,7 @@ import hashlib
import mimetypes
from enum import Enum
from pathlib import Path
from typing import Optional
from typing import Any, Mapping, Optional, Sequence
from scrapy.utils.python import to_bytes
@ -42,6 +42,30 @@ def local_audio_path(s: str) -> str:
return local_file_path(s)
def variant_media_path(base_path: str, profile: Mapping[str, Any]) -> str:
return f"{base_path}-{profile['name']}.{profile['extension']}"
def published_media_path(
file_type: FileType, source_url: str, profile: Mapping[str, Any]
) -> str:
if file_type == FileType.AUDIO:
return variant_media_path(local_audio_path(source_url), profile)
if file_type == FileType.VIDEO:
return variant_media_path(local_video_path(source_url), profile)
raise ValueError(f"Unsupported file type for published media path: {file_type}")
def canonical_published_media_path(
file_type: FileType, source_url: str, profiles: Sequence[Mapping[str, Any]]
) -> str:
if not profiles:
raise ValueError(f"Missing transcode profiles for {file_type.value}")
# The first configured profile is the public URL contract. Reordering profiles
# changes published URLs for already-mirrored media.
return published_media_path(file_type, source_url, profiles[0])
def determine_file_type(
url: str, medium: Optional[str] = None, mimetype: Optional[str] = None
):