Introduce abstraction for s3 metadata

S3 metadata only supports ascii characters. Whenever we save data to it
we need to make sure we encode it to save it and then decode it to
display it again to users. This abstraction will act as the place for
that decoding to happen so the rest of the code in our views doesn't
need to care about the encoding abstraction.
This commit is contained in:
David McDonald
2020-05-18 15:15:54 +01:00
parent e79ea0e383
commit ba80d5b7cd
2 changed files with 30 additions and 18 deletions

View File

@@ -48,9 +48,20 @@ def get_letter_pdf_and_metadata(service_id, file_id):
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'])
class LetterMetadata:
KEYS_TO_DECODE = []
def __init__(self, metadata):
self._metadata = metadata
def get(self, key, default=None):
if key in self.KEYS_TO_DECODE:
return urllib.parse.unquote(self._metadata.get(key, default))
return self._metadata.get(key, default)
def get_letter_metadata(service_id, file_id):
@@ -58,4 +69,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'])