Skip to content

Commit

Permalink
ref: only serve preview images from S3
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Feb 24, 2024
1 parent 526a02b commit 71782f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.9.0
- ref: don't create any preview files locally, upload directly to S3
- ref: only serve preview images from S3
- ref: migrate to dcor_shared 0.7.4
0.8.2
- maintenance release
Expand Down
31 changes: 6 additions & 25 deletions ckanext/dc_view/route_funcs.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import pathlib

import flask
from ckan.common import c
import ckan.lib.uploader as uploader
from ckan import logic
import ckan.model as model
import ckan.plugins.toolkit as toolkit

import botocore.exceptions
from dcor_shared import s3, s3cc


def dcpreview(ds_id, res_id):
"""Serve a preview image on disk
"""Redirect to a preview image on S3 for a DC resource
Parameters
----------
Expand All @@ -37,17 +32,9 @@ def dcpreview(ds_id, res_id):
res_stem, _ = res_dict["name"].rsplit(".", 1)
prev_name = f"{res_stem}_preview.jpg"

if s3 is not None and res_dict.get('s3_available'):
# check if the corresponding S3 object
bucket_name, object_name = s3cc.get_s3_bucket_object_for_artifact(
rid, artifact="preview")
s3_client, _, _ = s3.get_s3()
try:
s3_client.head_object(Bucket=bucket_name,
Key=object_name)
except botocore.exceptions.ClientError:
pass
else:
if s3.is_available() and res_dict.get('s3_available'):
# check if the corresponding S3 object exists
if s3cc.artifact_exists(resource_id=rid, artifact="preview"):
# We have an S3 object that we can redirect to. We are making use
# of presigned URLs to be able to specify a filename for download
# (otherwise, users that download via the web interface will
Expand All @@ -57,19 +44,13 @@ def dcpreview(ds_id, res_id):
expiration = 3600
else:
expiration = 86400
bucket_name, object_name = s3cc.get_s3_bucket_object_for_artifact(
resource_id=rid, artifact="preview")
ps_url = s3.create_presigned_url(
bucket_name=bucket_name,
object_name=object_name,
filename=prev_name,
expiration=expiration)
return toolkit.redirect_to(ps_url)

if res_dict.get('url_type') == 'upload':
upload = uploader.get_resource_uploader(res_dict)
filepath = pathlib.Path(upload.get_path(res_dict['id']))
jpg_file = filepath.with_name(filepath.name + "_preview.jpg")
if not jpg_file.exists():
return toolkit.abort(404, toolkit._('Preview not found'))
return flask.send_from_directory(jpg_file.parent, jpg_file.name,
attachment_filename=prev_name)
return toolkit.abort(404, toolkit._('No preview available'))

0 comments on commit 71782f5

Please sign in to comment.