Skip to content

Commit

Permalink
zip in fileresponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ychiucco committed Aug 7, 2024
1 parent d7f4928 commit f938f52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
31 changes: 19 additions & 12 deletions fractal_server/app/routes/api/v2/job.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
from pathlib import Path
from typing import Optional

from fastapi import APIRouter
from fastapi import Depends
from fastapi import Response
from fastapi import status
from fastapi.responses import FileResponse
from fastapi.responses import StreamingResponse
from sqlmodel import select

Expand Down Expand Up @@ -109,14 +111,13 @@ async def read_job(

@router.get(
"/project/{project_id}/job/{job_id}/download/",
response_class=StreamingResponse,
)
async def download_job_logs(
project_id: int,
job_id: int,
user: User = Depends(current_active_user),
db: AsyncSession = Depends(get_async_db),
) -> StreamingResponse:
):
"""
Download job folder
"""
Expand All @@ -127,16 +128,22 @@ async def download_job_logs(
db=db,
)
job = output["job"]

# Create and return byte stream for zipped log folder
PREFIX_ZIP = Path(job.working_dir).name
zip_filename = f"{PREFIX_ZIP}_archive.zip"
byte_stream = _zip_folder_to_byte_stream(folder=job.working_dir)
return StreamingResponse(
iter([byte_stream.getvalue()]),
media_type="application/x-zip-compressed",
headers={"Content-Disposition": f"attachment;filename={zip_filename}"},
)
if not os.path.exists(f"{job.working_dir}.zip"):
zip_filename = f"{Path(job.working_dir).name}.zip"
byte_stream = _zip_folder_to_byte_stream(folder=job.working_dir)
return StreamingResponse(
iter([byte_stream.getvalue()]),
media_type="application/x-zip-compressed",
headers={
"Content-Disposition": f"attachment;filename={zip_filename}"
},
)
else:
FileResponse(
path=Path(job.working_dir).parent,
filename=f"{Path(job.working_dir).name}.zip",
media_type="application/zip",
)


@router.get(
Expand Down
13 changes: 7 additions & 6 deletions tests/v2/03_api/test_api_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,20 @@ async def test_submit_jobs_with_same_dataset(
)

# Existing jobs with done/running status
existing_job_A_done = await job_factory_v2(
await job_factory_v2(
project_id=project.id,
dataset_id=dataset1.id,
workflow_id=workflow.id,
working_dir=tmp_path.as_posix(),
status="done",
)
debug(existing_job_A_done)
existing_job_B_done = await job_factory_v2(
await job_factory_v2(
project_id=project.id,
dataset_id=dataset2.id,
workflow_id=workflow.id,
working_dir=tmp_path.as_posix(),
status="submitted",
)
debug(existing_job_B_done)

# API call succeeds when the other job with the same dataset has
# status="done"
Expand All @@ -153,17 +151,20 @@ async def test_submit_jobs_with_same_dataset(
f"?workflow_id={workflow.id}&dataset_id={dataset1.id}",
json={},
)
debug(res.json())
assert res.status_code == 202

res = await client.get(
f"{PREFIX}/project/{project.id}/job/{res.json()['id']}/download/"
)
debug(res.json())

# API call fails when the other job with the same output_dataset has
# status="done"
res = await client.post(
f"{PREFIX}/project/{project.id}/job/submit/"
f"?workflow_id={workflow.id}&dataset_id={dataset2.id}",
json={},
)
debug(res.json())
assert res.status_code == 422
assert (
f"Dataset {dataset2.id} is already in use" in res.json()["detail"]
Expand Down

0 comments on commit f938f52

Please sign in to comment.