Spaces:
Runtime error
Runtime error
Bot commited on
Commit ·
000b2bd
1
Parent(s): 0607b48
Fix file extensions by preserving original filenames
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
|
|
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import shutil
|
|
@@ -52,17 +53,7 @@ async def startup_event():
|
|
| 52 |
asyncio.create_task(cleanup_old_files())
|
| 53 |
|
| 54 |
async def download_file(task_id: str, url: str):
|
| 55 |
-
file_path =
|
| 56 |
-
tasks[task_id] = {
|
| 57 |
-
"task_id": task_id,
|
| 58 |
-
"url": url,
|
| 59 |
-
"status": "downloading",
|
| 60 |
-
"total_size": 0,
|
| 61 |
-
"downloaded": 0,
|
| 62 |
-
"speed": 0.0,
|
| 63 |
-
"file_path": file_path,
|
| 64 |
-
"timestamp": time.time()
|
| 65 |
-
}
|
| 66 |
|
| 67 |
try:
|
| 68 |
async with aiohttp.ClientSession() as session:
|
|
@@ -71,6 +62,20 @@ async def download_file(task_id: str, url: str):
|
|
| 71 |
total_size = int(response.headers.get('Content-Length', 0))
|
| 72 |
tasks[task_id]["total_size"] = total_size
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
downloaded = 0
|
| 75 |
start_time = time.time()
|
| 76 |
last_time = start_time
|
|
@@ -100,6 +105,26 @@ async def download_file(task_id: str, url: str):
|
|
| 100 |
@app.post("/start_download")
|
| 101 |
async def start_download(req: DownloadRequest, background_tasks: BackgroundTasks):
|
| 102 |
task_id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
background_tasks.add_task(download_file, task_id, req.url)
|
| 104 |
return {"task_id": task_id}
|
| 105 |
|
|
@@ -113,7 +138,11 @@ async def get_status(task_id: str):
|
|
| 113 |
async def download(task_id: str):
|
| 114 |
if task_id not in tasks or tasks[task_id]["status"] != "completed":
|
| 115 |
return {"error": "File not ready"}
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
@app.get("/stream/{task_id}")
|
| 119 |
async def stream(task_id: str, request: Request):
|
|
@@ -182,7 +211,8 @@ async def get_history():
|
|
| 182 |
@app.post("/upload")
|
| 183 |
async def upload_file(file: UploadFile = File(...)):
|
| 184 |
task_id = str(uuid.uuid4())
|
| 185 |
-
|
|
|
|
| 186 |
|
| 187 |
with open(file_path, "wb") as buffer:
|
| 188 |
shutil.copyfileobj(file.file, buffer)
|
|
@@ -197,6 +227,7 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 197 |
"downloaded": file_size,
|
| 198 |
"speed": 0.0,
|
| 199 |
"file_path": file_path,
|
|
|
|
| 200 |
"timestamp": time.time()
|
| 201 |
}
|
| 202 |
return {"task_id": task_id}
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
+
import re
|
| 4 |
import time
|
| 5 |
import uuid
|
| 6 |
import shutil
|
|
|
|
| 53 |
asyncio.create_task(cleanup_old_files())
|
| 54 |
|
| 55 |
async def download_file(task_id: str, url: str):
|
| 56 |
+
file_path = tasks[task_id]["file_path"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
try:
|
| 59 |
async with aiohttp.ClientSession() as session:
|
|
|
|
| 62 |
total_size = int(response.headers.get('Content-Length', 0))
|
| 63 |
tasks[task_id]["total_size"] = total_size
|
| 64 |
|
| 65 |
+
# Check for Content-Disposition header for filename
|
| 66 |
+
cd = response.headers.get('Content-Disposition')
|
| 67 |
+
if cd and 'filename=' in cd:
|
| 68 |
+
# Extract filename from header
|
| 69 |
+
fname = re.findall('filename="([^"]+)"', cd)
|
| 70 |
+
if not fname:
|
| 71 |
+
fname = re.findall('filename=([^;]+)', cd)
|
| 72 |
+
if fname:
|
| 73 |
+
new_filename = fname[0]
|
| 74 |
+
new_file_path = os.path.join(DATA_DIR, f"{task_id}_{new_filename}")
|
| 75 |
+
tasks[task_id]["file_path"] = new_file_path
|
| 76 |
+
tasks[task_id]["original_filename"] = new_filename
|
| 77 |
+
file_path = new_file_path
|
| 78 |
+
|
| 79 |
downloaded = 0
|
| 80 |
start_time = time.time()
|
| 81 |
last_time = start_time
|
|
|
|
| 105 |
@app.post("/start_download")
|
| 106 |
async def start_download(req: DownloadRequest, background_tasks: BackgroundTasks):
|
| 107 |
task_id = str(uuid.uuid4())
|
| 108 |
+
|
| 109 |
+
# Try to extract filename from URL
|
| 110 |
+
filename = req.url.split("/")[-1]
|
| 111 |
+
if "?" in filename:
|
| 112 |
+
filename = filename.split("?")[0]
|
| 113 |
+
if not filename or "." not in filename:
|
| 114 |
+
filename = "downloaded_file.bin"
|
| 115 |
+
|
| 116 |
+
tasks[task_id] = {
|
| 117 |
+
"task_id": task_id,
|
| 118 |
+
"url": req.url,
|
| 119 |
+
"status": "downloading",
|
| 120 |
+
"total_size": 0,
|
| 121 |
+
"downloaded": 0,
|
| 122 |
+
"speed": 0.0,
|
| 123 |
+
"file_path": os.path.join(DATA_DIR, f"{task_id}_{filename}"),
|
| 124 |
+
"original_filename": filename,
|
| 125 |
+
"timestamp": time.time()
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
background_tasks.add_task(download_file, task_id, req.url)
|
| 129 |
return {"task_id": task_id}
|
| 130 |
|
|
|
|
| 138 |
async def download(task_id: str):
|
| 139 |
if task_id not in tasks or tasks[task_id]["status"] != "completed":
|
| 140 |
return {"error": "File not ready"}
|
| 141 |
+
|
| 142 |
+
file_path = tasks[task_id]["file_path"]
|
| 143 |
+
filename = tasks[task_id].get("original_filename", f"downloaded_{task_id}.bin")
|
| 144 |
+
|
| 145 |
+
return FileResponse(file_path, filename=filename)
|
| 146 |
|
| 147 |
@app.get("/stream/{task_id}")
|
| 148 |
async def stream(task_id: str, request: Request):
|
|
|
|
| 211 |
@app.post("/upload")
|
| 212 |
async def upload_file(file: UploadFile = File(...)):
|
| 213 |
task_id = str(uuid.uuid4())
|
| 214 |
+
original_filename = file.filename if file.filename else "uploaded_file.bin"
|
| 215 |
+
file_path = os.path.join(DATA_DIR, f"{task_id}_{original_filename}")
|
| 216 |
|
| 217 |
with open(file_path, "wb") as buffer:
|
| 218 |
shutil.copyfileobj(file.file, buffer)
|
|
|
|
| 227 |
"downloaded": file_size,
|
| 228 |
"speed": 0.0,
|
| 229 |
"file_path": file_path,
|
| 230 |
+
"original_filename": original_filename,
|
| 231 |
"timestamp": time.time()
|
| 232 |
}
|
| 233 |
return {"task_id": task_id}
|