Merge pull request #3450 from alphagov/non-ascii-metadata

Non ascii metadata
This commit is contained in:
David McDonald
2020-05-19 11:31:26 +01:00
committed by GitHub
4 changed files with 62 additions and 33 deletions

View File

@@ -1,7 +1,6 @@
import base64
import itertools
import json
import urllib
import uuid
from datetime import datetime
from functools import partial
@@ -255,14 +254,12 @@ def _get_error_from_upload_form(form_errors):
def format_recipient(address):
'''
To format the recipient we need to:
- decode, address is url encoded
- remove new line characters
- remove whitespace around the lines
- join the address lines, separated by a comma
'''
if not address:
return address
address = urllib.parse.unquote(address)
stripped_address_lines_no_trailing_commas = [
line.lstrip().rstrip(' ,')
for line in address.splitlines() if line

View File

@@ -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,15 +44,27 @@ def upload_letter_to_s3(
)
class LetterMetadata:
KEYS_TO_DECODE = ["filename", "recipient"]
def __init__(self, metadata):
self._metadata = metadata
def get(self, key, default=None):
value = self._metadata.get(key, default)
if value and key in self.KEYS_TO_DECODE:
value = urllib.parse.unquote(value)
return value
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()
metadata = s3_object['Metadata']
return pdf, metadata
return pdf, LetterMetadata(s3_object['Metadata'])
def get_letter_metadata(service_id, file_id):
@@ -58,4 +72,4 @@ def get_letter_metadata(service_id, file_id):
s3 = resource('s3')
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
return s3_object['Metadata']
return LetterMetadata(s3_object['Metadata'])