Spaces:
Paused
Paused
Commit ·
0851add
1
Parent(s): 78e2e6b
fix: restore /upload routes for binary and multipart uploads and cleanup main blocks
Browse files
app.py
CHANGED
|
@@ -226,6 +226,47 @@ async def download_file(filepath: str, request: Request, password: Optional[str]
|
|
| 226 |
|
| 227 |
return resp
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
@app.delete("/delete/{filename}")
|
| 230 |
async def delete_file(filename: str, x_user_id: str = Header(None)):
|
| 231 |
if not x_user_id: raise HTTPException(401)
|
|
@@ -238,7 +279,3 @@ async def delete_file(filename: str, x_user_id: str = Header(None)):
|
|
| 238 |
if __name__ == "__main__":
|
| 239 |
import uvicorn
|
| 240 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 241 |
-
|
| 242 |
-
if __name__ == "__main__":
|
| 243 |
-
import uvicorn
|
| 244 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 226 |
|
| 227 |
return resp
|
| 228 |
|
| 229 |
+
@app.api_route("/upload/{filename}", methods=["PUT", "POST"])
|
| 230 |
+
@app.api_route("/upload", methods=["PUT", "POST"])
|
| 231 |
+
async def upload_files(
|
| 232 |
+
filename: Optional[str] = None,
|
| 233 |
+
files: Optional[List[UploadFile]] = File(None),
|
| 234 |
+
file: Optional[UploadFile] = File(None),
|
| 235 |
+
x_user_id: str = Header(None),
|
| 236 |
+
request: Request = None
|
| 237 |
+
):
|
| 238 |
+
user_id = x_user_id or "cli-anonymous"
|
| 239 |
+
user_dir = os.path.join(UPLOAD_DIR, f"user_{user_id}")
|
| 240 |
+
os.makedirs(user_dir, exist_ok=True)
|
| 241 |
+
|
| 242 |
+
# Binary Upload (curl -T style)
|
| 243 |
+
if request.method == "PUT" or (not files and not file):
|
| 244 |
+
content = await request.body()
|
| 245 |
+
if not content:
|
| 246 |
+
raise HTTPException(status_code=400, detail="Empty file body.")
|
| 247 |
+
fname = filename or request.headers.get("x-filename") or f"upload_{int(time.time())}"
|
| 248 |
+
fname = os.path.basename(fname) # sanitize
|
| 249 |
+
safe_name = f"{str(uuid.uuid4())[:8]}_{fname}"
|
| 250 |
+
with open(os.path.join(user_dir, safe_name), "wb") as b:
|
| 251 |
+
b.write(content)
|
| 252 |
+
return f"{BASE_URL}/download/user_{user_id}/{safe_name}\n"
|
| 253 |
+
|
| 254 |
+
# Multipart Upload
|
| 255 |
+
target = (files or []) + ([file] if file else [])
|
| 256 |
+
if not target:
|
| 257 |
+
raise HTTPException(status_code=400, detail="No files provided.")
|
| 258 |
+
|
| 259 |
+
urls = []
|
| 260 |
+
for f in target:
|
| 261 |
+
fname = os.path.basename(f.filename or f"file_{int(time.time())}")
|
| 262 |
+
name = f"{str(uuid.uuid4())[:8]}_{fname}"
|
| 263 |
+
dest = os.path.join(user_dir, name)
|
| 264 |
+
with open(dest, "wb") as b:
|
| 265 |
+
shutil.copyfileobj(f.file, b)
|
| 266 |
+
urls.append(f"{BASE_URL}/download/user_{user_id}/{name}")
|
| 267 |
+
|
| 268 |
+
return urls[0] if len(target) == 1 and not x_user_id else {"urls": urls}
|
| 269 |
+
|
| 270 |
@app.delete("/delete/{filename}")
|
| 271 |
async def delete_file(filename: str, x_user_id: str = Header(None)):
|
| 272 |
if not x_user_id: raise HTTPException(401)
|
|
|
|
| 279 |
if __name__ == "__main__":
|
| 280 |
import uvicorn
|
| 281 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|