mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 19:03:30 -05:00
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:
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user