File size: 13,376 Bytes
851e5ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Stream every clip of a task as one ZIP, without staging it on disk.

The archive is produced on the fly: each clip is read in chunks from local
disk or object storage, written through ``zipfile`` into an in-memory sink, and
the sink is drained after every chunk. Memory stays at about one chunk per
download and nothing is written to disk.

Entries use DEFLATE at level 0 (stored blocks, no compression work; MP4 is
already compressed). Because the output is not seekable, sizes and CRCs go in
data descriptors after each entry. Readers that unpack a ZIP front to back
(Java's ``ZipInputStream``, some archive utilities) cannot find the end of a
STORED entry that has a data descriptor, but they can for a DEFLATE stream,
so this keeps streamed archives readable everywhere for ~0.01% overhead.
"""

from __future__ import annotations

from dataclasses import dataclass
import io
import logging
from pathlib import Path
import re
import secrets
import time
from typing import Any, AsyncIterator, Callable, Iterable
from urllib.parse import quote
import unicodedata
import zipfile

import redis.asyncio as redis

from .config import get_config
from .media_storage import MediaStorage, is_s3
from .utils.async_helpers import run_in_thread

logger = logging.getLogger(__name__)

CHUNK_BYTES = 1024 * 1024
# zipfile switches to ZIP64 records at 2 GiB less a margin; force them early
# for entries that could cross the limit.
ZIP64_THRESHOLD = int(zipfile.ZIP64_LIMIT / 1.05)
SLUG_MAX_CHARS = 60


@dataclass(frozen=True)
class ArchiveEntry:
    name: str
    reference: str
    size: int


class ClipArchiveBusy(Exception):
    pass


def slugify(text: str | None, max_chars: int = SLUG_MAX_CHARS) -> str:
    """Lowercase, filesystem-safe slug that keeps non-Latin letters."""
    normalized = unicodedata.normalize("NFKC", text or "").casefold()
    slug = re.sub(r"[^\w]+", "-", normalized).replace("_", "-")
    slug = re.sub(r"-{2,}", "-", slug).strip("-")
    return slug[:max_chars].rstrip("-")


def archive_entry_names(clips: list[dict[str, Any]]) -> list[str]:
    """``01-hook-title.mp4`` in the given order.

    The position prefix keeps names unique even when hook titles repeat.
    """
    width = max(2, len(str(len(clips))))
    names = []
    for position, clip in enumerate(clips, start=1):
        label = slugify(clip.get("hook_title")) or f"clip-{slugify(str(clip.get('id', '')))[:8] or position}"
        names.append(f"{position:0{width}d}-{label}.mp4")
    return names


# "Part 3: Title" / "Teil 3 – Title": the label the series pipeline prefixes
# onto a part's hook title (same pattern as the task page's partTitle).
_PART_PREFIX_RE = re.compile(r"^\s*\S{0,12}?\s*(\d+)\s*\S{0,3}?\s*[::–—\-·|]\s*")
# Characters Windows, macOS or common unzip tools refuse in a file name.
_UNSAFE_NAME_CHARS_RE = re.compile(r'[\x00-\x1f\x7f<>:"/\\|?*]+')
PART_TITLE_MAX_CHARS = 80


def _part_title(clip: dict[str, Any]) -> str:
    """A part's own title: the planned title, else its hook title without "Part n:"."""
    meta = clip.get("workflow_meta") or {}
    title = (meta.get("title") or "").strip() if isinstance(meta, dict) else ""
    if not title:
        title = (clip.get("hook_title") or "").strip()
        match = _PART_PREFIX_RE.match(title)
        if match and int(match.group(1)) == clip.get("part_number"):
            title = title[match.end():]
    title = unicodedata.normalize("NFKC", title)
    title = re.sub(r"\s+", " ", _UNSAFE_NAME_CHARS_RE.sub(" ", title)).strip()
    if len(title) > PART_TITLE_MAX_CHARS:
        title = title[:PART_TITLE_MAX_CHARS].rsplit(" ", 1)[0] or title[:PART_TITLE_MAX_CHARS]
    # A name may not end in a dot or space on Windows.
    return title.rstrip(". ")


def _part_order(clip: dict[str, Any]) -> tuple[bool, int]:
    number = clip.get("part_number")
    return (not isinstance(number, int), number if isinstance(number, int) else 0)


def order_for_archive(clips: list[dict[str, Any]], workflow: str | None) -> list[dict[str, Any]]:
    """Clips in the order they are meant to be watched.

    A series plays by part number (a split part's halves stay together in
    clip order); everything else keeps clip order.
    """
    if workflow != "series":
        return list(clips)
    return sorted(clips, key=_part_order)


def series_entry_names(clips: list[dict[str, Any]]) -> list[str]:
    """``Part 01 - Title.mp4`` for clips already in part order.

    Names carry the part's own number, so a part whose file is missing leaves
    a gap instead of renumbering the rest. The halves of a split part become
    ``Part 03.1`` and ``Part 03.2``. A clip without a part number (none is
    expected) is named ``Clip NN`` by its position.
    """
    numbers = [clip.get("part_number") for clip in clips]
    known = [number for number in numbers if isinstance(number, int)]
    width = max(2, len(str(max(known, default=0))), len(str(len(clips))))
    halves = {number: known.count(number) for number in known}
    seen: dict[int, int] = {}
    names = []
    for position, (clip, number) in enumerate(zip(clips, numbers), start=1):
        if isinstance(number, int):
            label = f"Part {number:0{width}d}"
            if halves[number] > 1:
                seen[number] = seen.get(number, 0) + 1
                label = f"{label}.{seen[number]}"
        else:
            label = f"Clip {position:0{width}d}"
        title = _part_title(clip)
        names.append(f"{label} - {title}.mp4" if title else f"{label}.mp4")
    return names


def archive_filename(title: str | None) -> str:
    return f"{slugify(title) or 'supoclip'}-clips.zip"


def content_disposition(filename: str) -> str:
    """Attachment header with an ASCII fallback and an RFC 5987 UTF-8 name."""
    ascii_name = unicodedata.normalize("NFKD", filename).encode("ascii", "ignore").decode()
    ascii_name = re.sub(r"[^A-Za-z0-9._-]+", "-", ascii_name)
    ascii_name = re.sub(r"-{2,}", "-", ascii_name).strip("-.") or "supoclip-clips.zip"
    if not ascii_name.endswith(".zip"):
        ascii_name = "supoclip-clips.zip"
    return f"attachment; filename=\"{ascii_name}\"; filename*=UTF-8''{quote(filename, safe='')}"


def resolve_entries(
    clips: list[dict[str, Any]], storage: MediaStorage, workflow: str | None = None
) -> list[ArchiveEntry]:
    """Keep only clips whose file exists, with their size, in watch order. Blocking."""
    present = []
    for clip in order_for_archive(clips, workflow):
        reference = clip.get("file_path") or ""
        if not reference:
            continue
        if is_s3(reference):
            size = storage.object_size(reference)
        else:
            path = Path(reference)
            size = path.stat().st_size if path.is_file() else None
        if size is None:
            logger.warning("Skipping clip %s in archive: file is missing", clip.get("id"))
            continue
        present.append((clip, reference, size))
    kept = [clip for clip, _, _ in present]
    names = series_entry_names(kept) if workflow == "series" else archive_entry_names(kept)
    return [ArchiveEntry(name, reference, size) for name, (_, reference, size) in zip(names, present)]


def open_entry(entry: ArchiveEntry, storage: MediaStorage):
    """A binary reader with ``read(n)`` and ``close()``. Blocking."""
    if is_s3(entry.reference):
        return storage.open_object(entry.reference)
    return open(entry.reference, "rb")


class _ChunkSink:
    """Write-only, non-seekable target that hands written bytes back out."""

    def __init__(self):
        self._chunks: list[bytes] = []
        self._offset = 0

    def write(self, data) -> int:
        data = bytes(data)
        self._chunks.append(data)
        self._offset += len(data)
        return len(data)

    def tell(self) -> int:
        return self._offset

    def seek(self, *_args):
        raise io.UnsupportedOperation("seek")

    def flush(self) -> None:
        pass

    def take(self) -> bytes:
        data = b"".join(self._chunks)
        self._chunks.clear()
        return data


def _entry_info(name: str) -> zipfile.ZipInfo:
    # ZipFile.open(name) would date entries 1980-01-01 and leave no permissions.
    info = zipfile.ZipInfo(name, date_time=time.localtime()[:6])
    info.compress_type = zipfile.ZIP_DEFLATED
    info._compresslevel = 0
    info.external_attr = 0o644 << 16
    return info


def _copy_chunk(source, target, size: int) -> bool:
    data = source.read(size)
    if not data:
        return False
    target.write(data)
    return True


async def stream_archive(
    entries: Iterable[ArchiveEntry],
    opener: Callable[[ArchiveEntry], Any],
    *,
    chunk_bytes: int = CHUNK_BYTES,
    on_chunk: Callable[[], Any] | None = None,
) -> AsyncIterator[bytes]:
    """Yield the ZIP's bytes. Blocking reads and writes run off the event loop.

    A read error mid-stream propagates, so the HTTP response is cut off and the
    browser reports a failed download instead of saving a truncated archive.
    """
    sink = _ChunkSink()
    archive = zipfile.ZipFile(sink, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=0, allowZip64=True)
    for entry in entries:
        source = await run_in_thread(opener, entry)
        try:
            target = archive.open(_entry_info(entry.name), mode="w", force_zip64=entry.size >= ZIP64_THRESHOLD)
            while await run_in_thread(_copy_chunk, source, target, chunk_bytes):
                data = sink.take()
                if data:
                    yield data
                if on_chunk is not None:
                    await on_chunk()
            await run_in_thread(target.close)
        finally:
            # Closing a file or an S3 body does not block; keep it synchronous
            # so it also runs when the client disconnects mid-download.
            source.close()
        data = sink.take()
        if data:
            yield data
    archive.close()
    data = sink.take()
    if data:
        yield data


# --- One archive download at a time per user --------------------------------
#
# Uvicorn may run several API processes or replicas, so the guard lives in
# Redis rather than in process memory. The key carries a random token and a
# short TTL that the stream refreshes while it is sending; a crashed process
# frees its slot within the TTL.

SLOT_TTL_MS = 60_000
SLOT_REFRESH_SECONDS = 15
_RELEASE = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) end return 0"
_REFRESH = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('pexpire', KEYS[1], ARGV[2]) end return 0"


def _slot_key(user_id: str) -> str:
    return f"clip_archive:{user_id}"


def _redis_client():
    config = get_config()
    return redis.Redis(
        host=config.redis_host, port=config.redis_port,
        password=config.redis_password, decode_responses=True,
    )


async def archive_in_progress(user_id: str) -> bool:
    client = _redis_client()
    try:
        return bool(await client.exists(_slot_key(user_id)))
    except Exception as exc:
        logger.warning("Could not check the clip archive slot: %s", exc)
        return False
    finally:
        await client.aclose()


class ArchiveSlot:
    """Held for the lifetime of one streamed archive."""

    def __init__(self, user_id: str):
        self.key = _slot_key(user_id)
        self.token = secrets.token_hex(16)
        self.client = None
        self._refreshed_at = 0.0

    async def acquire(self) -> "ArchiveSlot":
        client = _redis_client()
        try:
            acquired = await client.set(self.key, self.token, nx=True, px=SLOT_TTL_MS)
        except Exception as exc:
            # Fail open: a Redis outage should not block reading one's own clips.
            logger.warning("Could not reserve the clip archive slot: %s", exc)
            await client.aclose()
            return self
        if not acquired:
            await client.aclose()
            raise ClipArchiveBusy("A ZIP download of your clips is already in progress. Wait for it to finish, then try again.")
        self.client = client
        self._refreshed_at = time.monotonic()
        return self

    async def refresh(self) -> None:
        if self.client is None or time.monotonic() - self._refreshed_at < SLOT_REFRESH_SECONDS:
            return
        self._refreshed_at = time.monotonic()
        try:
            await self.client.eval(_REFRESH, 1, self.key, self.token, SLOT_TTL_MS)
        except Exception as exc:
            logger.warning("Could not refresh the clip archive slot: %s", exc)

    async def release(self) -> None:
        client, self.client = self.client, None
        if client is None:
            return
        try:
            await client.eval(_RELEASE, 1, self.key, self.token)
        except Exception as exc:
            logger.warning("Could not release the clip archive slot: %s", exc)
        finally:
            await client.aclose()


async def stream_with_slot(
    entries: list[ArchiveEntry], storage: MediaStorage, slot: ArchiveSlot
) -> AsyncIterator[bytes]:
    try:
        async for chunk in stream_archive(entries, lambda entry: open_entry(entry, storage), on_chunk=slot.refresh):
            yield chunk
    except Exception:
        logger.exception("Clip archive stream failed")
        raise
    finally:
        await slot.release()