feat: remove uploaded video artifacts
This commit is contained in:
parent
23f75adb40
commit
d00ff63897
2 changed files with 86 additions and 0 deletions
|
|
@ -2,6 +2,7 @@ import hashlib
|
|||
import json
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
|
@ -380,6 +381,25 @@ def should_publish_to_group_video(video: Video) -> bool:
|
|||
return (video.title or "").strip() != VK_CLIP_TITLE
|
||||
|
||||
|
||||
def cleanup_uploaded_video_artifacts(video: Video) -> None:
|
||||
video_root = (settings.storage_path / "vk").resolve()
|
||||
directories = {(video_root / str(video.id)).resolve()}
|
||||
for path_value in (video.local_video_path, video.local_cover_path):
|
||||
if path_value:
|
||||
directories.add(Path(path_value).resolve().parent)
|
||||
|
||||
for directory in directories:
|
||||
if directory == video_root or video_root not in directory.parents:
|
||||
raise ValueError(f"Refusing to clean path outside video storage: {directory}")
|
||||
|
||||
for directory in directories:
|
||||
if directory.exists():
|
||||
shutil.rmtree(directory)
|
||||
|
||||
video.local_video_path = None
|
||||
video.local_cover_path = None
|
||||
|
||||
|
||||
def all_post_videos_ready(session, media_items: list[PostMedia]) -> bool:
|
||||
for item in media_items:
|
||||
if item.media_type != "video":
|
||||
|
|
@ -1203,6 +1223,15 @@ def upload_video_to_ok(self, video_id: int) -> str:
|
|||
|
||||
session.commit()
|
||||
|
||||
uploaded_rows = duplicate_video_query(session, video).all()
|
||||
for uploaded_video in uploaded_rows:
|
||||
try:
|
||||
cleanup_uploaded_video_artifacts(uploaded_video)
|
||||
except (OSError, ValueError) as exc:
|
||||
uploaded_video.error_message = f"OK upload succeeded; local cleanup failed: {exc}"[-4000:]
|
||||
logger.exception("Local video cleanup failed for video_id=%s", uploaded_video.id)
|
||||
session.commit()
|
||||
|
||||
for post_id in waiting_posts:
|
||||
publish_post_to_ok.delay(post_id)
|
||||
return "uploaded"
|
||||
|
|
|
|||
57
tests/test_video_cleanup.py
Normal file
57
tests/test_video_cleanup.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from app.models import Video
|
||||
from app.worker import cleanup_uploaded_video_artifacts, settings
|
||||
|
||||
|
||||
class CleanupUploadedVideoArtifactsTests(unittest.TestCase):
|
||||
def test_removes_video_directory_and_clears_paths(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary_dir:
|
||||
storage = Path(temporary_dir)
|
||||
video_dir = storage / "vk" / "7"
|
||||
video_dir.mkdir(parents=True)
|
||||
video_path = video_dir / "video.mp4"
|
||||
cover_path = video_dir / "video.jpg"
|
||||
info_path = video_dir / "video.info.json"
|
||||
for path in (video_path, cover_path, info_path):
|
||||
path.write_text("test", encoding="utf-8")
|
||||
|
||||
video = Video(
|
||||
id=7,
|
||||
vk_video_url="https://vk.com/video-1_7",
|
||||
local_video_path=str(video_path),
|
||||
local_cover_path=str(cover_path),
|
||||
)
|
||||
|
||||
with patch.object(settings, "storage_dir", str(storage)):
|
||||
cleanup_uploaded_video_artifacts(video)
|
||||
|
||||
self.assertFalse(video_dir.exists())
|
||||
self.assertIsNone(video.local_video_path)
|
||||
self.assertIsNone(video.local_cover_path)
|
||||
|
||||
def test_refuses_path_outside_video_storage(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as temporary_dir:
|
||||
storage = Path(temporary_dir) / "storage"
|
||||
outside = Path(temporary_dir) / "outside"
|
||||
outside.mkdir()
|
||||
video_path = outside / "video.mp4"
|
||||
video_path.write_text("test", encoding="utf-8")
|
||||
video = Video(
|
||||
id=8,
|
||||
vk_video_url="https://vk.com/video-1_8",
|
||||
local_video_path=str(video_path),
|
||||
)
|
||||
|
||||
with patch.object(settings, "storage_dir", str(storage)):
|
||||
with self.assertRaises(ValueError):
|
||||
cleanup_uploaded_video_artifacts(video)
|
||||
|
||||
self.assertTrue(video_path.exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Reference in a new issue