Download-Update
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from fastapi import FastAPI, HTTPException, UploadFile, File
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.responses import FileResponse, StreamingResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -117,6 +119,54 @@ def get_file(name: str, filename: str):
|
||||
return FileResponse(target)
|
||||
|
||||
|
||||
@app.get("/api/download-all")
|
||||
def download_all_projects():
|
||||
projects = [p for p in PROJECTS_DIR.iterdir() if p.is_dir()]
|
||||
files_found = False
|
||||
|
||||
buffer = io.BytesIO()
|
||||
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for project in projects:
|
||||
for f in project.iterdir():
|
||||
if f.is_file() and f.suffix.lower() in ALLOWED_EXTENSIONS:
|
||||
zf.write(f, arcname=f"{project.name}/{f.name}")
|
||||
files_found = True
|
||||
|
||||
if not files_found:
|
||||
raise HTTPException(404, "No files to download")
|
||||
|
||||
buffer.seek(0)
|
||||
return StreamingResponse(
|
||||
buffer,
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": 'attachment; filename="alle-projekte.zip"'},
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/projects/{name}/download")
|
||||
def download_project(name: str):
|
||||
path = safe_project_path(name)
|
||||
if not path.exists():
|
||||
raise HTTPException(404, "Project not found")
|
||||
|
||||
files = [f for f in path.iterdir() if f.is_file() and f.suffix.lower() in ALLOWED_EXTENSIONS]
|
||||
if not files:
|
||||
raise HTTPException(404, "Project has no files to download")
|
||||
|
||||
buffer = io.BytesIO()
|
||||
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for f in files:
|
||||
zf.write(f, arcname=f.name)
|
||||
buffer.seek(0)
|
||||
|
||||
zip_filename = f"{name}.zip"
|
||||
return StreamingResponse(
|
||||
buffer,
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": f'attachment; filename="{zip_filename}"'},
|
||||
)
|
||||
|
||||
|
||||
@app.delete("/api/projects/{name}/files/{filename}")
|
||||
def delete_file(name: str, filename: str):
|
||||
path = safe_project_path(name)
|
||||
|
||||
Reference in New Issue
Block a user