54 lines
1.7 KiB
Python
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):
|
|
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)
|
|
return {"status": "download_started"}
|
|
|
|
|
|
@router.get("/{videoId}/stream")
|
|
def stream(videoId: int, db: DbSession):
|
|
video = Video.getById(db, videoId)
|
|
if not video:
|
|
raise HTTPException(404, "Video nicht gefunden")
|
|
|
|
if not video.filePath:
|
|
return StreamingResponse(streamAndSave(videoId, video.youtubeUrl), 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"}
|