diff --git a/ckanext/archiver/plugin.py b/ckanext/archiver/plugin.py index beb37e2..4a6b3a9 100644 --- a/ckanext/archiver/plugin.py +++ b/ckanext/archiver/plugin.py @@ -10,6 +10,7 @@ from ckanext.archiver import lib from ckanext.archiver.model import Archival, aggregate_archivals_for_a_dataset from ckanext.archiver import cli +from ckanext.archiver.views import s3_archived log = logging.getLogger(__name__) @@ -27,6 +28,7 @@ class ArchiverPlugin(p.SingletonPlugin, p.toolkit.DefaultDatasetForm): p.implements(p.IAuthFunctions) p.implements(p.ITemplateHelpers) p.implements(p.IPackageController, inherit=True) + p.implements(p.IBlueprint) if p.toolkit.check_ckan_version(min_version='2.9.0'): p.implements(p.IClick) @@ -230,6 +232,12 @@ def before_dataset_index(self, pkg_dict): def get_commands(self): return cli.get_commands() + + # IBlueprint + + def get_blueprint(self): + + return s3_archived class TestIPipePlugin(p.SingletonPlugin): diff --git a/ckanext/archiver/tasks.py b/ckanext/archiver/tasks.py index e598218..b9029c0 100644 --- a/ckanext/archiver/tasks.py +++ b/ckanext/archiver/tasks.py @@ -599,6 +599,8 @@ def archive_resource(context, resource, log, result=None, url_timeout=30): # delete the file from archive storage if os.path.isfile(saved_file): os.remove(saved_file) + # save in database for cachefile_path the path from S3 storage + saved_file = s3_file_path else: shutil.move(result['saved_file'], saved_file) diff --git a/ckanext/archiver/views.py b/ckanext/archiver/views.py new file mode 100644 index 0000000..c11ed0a --- /dev/null +++ b/ckanext/archiver/views.py @@ -0,0 +1,23 @@ +from flask import Blueprint +from ckan import model +from ckanext.archiver.model import Archival +from ckanext.cloudstorage.helpers import generate_download_signed_url_v4 +import ckantoolkit as tk + + +redirect = tk.redirect_to +config = tk.config + +s3_archived = Blueprint("s3_archived_redirect", __name__) + + +def s3_archived_redirect(id, resource_id, filename): + + q = model.Session.query(Archival).filter_by(resource_id=resource_id) + filepath = model.Session.execute(q).first()[8] + url = generate_download_signed_url_v4(filepath) + return redirect(url) + + +s3_archived.add_url_rule(u'/tmp///', + view_func=s3_archived_redirect)