Skip to content

Commit

Permalink
refactor custom_response_handler to download from s3
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Sep 30, 2024
1 parent 782fcdb commit 5c7bfba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
17 changes: 8 additions & 9 deletions onadata/libs/utils/api_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion onadata/libs/utils/image_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5c7bfba

Please sign in to comment.