feat: auto cleanup with retention period, storage limit, settings UI
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
"""Admin management routes."""
|
||||
import json
|
||||
import os
|
||||
from fastapi import APIRouter, HTTPException, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func, or_
|
||||
from app.database import get_db
|
||||
from app.models import Video, DownloadLog
|
||||
from app.schemas import VideoInfo, VideoListResponse, StorageStats, DownloadLogInfo, DownloadLogListResponse
|
||||
from app.models import Video, DownloadLog, AppSetting
|
||||
from app.schemas import (
|
||||
VideoInfo, VideoListResponse, StorageStats,
|
||||
DownloadLogInfo, DownloadLogListResponse,
|
||||
CleanupConfig, CleanupStatus, DiskStats,
|
||||
)
|
||||
from app.auth import get_current_user
|
||||
from app.services.cleanup import get_setting, set_setting, disk_stats, run_cleanup
|
||||
|
||||
router = APIRouter(prefix="/api/admin", tags=["admin"])
|
||||
|
||||
@@ -100,3 +106,37 @@ async def download_logs(
|
||||
items.append(d)
|
||||
|
||||
return DownloadLogListResponse(logs=items, total=total, page=page, page_size=page_size)
|
||||
|
||||
|
||||
@router.get("/settings/cleanup", response_model=CleanupStatus)
|
||||
async def get_cleanup_settings(user: dict = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
video_base = os.getenv("VIDEO_BASE_PATH", "/home/xdl/xdl_videos")
|
||||
last_result_raw = await get_setting(db, "cleanup_last_result", "{}")
|
||||
try:
|
||||
last_result = json.loads(last_result_raw) if last_result_raw else {}
|
||||
except Exception:
|
||||
last_result = {}
|
||||
return CleanupStatus(
|
||||
config=CleanupConfig(
|
||||
enabled=(await get_setting(db, "cleanup_enabled", "true")) == "true",
|
||||
retention_minutes=int(await get_setting(db, "cleanup_retention_minutes", "10080")),
|
||||
storage_limit_pct=int(await get_setting(db, "cleanup_storage_limit_pct", "80")),
|
||||
),
|
||||
disk=DiskStats(**disk_stats(video_base)),
|
||||
last_run=await get_setting(db, "cleanup_last_run", ""),
|
||||
last_result=last_result,
|
||||
)
|
||||
|
||||
|
||||
@router.put("/settings/cleanup", response_model=CleanupStatus)
|
||||
async def update_cleanup_settings(cfg: CleanupConfig, user: dict = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
await set_setting(db, "cleanup_enabled", "true" if cfg.enabled else "false")
|
||||
await set_setting(db, "cleanup_retention_minutes", str(cfg.retention_minutes))
|
||||
await set_setting(db, "cleanup_storage_limit_pct", str(cfg.storage_limit_pct))
|
||||
return await get_cleanup_settings(user=user, db=db)
|
||||
|
||||
|
||||
@router.post("/cleanup/run")
|
||||
async def trigger_cleanup(user: dict = Depends(get_current_user)):
|
||||
result = await run_cleanup()
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user