mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 14:09:20 -04:00
Ensure pdf letter filename is ascii encoded and decoded
S3 can only handle ascii characters, therefore for filename which could include non ascii characters, for example a filename with the character '£' in it, we must encode these using urllib before saving it as s3 metadata. We then also make sure that it comes back decoded when presenting it to the user.
This commit is contained in:
@@ -21,10 +21,12 @@ def upload_letter_to_s3(
|
||||
invalid_pages=None,
|
||||
recipient=None
|
||||
):
|
||||
# Use of urllib.parse.quote encodes metadata into ascii, which is required by s3.
|
||||
# Making sure data for displaying to users is decoded is taken care of by LetterMetadata
|
||||
metadata = {
|
||||
'status': status,
|
||||
'page_count': str(page_count),
|
||||
'filename': filename,
|
||||
'filename': urllib.parse.quote(filename),
|
||||
}
|
||||
if message:
|
||||
metadata['message'] = message
|
||||
@@ -42,18 +44,8 @@ def upload_letter_to_s3(
|
||||
)
|
||||
|
||||
|
||||
def get_letter_pdf_and_metadata(service_id, file_id):
|
||||
file_location = get_transient_letter_file_location(service_id, file_id)
|
||||
s3 = resource('s3')
|
||||
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
|
||||
|
||||
pdf = s3_object['Body'].read()
|
||||
|
||||
return pdf, LetterMetadata(s3_object['Metadata'])
|
||||
|
||||
|
||||
class LetterMetadata:
|
||||
KEYS_TO_DECODE = []
|
||||
KEYS_TO_DECODE = ["filename"]
|
||||
|
||||
def __init__(self, metadata):
|
||||
self._metadata = metadata
|
||||
@@ -64,6 +56,16 @@ class LetterMetadata:
|
||||
return self._metadata.get(key, default)
|
||||
|
||||
|
||||
def get_letter_pdf_and_metadata(service_id, file_id):
|
||||
file_location = get_transient_letter_file_location(service_id, file_id)
|
||||
s3 = resource('s3')
|
||||
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
|
||||
|
||||
pdf = s3_object['Body'].read()
|
||||
|
||||
return pdf, LetterMetadata(s3_object['Metadata'])
|
||||
|
||||
|
||||
def get_letter_metadata(service_id, file_id):
|
||||
file_location = get_transient_letter_file_location(service_id, file_id)
|
||||
s3 = resource('s3')
|
||||
|
||||
Reference in New Issue
Block a user