From 5c7bfba952702ea0d0b689ea3396058b86f6abfd Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Mon, 30 Sep 2024 18:56:04 +0300 Subject: [PATCH] refactor custom_response_handler to download from s3 --- onadata/libs/utils/api_export_tools.py | 17 ++++++++--------- onadata/libs/utils/image_tools.py | 8 +++++++- onadata/libs/utils/logger_tools.py | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/onadata/libs/utils/api_export_tools.py b/onadata/libs/utils/api_export_tools.py index 26073e33e4..8a747a7867 100644 --- a/onadata/libs/utils/api_export_tools.py +++ b/onadata/libs/utils/api_export_tools.py @@ -70,7 +70,6 @@ ) from onadata.libs.utils.google import create_flow from onadata.libs.utils.image_tools import generate_media_download_url -from onadata.libs.utils.logger_tools import response_with_mimetype_and_name from onadata.libs.utils.model_tools import get_columns_with_hxl from onadata.settings.common import XLS_EXTENSIONS @@ -234,15 +233,15 @@ def _new_export(): ) else: show_date = False - response = response_with_mimetype_and_name( - Export.EXPORT_MIMES[ext], - filename, - extension=ext, - show_date=show_date, - file_path=export.filepath, - ) - return response + mimetype = f"application/{Export.EXPORT_MIMES[ext]}" + + if Export.EXPORT_MIMES[ext] == "csv": + mimetype = "text/csv" + + return generate_media_download_url( + export.filepath, mimetype, f"{filename}.{ext}", show_date + ) def _generate_new_export( # noqa: C0901 diff --git a/onadata/libs/utils/image_tools.py b/onadata/libs/utils/image_tools.py index 217573aa6e..c42235aaba 100644 --- a/onadata/libs/utils/image_tools.py +++ b/onadata/libs/utils/image_tools.py @@ -30,7 +30,7 @@ def flat(*nums): def generate_media_download_url( - file_path, mimetype, filename=None, expiration: int = 3600 + file_path, mimetype, filename=None, show_date=False, expiration: int = 3600 ): """ Returns a HTTP response of a media object or a redirect to the image URL for S3 and @@ -41,6 +41,12 @@ def generate_media_download_url( if not filename: filename = file_path.split("/")[-1] + if show_date: + timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + # Split the filename and extension + name, ext = filename.rsplit(".", 1) + filename = f"{name}-{timestamp}.{ext}" + # The filename is enclosed in quotes because it ensures that special characters, # spaces, or punctuation in the filename are correctly interpreted by browsers # and clients. This is particularly important for filenames that may contain diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 6393311351..47357a8d08 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -749,7 +749,7 @@ def generate_content_disposition_header(name, extension, show_date=True): if show_date: timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S") name = f"{name}-{timestamp}" - return f"attachment; filename={name}.{extension}" + return f'attachment; filename="{name}.{extension}"' def store_temp_file(data):