File size: 17,648 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""Retry committed deletions and reclaim media left by interrupted jobs."""

from contextlib import nullcontext
import logging
from pathlib import Path, PurePosixPath
import time
import re

from sqlalchemy import text

from ..database import AsyncSessionLocal
from ..media_storage import MediaStorage, VIDEO_SUFFIXES, STORAGE_ERRORS, is_s3
from botocore.exceptions import ClientError
from ..editor_document import editor_dir
from ..repositories.media_repository import source_guard, queue_delete
from ..youtube_utils import get_youtube_video_id
from ..utils.async_helpers import run_in_thread

logger = logging.getLogger(__name__)


def source_url(reference: str, storage: MediaStorage) -> str | None:
    if is_s3(reference):
        key = storage.parse(reference)
        prefix = storage.key("sources") + "/"
        return "upload://s3/" + key.removeprefix(prefix) if key.startswith(prefix) else None
    path = Path(reference)
    if path.parent.resolve() == (Path(storage.config.temp_dir) / "uploads").resolve():
        return f"upload://{path.name}"
    return None


async def reference_index(db, storage):
    rows = await db.execute(text("SELECT id, task_id, filename, file_path FROM generated_clips"))
    rows = list(rows)
    editors = {editor_dir(storage.config.temp_dir, row.task_id, row.id, row.filename).name for row in rows}
    clips = {row.file_path for row in rows}
    # A post being published may still upload a render the clip already replaced.
    rows = await db.execute(text(
        "SELECT source_file_path FROM social_posts WHERE status = 'publishing' AND source_file_path IS NOT NULL"
    ))
    clips |= {row.source_file_path for row in rows}
    rows = await db.execute(text("SELECT DISTINCT s.url, s.type FROM sources s JOIN tasks t ON t.source_id = s.id"))
    rows = list(rows)
    sources = {row.url for row in rows}
    videos = {get_youtube_video_id(row.url) for row in rows if row.type == "youtube" and row.url}
    # A stored upload can be turned into a task during the session lifetime.
    # Its local file and object copy stay until that window closes or a task
    # records the same reference.
    rows = await db.execute(text("""
        SELECT video_path FROM media_upload_sessions
        WHERE state = 'completed' AND video_path IS NOT NULL
    """))
    sources |= {row.video_path for row in rows}
    rows = await db.execute(text("""
        SELECT storage_reference FROM media_upload_sessions
        WHERE state = 'completed' AND storage_reference IS NOT NULL
        UNION
        SELECT source_metadata->>'storage_reference' FROM tasks
        WHERE source_metadata->>'storage_reference' IS NOT NULL
    """))
    storage_refs = _with_capped_companions(storage, {row.storage_reference for row in rows})
    return {
        "editors": editors,
        "videos": videos,
        "clips": clips,
        "sources": sources,
        "storage": storage_refs,
    }


def _with_capped_companions(storage: MediaStorage, references: set[str]) -> set[str]:
    """Keep a 1080p master that sits beside a still-referenced original."""
    expanded = set(references)
    for reference in references:
        if not is_s3(reference):
            continue
        try:
            capped = storage.capped_source_key(storage.parse(reference))
        except ValueError:
            continue
        if capped:
            expanded.add(storage.reference(capped))
    return expanded


async def referenced(db, reference: str, storage: MediaStorage, *, prefix=False, index=None) -> bool:
    if prefix:
        if is_s3(reference):
            key = storage.parse(reference)
            if key.startswith(storage.key("clips") + "/"):
                return bool((await db.execute(text(
                    "SELECT EXISTS(SELECT 1 FROM generated_clips WHERE starts_with(file_path, :prefix))"
                ), {"prefix": reference.rstrip("/") + "/"})).scalar())
            if key.startswith(storage.key("sources") + "/"):
                url_prefix = "upload://s3/" + key.removeprefix(storage.key("sources") + "/").rstrip("/") + "/"
                return bool((await db.execute(text(
                    "SELECT EXISTS(SELECT 1 FROM sources s JOIN tasks t ON t.source_id = s.id WHERE starts_with(s.url, :prefix))"
                ), {"prefix": url_prefix})).scalar())
        name = PurePosixPath(reference).name
        live = index if index is not None else await reference_index(db, storage)
        return name in live["editors"]
    if is_s3(reference) and storage.parse(reference).startswith(storage.key("transcripts") + "/"):
        video_id = PurePosixPath(reference).name.removesuffix(".transcript_cache.json")
        live = index if index is not None else await reference_index(db, storage)
        return video_id in live["videos"]
    url = source_url(reference, storage)
    if index is not None:
        return (
            reference in index["clips"]
            or url in index["sources"]
            or reference in index.get("storage", ())
        )
    candidates = _companion_originals(storage, reference)
    result = await db.execute(text("""
        SELECT EXISTS(SELECT 1 FROM generated_clips WHERE file_path = :path)
            OR EXISTS(SELECT 1 FROM sources s JOIN tasks t ON t.source_id = s.id WHERE s.url = :url)
            OR EXISTS(SELECT 1 FROM social_posts WHERE status = 'publishing' AND source_file_path = :path)
            OR EXISTS(
                SELECT 1 FROM media_upload_sessions
                WHERE state = 'completed' AND (video_path = :url OR storage_reference = ANY(CAST(:candidates AS text[])))
            )
            OR EXISTS(
                SELECT 1 FROM tasks
                WHERE source_metadata->>'storage_reference' = ANY(CAST(:candidates AS text[]))
            )
    """), {"path": reference, "url": url, "candidates": candidates})
    return bool(result.scalar())


def _companion_originals(storage: MediaStorage, reference: str) -> list[str]:
    """The object itself, plus the original when this is its capped master."""
    found = [reference]
    if not is_s3(reference):
        return found
    try:
        key = storage.parse(reference)
    except ValueError:
        return found
    name = PurePosixPath(key).name
    marker = ".capped.mp4"
    if not name.endswith(marker):
        return found
    stem = name[: -len(marker)]
    parent = PurePosixPath(key).parent
    for suffix in VIDEO_SUFFIXES:
        found.append(storage.reference(str(parent / f"{stem}{suffix}")))
    return found


async def drain_deletions(db, storage: MediaStorage, *, limit=100):
    for _ in range(limit):
        result = await db.execute(text("""
            SELECT file_path, is_prefix FROM media_deletions
            WHERE hold_token IS NULL AND next_attempt_at <= clock_timestamp()
            ORDER BY next_attempt_at, created_at FOR UPDATE SKIP LOCKED LIMIT 1
        """))
        item = result.first()
        if item is None:
            await db.rollback()
            break
        reference, prefix = item.file_path, item.is_prefix
        try:
            url = source_url(reference, storage)
            guard = source_guard(db, url) if url else nullcontext()
            async with guard:
                if not await referenced(db, reference, storage, prefix=prefix):
                    await run_in_thread(storage.delete, reference, prefix=prefix)
                await db.execute(text("DELETE FROM media_deletions WHERE file_path = :path"), {"path": reference})
                await db.commit()
        except Exception:
            await db.rollback()
            logger.exception("Media deletion failed; retained for retry")
            await db.execute(text("""
                UPDATE media_deletions SET attempts = attempts + 1,
                    next_attempt_at = clock_timestamp() + make_interval(secs => LEAST(21600, 60 * power(2, LEAST(attempts, 9)))::integer)
                WHERE file_path = :path
            """), {"path": reference})
            await db.commit()


def orphan_candidates(storage: MediaStorage):
    """Group files by video/directory, avoiding separate deletion of sidecars."""
    cutoff = time.time() - storage.config.media_orphan_grace_seconds
    root = Path(storage.config.temp_dir)
    for folder in ("uploads", "clips", "exports"):
        directory = root / folder
        if directory.exists():
            for path in entries(directory):
                if path.is_file() and path.suffix.lower() in VIDEO_SUFFIXES and modified(path) < cutoff:
                    yield str(path), False
    if root.exists():
        for path in entries(root):
            if modified(path) >= cutoff:
                continue
            if path.is_file() and re.fullmatch(r"[A-Za-z0-9_-]{11}\.(?:mp4|webm|mkv|m4a|transcription\.mp3|assemblyai\.mp3|part)", path.name):
                yield str(path), False
            # Reclaim interrupted staging directories, including legacy providers.
            elif path.is_dir() and path.name.startswith(("proxy-", "apify-", "rapidapi-", "oxy-", "videoscale-", "ytdl-")):
                if max((modified(p) for p in entries(path)), default=0) < cutoff:
                    yield str(path), True
    if storage.enabled:
        for page in storage.client.get_paginator("list_objects_v2").paginate(Bucket=storage.bucket, Prefix=storage.key("transcripts") + "/"):
            for obj in page.get("Contents", []):
                if obj["LastModified"].timestamp() < cutoff:
                    yield storage.reference(obj["Key"]), False
        for kind in ("sources", "clips"):
            groups = {}
            for page in storage.client.get_paginator("list_objects_v2").paginate(Bucket=storage.bucket, Prefix=storage.key(kind) + "/"):
                for obj in page.get("Contents", []):
                    parent = str(PurePosixPath(obj["Key"]).parent)
                    group = groups.setdefault(parent, {"latest": 0, "has_video": False})
                    group["latest"] = max(group["latest"], obj["LastModified"].timestamp())
                    group["has_video"] |= PurePosixPath(obj["Key"]).suffix.lower() in VIDEO_SUFFIXES
                    if PurePosixPath(obj["Key"]).suffix.lower() in VIDEO_SUFFIXES and obj["LastModified"].timestamp() < cutoff:
                        yield storage.reference(obj["Key"]), False
            # A process may stop after uploading metadata but before its video.
            # Reclaim those otherwise invisible partial object groups too.
            for parent, group in groups.items():
                # Only an individual upload/render directory is eligible.
                # Unexpected flat keys must never cause a namespace deletion.
                relative_parts = PurePosixPath(parent).relative_to(PurePosixPath(storage.key(kind))).parts
                if len(relative_parts) == 2 and not group["has_video"] and group["latest"] < cutoff:
                    yield storage.reference(parent), True
        groups = {}
        prefix = storage.key("editor") + "/"
        for page in storage.client.get_paginator("list_objects_v2").paginate(Bucket=storage.bucket, Prefix=prefix):
            for obj in page.get("Contents", []):
                directory = obj["Key"].removeprefix(prefix).split("/")[0]
                groups[directory] = max(groups.get(directory, 0), obj["LastModified"].timestamp())
        for directory, last_write in groups.items():
            if last_write < cutoff:
                yield storage.reference(prefix + directory), True
    editor = root / "editor"
    if editor.exists():
        for directory in entries(editor):
            if directory.is_dir() and max((modified(p) for p in entries(directory)), default=0) < cutoff:
                yield str(directory), True


def entries(directory):
    try:
        return list(directory.iterdir())
    except FileNotFoundError:
        return []


def modified(path):
    try:
        return path.stat().st_mtime
    except FileNotFoundError:
        return time.time()


async def cleanup_media(ctx):
    storage = MediaStorage()
    async with AsyncSessionLocal() as db:
        await drain_deletions(db, storage)
        await cleanup_upload_sessions(db, storage)
        from ..services.clip_export_service import expire_clip_exports

        await expire_clip_exports(db, storage)
        await drain_deletions(db, storage)
    await run_in_thread(storage.sweep_cache)
    await run_in_thread(storage.sync_source_metadata)
    await run_in_thread(storage.sync_editor_drafts)
    await run_in_thread(storage.sweep_editor_cache)


def _finished_local_upload(storage, video_path: str) -> Path | None:
    """Uploads-directory file for a local ``upload://`` path, using this storage's temp dir."""
    if not isinstance(video_path, str) or not video_path.startswith("upload://") or video_path.startswith("upload://s3/"):
        return None
    name = video_path.removeprefix("upload://")
    if not name or name != Path(name).name or name in {".", ".."}:
        return None
    return Path(storage.config.temp_dir) / "uploads" / name


def _delete_finished_local_upload(storage, video_path: str) -> None:
    path = _finished_local_upload(storage, video_path)
    if path is None:
        return
    storage.delete(str(path))
    storage.delete(str(path.with_name(f"{path.stem}.capped.mp4")))


async def _upload_still_unreferenced(db, video_path: str | None) -> bool:
    if not video_path:
        return True
    referenced = await db.execute(text("""
        SELECT EXISTS (
            SELECT 1 FROM sources s
            JOIN tasks t ON t.source_id = s.id
            WHERE s.url = :path
        )
    """), {"path": video_path})
    return not bool(referenced.scalar())


async def _expire_one_upload_session(db, storage, row) -> None:
    """Delete one finished session unless a task source appears while we wait.

    Task creation holds ``source_guard`` across the source insert and its
    commit. Taking that same lock here, then checking again, means expiry
    cannot remove a file whose task source has already committed, and cannot
    pass a task create that is still inside the guard.
    """
    video_path = row.video_path or ""
    guard = source_guard(db, video_path) if video_path.startswith("upload://") else nullcontext()
    async with guard:
        if not await _upload_still_unreferenced(db, video_path):
            return
        try:
            await run_in_thread(_delete_finished_local_upload, storage, video_path)
        except (OSError, ValueError):
            return
        await db.execute(text("DELETE FROM media_upload_sessions WHERE id = :id"), {"id": row.id})
        await db.commit()


async def _expire_finished_upload_sessions(db, storage) -> None:
    """Drop completed and aborted sessions after two days, and their local files.

    A session whose ``video_path`` is still a task source stays. That row is the
    ownership record, and the file is still in use. Local storage has no bucket,
    so this expiry runs even when the multipart-abort loop does not.
    """
    rows = (await db.execute(text("""
        SELECT id, video_path FROM media_upload_sessions
        WHERE state IN ('completed', 'aborted')
          AND created_at < NOW() - INTERVAL '2 days'
          AND NOT EXISTS (
              SELECT 1 FROM sources s
              JOIN tasks t ON t.source_id = s.id
              WHERE s.url = media_upload_sessions.video_path
          )
    """))).all()
    # Release this read before waiting on a task create. The locked re-check
    # below is what decides whether the file is still free.
    await db.rollback()
    for row in rows:
        await _expire_one_upload_session(db, storage, row)
    await db.rollback()


async def cleanup_upload_sessions(db, storage):
    if storage.bucket:
        await _abort_expired_multipart_uploads(db, storage)
    await _expire_finished_upload_sessions(db, storage)


async def _abort_expired_multipart_uploads(db, storage):
    for _ in range(100):
        row = (await db.execute(text("""
            SELECT id, user_id, upload_id, video_path FROM media_upload_sessions
            WHERE expires_at < clock_timestamp() AND state IN ('reserved', 'uploading')
            ORDER BY expires_at FOR UPDATE SKIP LOCKED LIMIT 1
        """))).first()
        if row is None:
            await db.rollback()
            break
        try:
            if row.upload_id:
                try:
                    await run_in_thread(storage.abort_upload, row.user_id,
                        {"uploadId": row.upload_id, "video_path": row.video_path})
                except ClientError as exc:
                    if exc.response["Error"]["Code"] != "NoSuchUpload":
                        raise
            await db.execute(text("UPDATE media_upload_sessions SET state = 'aborted' WHERE id = :id"), {"id": row.id})
            await db.commit()
        except STORAGE_ERRORS:
            await db.rollback()
            logger.warning("Expired multipart upload retained for abort retry")
            break


async def sweep_orphaned_media(ctx):
    storage = MediaStorage()
    candidates = await run_in_thread(lambda: list(orphan_candidates(storage)))
    async with AsyncSessionLocal() as db:
        index = await reference_index(db, storage)
        for number, (reference, prefix) in enumerate(candidates, start=1):
            if not await referenced(db, reference, storage, prefix=prefix, index=index):
                await queue_delete(db, reference, prefix=prefix)
            if number % 100 == 0:
                await db.commit()
        await db.commit()
        await drain_deletions(db, storage)