From 2a64a79b665c14534fff51228c937fc332678fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor?= Date: Mon, 27 Sep 2021 13:45:11 -0300 Subject: [PATCH] simplify attach files --- app/main.py | 5 +++-- app/util.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 app/util.py diff --git a/app/main.py b/app/main.py index d20db37..fd55709 100644 --- a/app/main.py +++ b/app/main.py @@ -2,6 +2,7 @@ from fastapi.responses import Response from fastapi.responses import StreamingResponse +from app.util import attach_file from libraryapi.pergamum import PergamumDownloader @@ -14,7 +15,7 @@ async def get_marc(url: str, id: int) -> StreamingResponse: response = StreamingResponse( pergamumDownloader.download_iso(url, id), media_type="application/marc" ) - response.headers["Content-Disposition"] = f"attachment; filename={id}.mrc" + attach_file(response, id, "mrc") return response @@ -23,5 +24,5 @@ async def get_marcxml(url: str, id: int) -> Response: response = Response( pergamumDownloader.download_xml(url, id), media_type="application/xml" ) - response.headers["Content-Disposition"] = f"attachment; filename={id}.xml" + attach_file(response, id, "xml") return response diff --git a/app/util.py b/app/util.py new file mode 100644 index 0000000..019e8ca --- /dev/null +++ b/app/util.py @@ -0,0 +1,4 @@ +def attach_file(response, name, extension): + response.headers[ + "Content-Disposition" + ] = f"attachment; filename={name}.{extension}"