Specify mimetype for PDF letters to be downloaded

We had a report that when clicking on the 'Download this letter' link on
the notification page the file was not being downloaded as a PDF file
but was given a `.htm` file extension instead. We should be able to stop
that happening by using Flask's `send_file` function with the right mimetype.
This change updates the `view_letter_notification_as_preview` to use
`send_file` and splits out code to get the file data into a separate
function.

Mocks in the tests have been updated and some unused mocks removed.
This commit is contained in:
Katie Smith
2020-04-07 17:13:28 +01:00
parent 2992aacd8a
commit 6b055071f4
2 changed files with 20 additions and 17 deletions

View File

@@ -13,6 +13,7 @@ from flask import (
redirect,
render_template,
request,
send_file,
stream_with_context,
url_for,
)
@@ -57,7 +58,7 @@ def view_notification(service_id, notification_id):
error_message = None
if notification['template']['is_precompiled_letter']:
try:
file_contents, metadata = view_letter_notification_as_preview(
file_contents, metadata = get_letter_file_data(
service_id, notification_id, "pdf", with_metadata=True
)
page_count = int(
@@ -187,6 +188,18 @@ def get_preview_error_image():
def view_letter_notification_as_preview(
service_id, notification_id, filetype, with_metadata=False
):
image_data = get_letter_file_data(service_id, notification_id, filetype, with_metadata)
file = io.BytesIO(image_data)
mimetype = 'image/png' if filetype == 'png' else 'application/pdf'
return send_file(
filename_or_fp=file,
mimetype=mimetype,
)
def get_letter_file_data(service_id, notification_id, filetype, with_metadata=False):
try:
preview = notification_api_client.get_notification_letter_preview(
service_id,