VeuReu commited on
Commit
571e31c
·
verified ·
1 Parent(s): 8cc7fa8

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +24 -3
api.py CHANGED
@@ -1,5 +1,5 @@
1
  from __future__ import annotations
2
- from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks, HTTPException
3
  from fastapi import Body
4
  from fastapi.responses import JSONResponse, FileResponse
5
  from fastapi.middleware.cors import CORSMiddleware
@@ -44,6 +44,7 @@ IDENTITIES_ROOT = Path("/tmp/characters")
44
  IDENTITIES_ROOT.mkdir(parents=True, exist_ok=True)
45
  MEDIA_ROOT = Path("/data/media")
46
  file_manager = FileManager(MEDIA_ROOT)
 
47
 
48
  # Sistema de jobs asíncronos
49
  class JobStatus(str, Enum):
@@ -55,8 +56,22 @@ class JobStatus(str, Enum):
55
  jobs: Dict[str, dict] = {}
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
58
  @app.post("/upload_video", tags=["Media Manager"])
59
- async def upload_video(video: UploadFile = File(...)):
 
 
 
60
  """
61
  Saves an uploaded video by hashing it with SHA1 and placing it under:
62
  /data/media/<sha1>/clip/<original_filename>
@@ -68,6 +83,7 @@ async def upload_video(video: UploadFile = File(...)):
68
  - Create folder /data/media/<sha1>/clip if missing.
69
  - Save the video inside /data/media/<sha1>/clip/.
70
  """
 
71
 
72
  # Read content into memory (needed to compute hash twice)
73
  file_bytes = await video.read()
@@ -113,7 +129,10 @@ async def upload_video(video: UploadFile = File(...)):
113
 
114
 
115
  @app.get("/download_video/{sha1}", tags=["Media Manager"])
116
- def download_video(sha1: str):
 
 
 
117
  """
118
  Download a stored video by its SHA-1 directory name.
119
 
@@ -146,6 +165,8 @@ def download_video(sha1: str):
146
  - 404 if no MP4 files are found.
147
  - 404 if the file cannot be retrieved using FileManager.
148
  """
 
 
149
  sha1_folder = MEDIA_ROOT / sha1
150
  clip_folder = sha1_folder / "clip"
151
 
 
1
  from __future__ import annotations
2
+ from fastapi import FastAPI, UploadFile, File,Query, Form, BackgroundTasks, HTTPException
3
  from fastapi import Body
4
  from fastapi.responses import JSONResponse, FileResponse
5
  from fastapi.middleware.cors import CORSMiddleware
 
44
  IDENTITIES_ROOT.mkdir(parents=True, exist_ok=True)
45
  MEDIA_ROOT = Path("/data/media")
46
  file_manager = FileManager(MEDIA_ROOT)
47
+ HF_TOKEN = os.getenv("HF_TOKEN")
48
 
49
  # Sistema de jobs asíncronos
50
  class JobStatus(str, Enum):
 
56
  jobs: Dict[str, dict] = {}
57
 
58
 
59
+ def validate_token(token: str):
60
+ """
61
+ Validate the provided token against the HF_TOKEN environment variable.
62
+ Raises an HTTPException if validation fails.
63
+ """
64
+ if HF_TOKEN is None:
65
+ raise RuntimeError("HF_TOKEN environment variable is not set on the server.")
66
+
67
+ if token != HF_TOKEN:
68
+ raise HTTPException(status_code=401, detail="Invalid token")
69
+
70
  @app.post("/upload_video", tags=["Media Manager"])
71
+ async def upload_video(
72
+ video: UploadFile = File(...),
73
+ token: str = Query(..., description="Token required for authorization")
74
+ ):
75
  """
76
  Saves an uploaded video by hashing it with SHA1 and placing it under:
77
  /data/media/<sha1>/clip/<original_filename>
 
83
  - Create folder /data/media/<sha1>/clip if missing.
84
  - Save the video inside /data/media/<sha1>/clip/.
85
  """
86
+ validate_token(token)
87
 
88
  # Read content into memory (needed to compute hash twice)
89
  file_bytes = await video.read()
 
129
 
130
 
131
  @app.get("/download_video/{sha1}", tags=["Media Manager"])
132
+ def download_video(
133
+ sha1: str,
134
+ token: str = Query(..., description="Token required for authorization")
135
+ ):
136
  """
137
  Download a stored video by its SHA-1 directory name.
138
 
 
165
  - 404 if no MP4 files are found.
166
  - 404 if the file cannot be retrieved using FileManager.
167
  """
168
+ validate_token(token)
169
+
170
  sha1_folder = MEDIA_ROOT / sha1
171
  clip_folder = sha1_folder / "clip"
172