VeuReu commited on
Commit
8cc7fa8
verified
1 Parent(s): 7ed091e

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +68 -1
api.py CHANGED
@@ -55,7 +55,7 @@ class JobStatus(str, Enum):
55
  jobs: Dict[str, dict] = {}
56
 
57
 
58
- @app.post("/upload_video", tags=["Media Upload"])
59
  async def upload_video(video: UploadFile = File(...)):
60
  """
61
  Saves an uploaded video by hashing it with SHA1 and placing it under:
@@ -111,6 +111,73 @@ async def upload_video(video: UploadFile = File(...)):
111
  }
112
  )
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def describe_image_with_svision(image_path: str, is_face: bool = True) -> tuple[str, str]:
115
  """
116
  Llama al space svision para describir una imagen (usado en generaci贸n de AD).
 
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:
 
111
  }
112
  )
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
+
120
+ This endpoint looks for a video stored under the path:
121
+ /data/media/<sha1>/clip/
122
+ and returns the first MP4 file found in that folder.
123
+
124
+ The method performs the following steps:
125
+ - Checks if the SHA-1 folder exists inside the media root.
126
+ - Validates that the "clip" subfolder exists.
127
+ - Searches for the first .mp4 file inside the clip folder.
128
+ - Uses the FileManager.get_file method to ensure the file is accessible.
129
+ - Returns the video directly as a FileResponse.
130
+
131
+ Parameters
132
+ ----------
133
+ sha1 : str
134
+ The SHA-1 hash corresponding to the directory where the video is stored.
135
+
136
+ Returns
137
+ -------
138
+ FileResponse
139
+ A streaming response containing the MP4 video.
140
+
141
+ Raises
142
+ ------
143
+ HTTPException
144
+ - 404 if the SHA-1 folder does not exist.
145
+ - 404 if the clip folder is missing.
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
+
152
+ if not sha1_folder.exists() or not sha1_folder.is_dir():
153
+ raise HTTPException(status_code=404, detail="SHA1 folder not found")
154
+
155
+ if not clip_folder.exists() or not clip_folder.is_dir():
156
+ raise HTTPException(status_code=404, detail="Clip folder not found")
157
+
158
+ # Find first MP4 file
159
+ mp4_files = list(clip_folder.glob("*.mp4"))
160
+ if not mp4_files:
161
+ raise HTTPException(status_code=404, detail="No MP4 files found")
162
+
163
+ video_path = mp4_files[0]
164
+
165
+ # Convert to relative path for FileManager
166
+ relative_path = video_path.relative_to(MEDIA_ROOT)
167
+
168
+ handler = file_manager.get_file(relative_path)
169
+ if handler is None:
170
+ raise HTTPException(status_code=404, detail="Video not accessible")
171
+
172
+ handler.close()
173
+
174
+ return FileResponse(
175
+ path=video_path,
176
+ media_type="video/mp4",
177
+ filename=video_path.name
178
+ )
179
+
180
+
181
  def describe_image_with_svision(image_path: str, is_face: bool = True) -> tuple[str, str]:
182
  """
183
  Llama al space svision para describir una imagen (usado en generaci贸n de AD).