Save and retrieve playlists and epg guides from vercel blob storage

This commit is contained in:
2025-05-06 13:27:14 -05:00
parent a55695865e
commit beb1bdd8c0
8 changed files with 230 additions and 31 deletions

View File

@@ -1,33 +1,24 @@
from fastapi import APIRouter, Depends, Path, HTTPException
from fastapi.responses import FileResponse
from pathlib import Path as FilePath
from src.config import CONTENT_PATH
from utils.database import get_blob_by_path
from fastapi import APIRouter, Depends, Path
from fastapi.responses import RedirectResponse
from src.utils.auth import authenticate_user_query
router = APIRouter(prefix="/content", tags=["content"])
@router.get("/{filename}")
@router.get("/playlist/{filename}")
async def serve_content(
username: str = Depends(authenticate_user_query),
filename: str = Path(..., min_length=1), # Ensure filename is not empty
filename: str = Path(..., min_length=1),
):
"""
Serves a specific file from the configured content directory by filename only.
Prevents directory traversal and ensures files are served from the base content directory.
"""
base_dir = FilePath(CONTENT_PATH).resolve()
requested_file = base_dir / filename
print(f"Requested file: {requested_file}")
# Security checks
try:
# Resolve the full path and check it's within base directory
requested_file = requested_file.resolve()
if not requested_file.is_relative_to(base_dir):
raise ValueError("Invalid path")
except (ValueError, FileNotFoundError):
raise HTTPException(status_code=404, detail="File not found")
"""Redirect to the Vercel Blob URL for the requested playlist file"""
matching_blob = get_blob_by_path('playlists', filename)
return RedirectResponse(url=matching_blob['url'])
if not requested_file.is_file():
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(requested_file)
@router.get("/epg/{filename}")
async def serve_content(
username: str = Depends(authenticate_user_query),
filename: str = Path(..., min_length=1),
):
"""Redirect to the Vercel Blob URL for the requested playlist file"""
matching_blob = get_blob_by_path('epgs', filename)
return RedirectResponse(url=matching_blob['url'])