Skip to content

Commit

Permalink
Use FileResponse for asynchronous serving of individual tiff files an…
Browse files Browse the repository at this point in the history
…d write in 32 MB chunks on the client side (#275)

Use FileResponse for asynchronous serving of individual TIFF files and write in 32 MB chunks on the client side
  • Loading branch information
d-j-hatton authored May 17, 2024
1 parent 39e449e commit 9faf97e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/murfey/client/tui/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,12 @@ def on_button_pressed(self, event: Button.Pressed):
upstream_tiff_paths = upstream_tiff_paths_response.json()
for tp in upstream_tiff_paths:
(download_dir / tp).parent.mkdir(exist_ok=True, parents=True)
stream_response = requests.get(
f"{self.app._environment.url.geturl()}/visits/{event.button.label}/upstream_tiff/{tp}",
stream=True,
)
with open(download_dir / tp, "wb") as utiff:
stream_response = requests.get(
f"{self.app._environment.url.geturl()}/visits/{event.button.label}/upstream_tiff/{tp}",
stream=True,
)
for chunk in stream_response.iter_content():
for chunk in stream_response.iter_content(chunk_size=32 * 1024**2):
utiff.write(chunk)
self.app.pop_screen()

Expand Down
12 changes: 2 additions & 10 deletions src/murfey/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import packaging.version
import sqlalchemy
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, StreamingResponse
from fastapi.responses import FileResponse, HTMLResponse
from ispyb.sqlalchemy import AutoProcProgram as ISPyBAutoProcProgram
from ispyb.sqlalchemy import (
BLSample,
Expand Down Expand Up @@ -1640,12 +1640,4 @@ async def get_tiff(visit_name: str, tiff_path: str):

tiff_path = "/".join(secure_filename(p) for p in tiff_path.split("/"))

def iterfile():
with open(processed_dir / tiff_path, mode="rb") as f:
yield from f

return StreamingResponse(
iterfile(),
media_type="image/tiff",
headers={"Content-Disposition": f"attachment; filename={Path(tiff_path).name}"},
)
return FileResponse(path=processed_dir / tiff_path)
12 changes: 2 additions & 10 deletions src/murfey/server/demo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import packaging.version
import sqlalchemy
from fastapi import APIRouter, Request
from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse
from fastapi.responses import FileResponse, HTMLResponse
from ispyb.sqlalchemy import BLSession
from PIL import Image
from pydantic import BaseModel, BaseSettings
Expand Down Expand Up @@ -1475,12 +1475,4 @@ async def get_tiff(visit_name: str, tiff_path: str):

tiff_path = "/".join(secure_filename(p) for p in tiff_path.split("/"))

def iterfile():
with open(processed_dir / tiff_path, mode="rb") as f:
yield from f

return StreamingResponse(
iterfile(),
media_type="image/tiff",
headers={"Content-Disposition": f"attachment; filename={Path(tiff_path).name}"},
)
return FileResponse(path=processed_dir / tiff_path)

0 comments on commit 9faf97e

Please sign in to comment.