-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
28 lines (25 loc) · 1.01 KB
/
main.py
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
import zipfile
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import io
import os
## FastApi configuration
app = FastAPI()
## Route
@app.post("/zip/download")
@app.post("/zip/download/")
async def zip_download():
zip_bytes_io = io.BytesIO()
with zipfile.ZipFile(zip_bytes_io, 'w', zipfile.ZIP_DEFLATED) as zipped:
for dirname, subdirs, files in os.walk('output'):
zipped.write(dirname)
for filename in files:
zipped.write(os.path.join(dirname, filename))
response = StreamingResponse(
iter([zip_bytes_io.getvalue()]),
media_type="application/x-zip-compressed",
headers = {"Content-Disposition":f"attachment;filename=output.zip", # 'attachment' automatically downloads the file if IDM extension is enabled on browser. Use 'inline' to avoid that.
"Content-Length": str(zip_bytes_io.getbuffer().nbytes)}
)
zip_bytes_io.close()
return response