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 <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
root
2026-01-23 11:25:43 +00:00
parent b27c331d1c
commit 944d13a4c9
+47
View File
@@ -32,6 +32,53 @@ app.add_middleware(
# Initialize download manager # Initialize download manager
download_manager = DownloadManager(download_dir="downloads", max_parallel=3) 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 # Mount static files and templates
app.mount("/static", StaticFiles(directory="static"), name="static") app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads") app.mount("/downloads", StaticFiles(directory="downloads"), name="downloads")