Spaces:
Starting
Starting
Bot commited on
Commit ·
dd106bc
1
Parent(s): 000b2bd
Add GoFile Cloud Transfer endpoint
Browse files
app.py
CHANGED
|
@@ -232,6 +232,105 @@ async def upload_file(file: UploadFile = File(...)):
|
|
| 232 |
}
|
| 233 |
return {"task_id": task_id}
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
if __name__ == "__main__":
|
| 236 |
|
| 237 |
import uvicorn
|
|
|
|
| 232 |
}
|
| 233 |
return {"task_id": task_id}
|
| 234 |
|
| 235 |
+
|
| 236 |
+
@app.post("/start_gofile_transfer")
|
| 237 |
+
async def start_gofile_transfer(req: DownloadRequest, background_tasks: BackgroundTasks):
|
| 238 |
+
task_id = str(uuid.uuid4())
|
| 239 |
+
filename = req.url.split("/")[-1]
|
| 240 |
+
if "?" in filename:
|
| 241 |
+
filename = filename.split("?")[0]
|
| 242 |
+
if not filename or "." not in filename:
|
| 243 |
+
filename = "transfer_file.bin"
|
| 244 |
+
|
| 245 |
+
tasks[task_id] = {
|
| 246 |
+
"task_id": task_id,
|
| 247 |
+
"url": req.url,
|
| 248 |
+
"status": "downloading",
|
| 249 |
+
"total_size": 0,
|
| 250 |
+
"downloaded": 0,
|
| 251 |
+
"speed": 0.0,
|
| 252 |
+
"file_path": os.path.join(DATA_DIR, f"{task_id}_{filename}"),
|
| 253 |
+
"original_filename": filename,
|
| 254 |
+
"gofile_url": None,
|
| 255 |
+
"timestamp": time.time()
|
| 256 |
+
}
|
| 257 |
+
background_tasks.add_task(process_gofile_transfer, task_id, req.url)
|
| 258 |
+
return {"task_id": task_id}
|
| 259 |
+
|
| 260 |
+
async def process_gofile_transfer(task_id: str, url: str):
|
| 261 |
+
file_path = tasks[task_id]["file_path"]
|
| 262 |
+
try:
|
| 263 |
+
# 1. Download
|
| 264 |
+
async with aiohttp.ClientSession() as session:
|
| 265 |
+
async with session.get(url) as response:
|
| 266 |
+
response.raise_for_status()
|
| 267 |
+
total_size = int(response.headers.get('Content-Length', 0))
|
| 268 |
+
tasks[task_id]["total_size"] = total_size
|
| 269 |
+
|
| 270 |
+
cd = response.headers.get('Content-Disposition')
|
| 271 |
+
if cd and 'filename=' in cd:
|
| 272 |
+
fname = re.findall('filename="([^"]+)"', cd)
|
| 273 |
+
if not fname:
|
| 274 |
+
fname = re.findall('filename=([^;]+)', cd)
|
| 275 |
+
if fname:
|
| 276 |
+
new_filename = fname[0]
|
| 277 |
+
new_file_path = os.path.join(DATA_DIR, f"{task_id}_{new_filename}")
|
| 278 |
+
tasks[task_id]["file_path"] = new_file_path
|
| 279 |
+
tasks[task_id]["original_filename"] = new_filename
|
| 280 |
+
file_path = new_file_path
|
| 281 |
+
|
| 282 |
+
downloaded = 0
|
| 283 |
+
start_time = time.time()
|
| 284 |
+
last_time = start_time
|
| 285 |
+
last_downloaded = 0
|
| 286 |
+
|
| 287 |
+
with open(file_path, 'wb') as f:
|
| 288 |
+
async for chunk in response.content.iter_chunked(1024 * 1024):
|
| 289 |
+
if not chunk:
|
| 290 |
+
break
|
| 291 |
+
f.write(chunk)
|
| 292 |
+
downloaded += len(chunk)
|
| 293 |
+
tasks[task_id]["downloaded"] = downloaded
|
| 294 |
+
|
| 295 |
+
current_time = time.time()
|
| 296 |
+
if current_time - last_time >= 1.0:
|
| 297 |
+
tasks[task_id]["speed"] = (downloaded - last_downloaded) / (current_time - last_time)
|
| 298 |
+
last_time = current_time
|
| 299 |
+
last_downloaded = downloaded
|
| 300 |
+
|
| 301 |
+
# 2. Upload to GoFile
|
| 302 |
+
tasks[task_id]["status"] = "uploading_to_gofile"
|
| 303 |
+
tasks[task_id]["speed"] = 0.0
|
| 304 |
+
|
| 305 |
+
async with aiohttp.ClientSession() as session:
|
| 306 |
+
# Get server
|
| 307 |
+
async with session.get("https://api.gofile.io/servers") as resp:
|
| 308 |
+
servers_data = await resp.json()
|
| 309 |
+
server = servers_data["data"]["servers"][0]["name"]
|
| 310 |
+
|
| 311 |
+
# Upload
|
| 312 |
+
upload_url = f"https://{server}.gofile.io/uploadFile"
|
| 313 |
+
with open(file_path, 'rb') as f:
|
| 314 |
+
data = aiohttp.FormData()
|
| 315 |
+
data.add_field('file', f, filename=tasks[task_id]["original_filename"])
|
| 316 |
+
async with session.post(upload_url, data=data) as upload_resp:
|
| 317 |
+
upload_result = await upload_resp.json()
|
| 318 |
+
if upload_result["status"] == "ok":
|
| 319 |
+
tasks[task_id]["gofile_url"] = upload_result["data"]["downloadPage"]
|
| 320 |
+
tasks[task_id]["status"] = "completed"
|
| 321 |
+
else:
|
| 322 |
+
raise Exception("GoFile upload failed")
|
| 323 |
+
|
| 324 |
+
# Cleanup local file after successful transfer
|
| 325 |
+
try:
|
| 326 |
+
os.remove(file_path)
|
| 327 |
+
except:
|
| 328 |
+
pass
|
| 329 |
+
|
| 330 |
+
except Exception as e:
|
| 331 |
+
tasks[task_id]["status"] = "error"
|
| 332 |
+
tasks[task_id]["error"] = str(e)
|
| 333 |
+
|
| 334 |
if __name__ == "__main__":
|
| 335 |
|
| 336 |
import uvicorn
|