forked from devopshobbies/devops-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (30 loc) · 1.43 KB
/
utils.py
File metadata and controls
43 lines (30 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from app.app_instance import app
from fastapi import FastAPI, HTTPException,Response
from fastapi.responses import FileResponse
import os
import zipfile
import shutil
def zip_folder(folder_path: str, output_zip_path: str):
"""Zip the entire folder."""
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
# Add file to the zip file
zip_file.write(file_path, os.path.relpath(file_path, folder_path))
def add_files_to_folder(files:list,folder:str):
os.makedirs(folder, exist_ok=True)
for filename in files:
os.path.join(folder, filename)
destination_file = os.path.join(folder, os.path.basename(filename))
shutil.copy(filename, destination_file)
@app.get("/api/download-folder{folder_name}/{source}")
async def download_folder_MyHelm(folder_name: str,source:str):
folder_path = f"app/media/{folder_name}" # Adjust the path as needed
if not os.path.exists(folder_path):
raise HTTPException(status_code=404, detail="Folder not found")
zip_file_path = f"app/media/{folder_name}_zip.zip"
# Zip the folder
zip_folder(folder_path, zip_file_path)
# Return the zip file as a response
return FileResponse(zip_file_path, media_type='application/zip', filename=f"{folder_name}_{source}.zip")