Shoraky commited on
Commit
9d7b599
·
1 Parent(s): 31b5eb5

Return 404 for missing video posters

Browse files
Files changed (1) hide show
  1. app.py +6 -2
app.py CHANGED
@@ -27,6 +27,7 @@ from dotenv import load_dotenv
27
  from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
28
  from fastapi.responses import FileResponse, JSONResponse
29
  from huggingface_hub import HfApi, hf_hub_download, snapshot_download
 
30
 
31
  load_dotenv(Path(__file__).with_name(".env"))
32
  jobs = {}
@@ -544,6 +545,9 @@ def video_poster(session_id: UUID, filename: str, request: Request):
544
  row = session_row(session_id, request.state.user_id)
545
  if Path(filename).name != filename or "\\" in filename or not filename.endswith(".webp"):
546
  raise HTTPException(404, "Preview not found")
547
- path = hf_hub_download(repo_id=os.environ["STORAGE_REPO_ID"], repo_type="dataset",
548
- filename=f'{row["path"]}/posters/{filename}', token=os.environ["HF_TOKEN"])
 
 
 
549
  return FileResponse(path, media_type="image/webp", headers={"Cache-Control": "private, max-age=86400"})
 
27
  from fastapi import FastAPI, File, Form, HTTPException, Request, UploadFile
28
  from fastapi.responses import FileResponse, JSONResponse
29
  from huggingface_hub import HfApi, hf_hub_download, snapshot_download
30
+ from huggingface_hub.errors import RemoteEntryNotFoundError
31
 
32
  load_dotenv(Path(__file__).with_name(".env"))
33
  jobs = {}
 
545
  row = session_row(session_id, request.state.user_id)
546
  if Path(filename).name != filename or "\\" in filename or not filename.endswith(".webp"):
547
  raise HTTPException(404, "Preview not found")
548
+ try:
549
+ path = hf_hub_download(repo_id=os.environ["STORAGE_REPO_ID"], repo_type="dataset",
550
+ filename=f'{row["path"]}/posters/{filename}', token=os.environ["HF_TOKEN"])
551
+ except RemoteEntryNotFoundError as error:
552
+ raise HTTPException(404, "Preview not found") from error
553
  return FileResponse(path, media_type="image/webp", headers={"Cache-Control": "private, max-age=86400"})