2019-10-15 15:55:17 +01:00
|
|
|
import json
|
2020-01-02 16:35:54 +00:00
|
|
|
import urllib
|
2019-10-15 15:55:17 +01:00
|
|
|
|
2019-09-09 10:59:32 +01:00
|
|
|
from boto3 import resource
|
2019-09-06 17:10:48 +01:00
|
|
|
from flask import current_app
|
|
|
|
|
from notifications_utils.s3 import s3upload as utils_s3upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_transient_letter_file_location(service_id, upload_id):
|
|
|
|
|
return 'service-{}/{}.pdf'.format(service_id, upload_id)
|
|
|
|
|
|
|
|
|
|
|
2019-11-08 09:56:59 +00:00
|
|
|
def upload_letter_to_s3(
|
|
|
|
|
data,
|
|
|
|
|
*,
|
|
|
|
|
file_location,
|
|
|
|
|
status,
|
|
|
|
|
page_count,
|
|
|
|
|
filename,
|
|
|
|
|
message=None,
|
|
|
|
|
invalid_pages=None,
|
|
|
|
|
recipient=None
|
|
|
|
|
):
|
2020-05-18 15:26:35 +01:00
|
|
|
# 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
|
2019-10-15 15:55:17 +01:00
|
|
|
metadata = {
|
|
|
|
|
'status': status,
|
|
|
|
|
'page_count': str(page_count),
|
2020-05-18 15:26:35 +01:00
|
|
|
'filename': urllib.parse.quote(filename),
|
2019-10-15 15:55:17 +01:00
|
|
|
}
|
|
|
|
|
if message:
|
|
|
|
|
metadata['message'] = message
|
|
|
|
|
if invalid_pages:
|
|
|
|
|
metadata['invalid_pages'] = json.dumps(invalid_pages)
|
2019-11-08 09:56:59 +00:00
|
|
|
if recipient:
|
2020-01-02 16:35:54 +00:00
|
|
|
metadata['recipient'] = urllib.parse.quote(recipient)
|
2019-10-15 15:55:17 +01:00
|
|
|
|
2019-09-06 17:10:48 +01:00
|
|
|
utils_s3upload(
|
|
|
|
|
filedata=data,
|
|
|
|
|
region=current_app.config['AWS_REGION'],
|
|
|
|
|
bucket_name=current_app.config['TRANSIENT_UPLOADED_LETTERS'],
|
|
|
|
|
file_location=file_location,
|
2019-10-15 15:55:17 +01:00
|
|
|
metadata=metadata,
|
2019-09-06 17:10:48 +01:00
|
|
|
)
|
2019-09-09 10:59:32 +01:00
|
|
|
|
|
|
|
|
|
2020-05-18 15:15:54 +01:00
|
|
|
class LetterMetadata:
|
2020-05-18 15:44:20 +01:00
|
|
|
KEYS_TO_DECODE = ["filename", "recipient"]
|
2020-05-18 15:15:54 +01:00
|
|
|
|
|
|
|
|
def __init__(self, metadata):
|
|
|
|
|
self._metadata = metadata
|
|
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
2020-05-18 15:44:20 +01:00
|
|
|
value = self._metadata.get(key, default)
|
|
|
|
|
if value and key in self.KEYS_TO_DECODE:
|
|
|
|
|
value = urllib.parse.unquote(value)
|
|
|
|
|
return value
|
2019-10-07 15:22:11 +01:00
|
|
|
|
|
|
|
|
|
2020-05-18 15:26:35 +01:00
|
|
|
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'])
|
|
|
|
|
|
|
|
|
|
|
2019-10-07 15:22:11 +01:00
|
|
|
def get_letter_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()
|
|
|
|
|
|
2020-05-18 15:15:54 +01:00
|
|
|
return LetterMetadata(s3_object['Metadata'])
|