|
2 | 2 | from fastapi import FastAPI, HTTPException,Response |
3 | 3 | from fastapi.responses import FileResponse |
4 | 4 | import os |
| 5 | +import zipfile |
5 | 6 |
|
6 | 7 |
|
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)) |
9 | 16 |
|
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.") |
15 | 17 |
|
16 | | - |
17 | | - return FileResponse(file_path, media_type='application/octet-stream', filename=filename) |
18 | 18 |
|
19 | 19 |
|
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") |
22 | 25 |
|
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" |
28 | 27 |
|
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") |
31 | 33 |
|
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.") |
37 | 34 |
|
38 | | - # List the contents of the directory |
39 | | - contents = os.listdir(folder) |
40 | | - return {"folder": folder, "contents": contents} |
0 commit comments