Chandima Prabhath
Refactor Redis connection handling and update Dockerfile for environment variable support
f7bd5c7
raw
history blame
2.31 kB
from fastapi import FastAPI, UploadFile, HTTPException, status
from fastapi.responses import JSONResponse, FileResponse
from pathlib import Path
import uuid
import os
import hmac
import hashlib
from datetime import datetime
from rq import Queue
from redis import from_url
from ..config import EncodingConfig, RedisConfig
from ..worker.tasks import encode_video_task
q = Queue(connection=RedisConfig.get_connection(), default_timeout=3600)
app = FastAPI(title="Video Encoding Service")
@app.post("/upload")
async def upload_video(file: UploadFile):
job_id = str(uuid.uuid4())
temp_dir = os.path.join(EncodingConfig.TEMP_DIR, job_id)
os.makedirs(temp_dir, exist_ok=True)
input_path = os.path.join(temp_dir, file.filename)
with open(input_path, "wb") as f:
f.write(await file.read())
q.enqueue(encode_video_task, job_id, input_path)
return JSONResponse(
content={"job_id": job_id},
status_code=status.HTTP_202_ACCEPTED
)
@app.get("/status/{job_id}")
async def get_status(job_id: str):
redis = RedisConfig.get_connection()
progress = redis.hget(f"job:{job_id}", "progress")
if not progress:
raise HTTPException(status_code=404, detail="Job not found")
return {"job_id": job_id, "progress": float(progress)}
@app.get("/play/{job_id}")
async def play_video(job_id: str, token: str):
expected_token = hmac.new(
EncodingConfig.HMAC_SECRET.encode(),
job_id.encode(),
hashlib.sha256
).hexdigest()
if not hmac.compare_digest(token, expected_token):
raise HTTPException(status_code=403, detail="Invalid token")
master_playlist = os.path.join(EncodingConfig.TEMP_DIR, job_id, f"{job_id}_master.m3u8")
if not os.path.exists(master_playlist):
raise HTTPException(status_code=404, detail="Playlist not found")
return FileResponse(
master_playlist,
media_type="application/vnd.apple.mpegurl"
)
@app.get("/token/{job_id}")
async def generate_token(job_id: str):
timestamp = datetime.now().isoformat()
signature = hmac.new(
EncodingConfig.HMAC_SECRET.encode(),
f"{job_id}{timestamp}".encode(),
hashlib.sha256
).hexdigest()
return {"token": f"{timestamp}:{signature}"}