Copy original file tp redaction_failure folder when redaction fails

This commit is contained in:
Pea Tyczynska
2019-09-10 10:59:12 +01:00
parent b5b72a83d6
commit fecd7b5728
4 changed files with 108 additions and 8 deletions

View File

@@ -31,6 +31,7 @@ from app.dao.notifications_dao import (
)
from app.errors import VirusScanError
from app.letters.utils import (
copy_redaction_failed_pdf,
get_reference_from_filename,
get_folder_name,
upload_letter_pdf,
@@ -219,10 +220,19 @@ def process_virus_scan_passed(self, filename):
if not sanitise_response:
new_pdf = None
else:
sanitise_response = sanitise_response.json()
try:
new_pdf = base64.b64decode(sanitise_response.json()["file"].encode())
new_pdf = base64.b64decode(sanitise_response["file"].encode())
except JSONDecodeError:
new_pdf = sanitise_response.content
redaction_failed_message = sanitise_response.get("redaction_failed_message")
if redaction_failed_message:
current_app.logger.info('{} for notification id {} ({})'.format(
redaction_failed_message, notification.id, filename)
)
copy_redaction_failed_pdf(filename)
# TODO: Remove this once CYSP update their template to not cross over the margins
if notification.service_id == UUID('fe44178f-3b45-4625-9f85-2264a36dd9ec'): # CYSP
# Check your state pension submit letters with good addresses and notify tags, so just use their supplied pdf

View File

@@ -113,6 +113,14 @@ def move_failed_pdf(source_filename, scan_error_type):
_move_s3_object(scan_bucket, source_filename, scan_bucket, target_filename)
def copy_redaction_failed_pdf(source_filename):
scan_bucket = current_app.config['LETTERS_SCAN_BUCKET_NAME']
target_filename = 'REDACTION_FAILURE/' + source_filename
_copy_s3_object(scan_bucket, source_filename, scan_bucket, target_filename)
def move_error_pdf_to_scan_bucket(source_filename):
scan_bucket = current_app.config['LETTERS_SCAN_BUCKET_NAME']
error_file = 'ERROR/' + source_filename
@@ -166,6 +174,22 @@ def _move_s3_object(source_bucket, source_filename, target_bucket, target_filena
source_bucket, source_filename, target_bucket, target_filename))
def _copy_s3_object(source_bucket, source_filename, target_bucket, target_filename):
s3 = boto3.resource('s3')
copy_source = {'Bucket': source_bucket, 'Key': source_filename}
target_bucket = s3.Bucket(target_bucket)
obj = target_bucket.Object(target_filename)
# Tags are copied across but the expiration time is reset in the destination bucket
# e.g. if a file has 5 days left to expire on a ONE_WEEK retention in the source bucket,
# in the destination bucket the expiration time will be reset to 7 days left to expire
obj.copy(copy_source, ExtraArgs={'ServerSideEncryption': 'AES256'})
current_app.logger.info("Copied letter PDF: {}/{} to {}/{}".format(
source_bucket, source_filename, target_bucket, target_filename))
def letter_print_day(created_at):
bst_print_datetime = convert_utc_to_bst(created_at) + timedelta(hours=6, minutes=30)
bst_print_date = bst_print_datetime.date()