Add cookies.txt upload UI — drag/drop or click to upload, stored in data volume
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, UploadFile, File, HTTPException
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..auth_utils import get_current_user
|
||||
from ..config import settings
|
||||
from ..database import get_db
|
||||
from ..models import User, UserSettings
|
||||
from ..services import ytdlp
|
||||
@@ -120,3 +122,46 @@ def update_settings(
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
return s
|
||||
|
||||
|
||||
def _cookies_path() -> Path:
|
||||
db_file = settings.database_url.replace("sqlite:///", "")
|
||||
return Path(db_file).parent / "cookies.txt"
|
||||
|
||||
|
||||
@router.post("/cookies-file", response_model=SettingsOut)
|
||||
async def upload_cookies_file(
|
||||
file: UploadFile = File(...),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
if not file.filename or not file.filename.endswith(".txt"):
|
||||
raise HTTPException(status_code=400, detail="Upload a .txt cookies file")
|
||||
content = await file.read()
|
||||
if len(content) > 5 * 1024 * 1024:
|
||||
raise HTTPException(status_code=400, detail="File too large (max 5 MB)")
|
||||
path = _cookies_path()
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(content)
|
||||
s = _get_or_create(db, current_user.id)
|
||||
s.cookies_file = str(path)
|
||||
ytdlp.set_cookies_file(str(path))
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
return s
|
||||
|
||||
|
||||
@router.delete("/cookies-file", response_model=SettingsOut)
|
||||
def delete_cookies_file(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
path = _cookies_path()
|
||||
if path.exists():
|
||||
path.unlink()
|
||||
s = _get_or_create(db, current_user.id)
|
||||
s.cookies_file = ""
|
||||
ytdlp.set_cookies_file("")
|
||||
db.commit()
|
||||
db.refresh(s)
|
||||
return s
|
||||
|
||||
Reference in New Issue
Block a user