From 944d13a4c9b2101a5de971d5f448de5a02c8bdbb Mon Sep 17 00:00:00 2001 From: root Date: Fri, 23 Jan 2026 11:25:43 +0000 Subject: [PATCH] feat: Auto-restore completed downloads on server restart Scan the downloads folder on startup and recreate tasks for all video files. This prevents losing download history when the server restarts. - Only restores video files larger than 1MB (avoids partial files) - Preserves original file timestamps as created/completed dates - Generates new task IDs for restored downloads Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/main.py b/main.py index e827c8e..71520ef 100644 --- a/main.py +++ b/main.py @@ -32,6 +32,53 @@ app.add_middleware( # Initialize download manager download_manager = DownloadManager(download_dir="downloads", max_parallel=3) + +def restore_completed_downloads(): + """Scan downloads directory and restore completed download tasks""" + from datetime import datetime + from pathlib import Path + import uuid + + download_dir = Path("downloads") + if not download_dir.exists(): + return + + # Get all video files (exclude partial files and logs) + video_extensions = {'.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.webm'} + + for file_path in download_dir.iterdir(): + if file_path.is_file() and file_path.suffix.lower() in video_extensions: + # Skip small files (likely partial or errors) + if file_path.stat().st_size < 1024 * 1024: # Less than 1MB + continue + + filename = file_path.name + file_size = file_path.stat().st_size + + # Create a task for this file + task_id = str(uuid.uuid4()) + task = DownloadTask( + id=task_id, + url="", # No original URL + filename=filename, + host="other", + status=DownloadStatus.COMPLETED, + progress=100.0, + downloaded_bytes=file_size, + total_bytes=file_size, + speed=0.0, + file_path=str(file_path), + created_at=datetime.fromtimestamp(file_path.stat().st_ctime), + completed_at=datetime.fromtimestamp(file_path.stat().st_mtime) + ) + + download_manager.tasks[task_id] = task + print(f"[RESTORE] Restored completed download: {filename}") + + +# Restore completed downloads on startup +restore_completed_downloads() + # Mount static files and templates app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")