VeuReu commited on
Commit
0d097ac
·
verified ·
1 Parent(s): 571e31c

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +87 -3
api.py CHANGED
@@ -67,7 +67,91 @@ def validate_token(token: str):
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")
@@ -128,7 +212,7 @@ async def upload_video(
128
  )
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")
@@ -166,7 +250,7 @@ def download_video(
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
 
 
67
  if token != HF_TOKEN:
68
  raise HTTPException(status_code=401, detail="Invalid token")
69
 
70
+
71
+ @app.post("/upload_cast_csv/{sha1}", tags=["Media Manager"])
72
+ async def upload_cast_csv(
73
+ sha1: str,
74
+ cast_file: UploadFile = File(...),
75
+ token: str = Query(..., description="Token required for authorization")
76
+ ):
77
+ """
78
+ Upload a cast CSV file for a specific video identified by its SHA-1.
79
+
80
+ The CSV will be stored under:
81
+ /data/media/<sha1>/cast/cast.csv
82
+
83
+ Steps:
84
+ - Validate the token.
85
+ - Ensure /data/media/<sha1> exists.
86
+ - Create /cast folder if missing.
87
+ - Save the CSV file inside /cast.
88
+ """
89
+ validate_token(token)
90
+
91
+ base_folder = MEDIA_ROOT / sha1
92
+ if not base_folder.exists() or not base_folder.is_dir():
93
+ raise HTTPException(status_code=404, detail="SHA1 folder not found")
94
+
95
+ cast_folder = base_folder / "cast"
96
+ cast_folder.mkdir(parents=True, exist_ok=True)
97
+
98
+ final_path = cast_folder / "cast.csv"
99
+
100
+ file_bytes = await cast_file.read()
101
+ save_result = file_manager.upload_file(io.BytesIO(file_bytes), final_path)
102
+ if not save_result["operation_success"]:
103
+ raise HTTPException(status_code=500, detail=save_result["error"])
104
+
105
+ return JSONResponse(
106
+ status_code=200,
107
+ content={"status": "ok", "saved_to": str(final_path)}
108
+ )
109
+
110
+
111
+ @app.get("/download_cast_csv/{sha1}", tags=["Media Manager"])
112
+ def download_cast_csv(
113
+ sha1: str,
114
+ token: str = Query(..., description="Token required for authorization")
115
+ ):
116
+ """
117
+ Download the cast CSV for a specific video identified by its SHA-1.
118
+
119
+ The CSV is expected under:
120
+ /data/media/<sha1>/cast/cast.csv
121
+
122
+ Steps:
123
+ - Validate the token.
124
+ - Ensure /data/media/<sha1> and /cast exist.
125
+ - Return the CSV as a FileResponse.
126
+ - Raise 404 if any folder or file is missing.
127
+ """
128
+ validate_token(token)
129
+
130
+ base_folder = MEDIA_ROOT / sha1
131
+ cast_folder = base_folder / "cast"
132
+ csv_path = cast_folder / "cast.csv"
133
+
134
+ if not base_folder.exists() or not base_folder.is_dir():
135
+ raise HTTPException(status_code=404, detail="SHA1 folder not found")
136
+ if not cast_folder.exists() or not cast_folder.is_dir():
137
+ raise HTTPException(status_code=404, detail="Cast folder not found")
138
+ if not csv_path.exists() or not csv_path.is_file():
139
+ raise HTTPException(status_code=404, detail="Cast CSV not found")
140
+
141
+ # Convert to relative path for FileManager
142
+ relative_path = csv_path.relative_to(MEDIA_ROOT)
143
+ handler = file_manager.get_file(relative_path)
144
+ if handler is None:
145
+ raise HTTPException(status_code=404, detail="Cast CSV not accessible")
146
+ handler.close()
147
+
148
+ return FileResponse(
149
+ path=csv_path,
150
+ media_type="text/csv",
151
+ filename="cast.csv"
152
+ )
153
+
154
+ @app.post("/upload_original_video", tags=["Media Manager"])
155
  async def upload_video(
156
  video: UploadFile = File(...),
157
  token: str = Query(..., description="Token required for authorization")
 
212
  )
213
 
214
 
215
+ @app.get("/download_original_video/{sha1}", tags=["Media Manager"])
216
  def download_video(
217
  sha1: str,
218
  token: str = Query(..., description="Token required for authorization")
 
250
  - 404 if the file cannot be retrieved using FileManager.
251
  """
252
  validate_token(token)
253
+
254
  sha1_folder = MEDIA_ROOT / sha1
255
  clip_folder = sha1_folder / "clip"
256