aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-08-03 17:24:35 +0100
committerMatthew Lemon <y@yulqen.org>2024-08-03 17:24:35 +0100
commit1afd527efe4e2a1a481b9cf52cdd915356b1d58a (patch)
tree059374a13ce3fec6979e34a3645a5f48b2a13eb8 /pyblackbird_cc/resources/views.py
parent7b757fcd38ca560139aa3c4c86ae57b4c6afe2dd (diff)
Adds ability to update the thumbnail/feature images
Diffstat (limited to 'pyblackbird_cc/resources/views.py')
-rw-r--r--pyblackbird_cc/resources/views.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py
index 2feb655..4ce2472 100644
--- a/pyblackbird_cc/resources/views.py
+++ b/pyblackbird_cc/resources/views.py
@@ -210,6 +210,29 @@ def upload_to_s3(pdf_files, thumbnail_files, snapshotted_pages) -> bool:
return False
+def upload_thumbnails_to_s3(thumbnail_files) -> bool:
+ session = boto3.Session()
+ client = session.client(
+ "s3",
+ endpoint_url=settings.AWS_S3_ENDPOINT_URL,
+ aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
+ aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
+ region_name=settings.AWS_S3_REGION_NAME,
+ )
+ try:
+ for f in thumbnail_files:
+ logger.info(f"Uploading {f.name} to S3")
+ client.upload_fileobj(
+ f,
+ settings.AWS_STORAGE_BUCKET_NAME,
+ f"thumbnails/{f.name}",
+ )
+ return True
+ except Exception as e: # Any exceptions generated by boto3 client will be caught here
+ logger.error(f"Error uploading thumbnail files to S3: {e}")
+ return False
+
+
def _write_pdf_to_tempdir(f) -> str:
temp_dir = tempfile.mkdtemp()
file_path = os.path.join(temp_dir, f.name)
@@ -376,6 +399,13 @@ def update_resource_thumbnails(request, pk):
resource = get_object_or_404(Resource, pk=pk)
if request.method == "POST":
form = ResourceUpdateThumbnailsForm(request.POST, request.FILES)
+ if form.is_valid():
+ thumbnail_files = form.cleaned_data["thumbnail_files"]
+ resource.thumbnail_filenames = [f.name for f in thumbnail_files]
+ if not upload_thumbnails_to_s3(thumbnail_files):
+ raise Exception("Error uploading files to S3")
+ resource.save()
+ return redirect("resources:resource_detail", resource_id=resource.id)
else:
form = ResourceUpdateThumbnailsForm(resource=pk)