Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -12,13 +12,13 @@ UPLOAD_DIR = "uploads"
|
|
| 12 |
if not os.path.exists(UPLOAD_DIR):
|
| 13 |
os.makedirs(UPLOAD_DIR)
|
| 14 |
|
| 15 |
-
# Store file mappings: {unique_code: filename}
|
| 16 |
-
file_codes: Dict[str,
|
| 17 |
-
# Reverse mapping
|
| 18 |
-
filename_codes: Dict[str,
|
| 19 |
|
| 20 |
@app.post("/upload/")
|
| 21 |
-
async def upload_file(file: UploadFile = File(...)):
|
| 22 |
try:
|
| 23 |
# Generate unique code
|
| 24 |
unique_code = str(uuid.uuid4())[:8]
|
|
@@ -28,9 +28,9 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 28 |
with open(file_path, "wb") as buffer:
|
| 29 |
shutil.copyfileobj(file.file, buffer)
|
| 30 |
|
| 31 |
-
# Store the mapping
|
| 32 |
-
file_codes[unique_code] = file.filename
|
| 33 |
-
filename_codes[file.filename] = unique_code
|
| 34 |
|
| 35 |
return {
|
| 36 |
"filename": file.filename,
|
|
@@ -53,8 +53,8 @@ async def upload_multiple_files(files: List[UploadFile] = File(...)):
|
|
| 53 |
shutil.copyfileobj(file.file, buffer)
|
| 54 |
|
| 55 |
# Store the mapping
|
| 56 |
-
file_codes[unique_code] = file.filename
|
| 57 |
-
filename_codes[file.filename] = unique_code
|
| 58 |
|
| 59 |
uploaded_files.append({
|
| 60 |
"filename": file.filename,
|
|
@@ -64,16 +64,18 @@ async def upload_multiple_files(files: List[UploadFile] = File(...)):
|
|
| 64 |
except Exception as e:
|
| 65 |
raise HTTPException(status_code=500, detail=str(e))
|
| 66 |
|
| 67 |
-
@app.get("/files/")
|
| 68 |
-
async def list_files():
|
|
|
|
| 69 |
try:
|
| 70 |
files = []
|
| 71 |
for filename in os.listdir(UPLOAD_DIR):
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
return {"files": files}
|
| 78 |
except Exception as e:
|
| 79 |
raise HTTPException(status_code=500, detail=str(e))
|
|
@@ -84,7 +86,7 @@ async def download_file(access_code: str):
|
|
| 84 |
if access_code not in file_codes:
|
| 85 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 86 |
|
| 87 |
-
filename = file_codes[access_code]
|
| 88 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 89 |
|
| 90 |
if os.path.exists(file_path):
|
|
@@ -100,7 +102,7 @@ async def delete_file(access_code: str):
|
|
| 100 |
if access_code not in file_codes:
|
| 101 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 102 |
|
| 103 |
-
filename = file_codes[access_code]
|
| 104 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 105 |
|
| 106 |
if os.path.exists(file_path):
|
|
@@ -122,7 +124,7 @@ async def direct_download(access_code: str):
|
|
| 122 |
# If not a valid access code, return 404 or redirect to home
|
| 123 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 124 |
|
| 125 |
-
filename = file_codes[access_code]
|
| 126 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 127 |
|
| 128 |
if os.path.exists(file_path):
|
|
|
|
| 12 |
if not os.path.exists(UPLOAD_DIR):
|
| 13 |
os.makedirs(UPLOAD_DIR)
|
| 14 |
|
| 15 |
+
# Store file mappings with user info: {unique_code: {"filename": filename, "user_id": user_id}}
|
| 16 |
+
file_codes: Dict[str, dict] = {}
|
| 17 |
+
# Reverse mapping: {filename: {"code": code, "user_id": user_id}}
|
| 18 |
+
filename_codes: Dict[str, dict] = {}
|
| 19 |
|
| 20 |
@app.post("/upload/")
|
| 21 |
+
async def upload_file(file: UploadFile = File(...), user_id: str = None):
|
| 22 |
try:
|
| 23 |
# Generate unique code
|
| 24 |
unique_code = str(uuid.uuid4())[:8]
|
|
|
|
| 28 |
with open(file_path, "wb") as buffer:
|
| 29 |
shutil.copyfileobj(file.file, buffer)
|
| 30 |
|
| 31 |
+
# Store the mapping with user info
|
| 32 |
+
file_codes[unique_code] = {"filename": file.filename, "user_id": user_id}
|
| 33 |
+
filename_codes[file.filename] = {"code": unique_code, "user_id": user_id}
|
| 34 |
|
| 35 |
return {
|
| 36 |
"filename": file.filename,
|
|
|
|
| 53 |
shutil.copyfileobj(file.file, buffer)
|
| 54 |
|
| 55 |
# Store the mapping
|
| 56 |
+
file_codes[unique_code] = {"filename": file.filename, "user_id": None}
|
| 57 |
+
filename_codes[file.filename] = {"code": unique_code, "user_id": None}
|
| 58 |
|
| 59 |
uploaded_files.append({
|
| 60 |
"filename": file.filename,
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
raise HTTPException(status_code=500, detail=str(e))
|
| 66 |
|
| 67 |
+
@app.get("/files/{user_id}")
|
| 68 |
+
async def list_files(user_id: str):
|
| 69 |
+
"""List files for a specific user"""
|
| 70 |
try:
|
| 71 |
files = []
|
| 72 |
for filename in os.listdir(UPLOAD_DIR):
|
| 73 |
+
file_info = filename_codes.get(filename, {})
|
| 74 |
+
if file_info.get("user_id") == user_id:
|
| 75 |
+
files.append({
|
| 76 |
+
"filename": filename,
|
| 77 |
+
"access_code": file_info["code"]
|
| 78 |
+
})
|
| 79 |
return {"files": files}
|
| 80 |
except Exception as e:
|
| 81 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 86 |
if access_code not in file_codes:
|
| 87 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 88 |
|
| 89 |
+
filename = file_codes[access_code]["filename"]
|
| 90 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 91 |
|
| 92 |
if os.path.exists(file_path):
|
|
|
|
| 102 |
if access_code not in file_codes:
|
| 103 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 104 |
|
| 105 |
+
filename = file_codes[access_code]["filename"]
|
| 106 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 107 |
|
| 108 |
if os.path.exists(file_path):
|
|
|
|
| 124 |
# If not a valid access code, return 404 or redirect to home
|
| 125 |
raise HTTPException(status_code=404, detail="Invalid access code")
|
| 126 |
|
| 127 |
+
filename = file_codes[access_code]["filename"]
|
| 128 |
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 129 |
|
| 130 |
if os.path.exists(file_path):
|