diff --git a/judgments/utils.py b/judgments/utils.py index ee93f8c1f..fb78948f4 100644 --- a/judgments/utils.py +++ b/judgments/utils.py @@ -102,8 +102,8 @@ def set_metadata(old_uri, new_uri): def copy_assets(old_uri, new_uri): client = create_s3_client() bucket = env("PRIVATE_ASSET_BUCKET") - old_uri = old_uri.lstrip("/") - new_uri = new_uri.lstrip("/") + old_uri = uri_for_s3(old_uri) + new_uri = uri_for_s3(new_uri) response = client.list_objects(Bucket=bucket, Prefix=old_uri) @@ -141,3 +141,7 @@ def create_s3_client(): region_name=env("PRIVATE_ASSET_BUCKET_REGION", default=None), config=botocore.client.Config(signature_version="s3v4"), ) + + +def uri_for_s3(uri: str): + return uri.lstrip("/") diff --git a/judgments/views.py b/judgments/views.py index 443cddaa1..b98fd529d 100644 --- a/judgments/views.py +++ b/judgments/views.py @@ -1,3 +1,4 @@ +import logging import math import re import time @@ -28,6 +29,7 @@ NeutralCitationToUriError, get_judgment_root, update_judgment_uri, + uri_for_s3, ) env = environ.Env() @@ -59,8 +61,8 @@ def get_metadata(self, uri: str, judgment: ET.Element) -> dict: xml_tools.get_neutral_citation_name_value(judgment) or "" ) meta["judgment_date"] = xml_tools.get_judgment_date_value(judgment) or "" - meta["docx_url"] = generate_docx_url(uri) - meta["pdf_url"] = generate_pdf_url(uri) + meta["docx_url"] = generate_docx_url(uri_for_s3(uri)) + meta["pdf_url"] = generate_pdf_url(uri_for_s3(uri)) meta["previous_versions"] = self.get_versions(uri) meta["consignment_reference"] = api_client.get_property( uri, "transfer-consignment-reference" @@ -111,9 +113,9 @@ def post(self, request, *args, **kwargs): api_client.set_anonymised(judgment_uri, anonymised) if published: - publish_documents(judgment_uri) + publish_documents(uri_for_s3(judgment_uri)) else: - unpublish_documents(judgment_uri) + unpublish_documents(uri_for_s3(judgment_uri)) # Set name new_name = request.POST["metadata_name"] @@ -179,8 +181,8 @@ def detail(request): judgment = multipart_data.parts[0].text context["judgment"] = judgment context["page_title"] = metadata_name - context["docx_url"] = generate_docx_url(judgment_uri) - context["pdf_url"] = generate_pdf_url(judgment_uri) + context["docx_url"] = generate_docx_url(uri_for_s3(judgment_uri)) + context["pdf_url"] = generate_pdf_url(uri_for_s3(judgment_uri)) if version_uri: try: @@ -381,7 +383,12 @@ def publish_documents(uri: str) -> None: if not key.endswith("parser.log") and not key.endswith(".tar.gz"): source = {"Bucket": private_bucket, "Key": key} extra_args = {"ACL": "public-read"} - client.copy(source, public_bucket, key, extra_args) + try: + client.copy(source, public_bucket, key, extra_args) + except botocore.client.ClientError as e: + logging.warning( + f"Unable to copy file {key} to new location {public_bucket}, error: {e}" + ) def unpublish_documents(uri: str) -> None: @@ -426,7 +433,6 @@ def generate_docx_url(uri: str): return "" client = create_s3_client() - uri = uri.lstrip("/") key = f'{uri}/{uri.replace("/", "_")}.docx' @@ -441,7 +447,6 @@ def generate_pdf_url(uri: str): return "" client = create_s3_client() - uri = uri.lstrip("/") key = f'{uri}/{uri.replace("/", "_")}.pdf'