Skip to content

Commit b5a3e55

Browse files
authored
Merge pull request #76 from abolfazl8131/master
fix(download): optimize downloading process
2 parents 7aec562 + b7f406d commit b5a3e55

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

app/media/MyHelm_zip.zip

1.81 KB
Binary file not shown.

app/routes/utils.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,33 @@
22
from fastapi import FastAPI, HTTPException,Response
33
from fastapi.responses import FileResponse
44
import os
5+
import zipfile
56

67

7-
@app.get("/download-helm/{filename}")
8-
def download_file_helm(filename: str):
8+
def zip_folder(folder_path: str, output_zip_path: str):
9+
"""Zip the entire folder."""
10+
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
11+
for root, dirs, files in os.walk(folder_path):
12+
for file in files:
13+
file_path = os.path.join(root, file)
14+
# Add file to the zip file
15+
zip_file.write(file_path, os.path.relpath(file_path, folder_path))
916

10-
folder = 'app/media/MyHelm'
11-
file_path = os.path.join(folder, filename)
12-
13-
if not os.path.isfile(file_path):
14-
raise HTTPException(status_code=404, detail="File not found.")
1517

16-
17-
return FileResponse(file_path, media_type='application/octet-stream', filename=filename)
1818

1919

20-
@app.get("/download-terraform/{filename}")
21-
def download_file_terraform(filename: str):
20+
@app.get("/download-folder{folder_name}")
21+
async def download_folder_MyHelm(folder_name: str):
22+
folder_path = f"app/media/{folder_name}" # Adjust the path as needed
23+
if not os.path.exists(folder_path):
24+
raise HTTPException(status_code=404, detail="Folder not found")
2225

23-
folder = 'app/media/MyTerraform'
24-
file_path = os.path.join(folder, filename)
25-
26-
if not os.path.isfile(file_path):
27-
raise HTTPException(status_code=404, detail="File not found.")
26+
zip_file_path = f"app/media/{folder_name}_zip.zip"
2827

29-
30-
return FileResponse(file_path, media_type='application/octet-stream', filename=filename)
28+
# Zip the folder
29+
zip_folder(folder_path, zip_file_path)
30+
31+
# Return the zip file as a response
32+
return FileResponse(zip_file_path, media_type='application/zip', filename=f"app/media{folder_name}_zip.zip")
3133

32-
@app.get("/list-directory")
33-
def list_directory(folder: str):
34-
# Ensure the folder exists
35-
if not os.path.isdir(folder):
36-
raise HTTPException(status_code=404, detail=f"{folder} does not exist.")
3734

38-
# List the contents of the directory
39-
contents = os.listdir(folder)
40-
return {"folder": folder, "contents": contents}

0 commit comments

Comments
 (0)