Files
youtube-app/backend/api/video_controller.py
Marek Lenczewski 1cef5fa1e8 update
2026-04-15 21:53:37 +02:00

54 lines
1.7 KiB
Python

from pathlib import Path
from fastapi import APIRouter, HTTPException
from fastapi.responses import FileResponse, StreamingResponse
from database.database import DbSession
from download.download_service import downloadAsync
from model.video import Video
from stream.stream_service import streamAndSave
router = APIRouter(prefix="/videos")
@router.post("/{videoId}/download")
def download(videoId: int, db: DbSession, maxHeight: int = 1080):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
if video.filePath:
return {"status": "already_downloaded"}
downloadAsync(videoId, video.youtubeUrl, maxHeight)
return {"status": "download_started"}
@router.get("/{videoId}/stream")
def stream(videoId: int, db: DbSession, maxHeight: int = 1080):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
if not video.filePath:
return StreamingResponse(streamAndSave(videoId, video.youtubeUrl, maxHeight), media_type="video/mp4")
path = Path(video.filePath)
if not path.exists():
raise HTTPException(404, "Videodatei nicht gefunden")
return FileResponse(path, media_type="video/mp4")
@router.get("/{videoId}/file")
def getFile(videoId: int, db: DbSession):
path, video = Video.getValidPath(db, videoId)
if not path:
raise HTTPException(404, "Video noch nicht heruntergeladen")
return FileResponse(path, media_type="video/mp4", filename=f"{video.title}.mp4")
@router.delete("/{videoId}/file")
def deleteFile(videoId: int, db: DbSession):
if not Video.deleteServerFile(db, videoId):
raise HTTPException(404, "Video nicht gefunden")
return {"status": "deleted"}