nikhilyadavtemp1 commited on
Commit
41ff2c2
·
1 Parent(s): e065313

feat: implement binary PUT upload (curl -T) and fix 301 redirects

Browse files
Files changed (2) hide show
  1. main.py +26 -12
  2. static/index.html +1 -1
main.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import shutil
3
  import time
4
- from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks, Header
5
  from fastapi.responses import FileResponse, JSONResponse
6
  from fastapi.staticfiles import StaticFiles
7
  from typing import List, Optional
@@ -38,37 +38,51 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
38
  async def read_index():
39
  return FileResponse("static/index.html")
40
 
41
- @app.post("/upload")
 
42
  async def upload_files(
 
43
  files: Optional[List[UploadFile]] = File(None),
44
  file: Optional[UploadFile] = File(None),
45
- x_user_id: str = Header(None)
 
46
  ):
47
  user_id = x_user_id or "cli-anonymous"
48
  uploaded_files = []
49
-
50
- # Handle both "files" (multiple) and "file" (single CLI)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  target_files = []
52
  if files: target_files.extend(files)
53
  if file: target_files.append(file)
54
 
55
- if not target_files:
56
- raise HTTPException(status_code=400, detail="No files provided")
57
-
58
  for f in target_files:
59
  file_id = str(uuid.uuid4())[:8]
60
  filename = f"{user_id}_{file_id}_{f.filename}"
61
  file_path = os.path.join(UPLOAD_DIR, filename)
62
-
63
  with open(file_path, "wb") as buffer:
64
  shutil.copyfileobj(f.file, buffer)
65
-
66
  uploaded_files.append(f"https://ttnikal-filesharing.hf.space/download/{filename}")
67
 
68
- # For CLI (single file), return raw text; for Web, return JSON
69
  if len(target_files) == 1 and not x_user_id:
70
  return f"{uploaded_files[0]}\n"
71
-
72
  return {"message": "Upload successful", "urls": uploaded_files}
73
 
74
  @app.get("/files")
 
1
  import os
2
  import shutil
3
  import time
4
+ from fastapi import FastAPI, UploadFile, File, HTTPException, BackgroundTasks, Header, Request
5
  from fastapi.responses import FileResponse, JSONResponse
6
  from fastapi.staticfiles import StaticFiles
7
  from typing import List, Optional
 
38
  async def read_index():
39
  return FileResponse("static/index.html")
40
 
41
+ @app.api_route("/upload/{filename}", methods=["PUT", "POST"])
42
+ @app.api_route("/upload", methods=["PUT", "POST"])
43
  async def upload_files(
44
+ filename: Optional[str] = None,
45
  files: Optional[List[UploadFile]] = File(None),
46
  file: Optional[UploadFile] = File(None),
47
+ x_user_id: str = Header(None),
48
+ request: Request = None
49
  ):
50
  user_id = x_user_id or "cli-anonymous"
51
  uploaded_files = []
52
+
53
+ # Case 1: Direct Binary Upload (curl -T)
54
+ if request.method == "PUT" or (not files and not file):
55
+ content = await request.body()
56
+ if not content:
57
+ raise HTTPException(status_code=400, detail="Empty file")
58
+
59
+ # Determine filename: from URL path, then header, then default
60
+ final_name = filename or request.headers.get("x-filename") or f"upload_{int(time.time())}"
61
+ file_id = str(uuid.uuid4())[:8]
62
+ safe_name = f"{user_id}_{file_id}_{final_name}"
63
+ file_path = os.path.join(UPLOAD_DIR, safe_name)
64
+
65
+ with open(file_path, "wb") as buffer:
66
+ buffer.write(content)
67
+
68
+ url = f"https://ttnikal-filesharing.hf.space/download/{safe_name}"
69
+ return f"{url}\n"
70
+
71
+ # Case 2: Multipart Upload (Web/Standard Curl)
72
  target_files = []
73
  if files: target_files.extend(files)
74
  if file: target_files.append(file)
75
 
 
 
 
76
  for f in target_files:
77
  file_id = str(uuid.uuid4())[:8]
78
  filename = f"{user_id}_{file_id}_{f.filename}"
79
  file_path = os.path.join(UPLOAD_DIR, filename)
 
80
  with open(file_path, "wb") as buffer:
81
  shutil.copyfileobj(f.file, buffer)
 
82
  uploaded_files.append(f"https://ttnikal-filesharing.hf.space/download/{filename}")
83
 
 
84
  if len(target_files) == 1 and not x_user_id:
85
  return f"{uploaded_files[0]}\n"
 
86
  return {"message": "Upload successful", "urls": uploaded_files}
87
 
88
  @app.get("/files")
static/index.html CHANGED
@@ -35,7 +35,7 @@
35
  </div>
36
  <p>Run this command once to enable <code>share</code> in your terminal:</p>
37
  <div class="link-group">
38
- <code id="cli-setup-cmd">echo 'share() { curl -F "file=@$1" ttnikal-filesharing.hf.space/upload; }' >> ~/.bashrc && source ~/.bashrc</code>
39
  <button class="copy-btn" onclick="copySetupCmd()"><i class="fas fa-copy"></i></button>
40
  </div>
41
  <p class="meta-hint">Usage: <code>share filename.txt</code></p>
 
35
  </div>
36
  <p>Run this command once to enable <code>share</code> in your terminal:</p>
37
  <div class="link-group">
38
+ <code id="cli-setup-cmd">echo 'share() { curl -T "$1" "https://ttnikal-filesharing.hf.space/upload/$(basename "$1")"; }' >> ~/.bashrc && source ~/.bashrc</code>
39
  <button class="copy-btn" onclick="copySetupCmd()"><i class="fas fa-copy"></i></button>
40
  </div>
41
  <p class="meta-hint">Usage: <code>share filename.txt</code></p>