Skip to content

Commit

Permalink
Overwrite CKAN download prep
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed Sep 20, 2023
1 parent 1dcb6c1 commit 30a7cd4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
29 changes: 26 additions & 3 deletions ckanext/big_resources/plugin.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckanext.big_resources.views as views
from ckan.lib.uploader import ResourceUpload as DefaultResourceUpload
from ckan.lib.uploader import _copy_file
import ckanext.big_resources.views as views
import os
import requests
from requests_toolbelt.multipart import encoder
from ckan.lib.uploader import _copy_file
import ckan.logic as logic
from ckan.common import g, request
from requests_toolbelt.multipart.encoder import (
MultipartEncoder,
MultipartEncoderMonitor,
)


class BigResourcesPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IUploader, inherit=True)
plugins.implements(plugins.IBlueprint)

def get_resource_uploader(self, data_dict):
return ResourceUpload(data_dict)
Expand All @@ -22,6 +30,11 @@ def update_config(self, config_):
toolkit.add_resource('fanstatic',
'big_resources')

# IBlueprint

def get_blueprint(self):
return views.get_blueprints()


class ResourceUpload(DefaultResourceUpload):

Expand All @@ -47,7 +60,6 @@ def upload(self, id, max_size=10):
# we write it to the filepath (and overwrite it if it already
# exists). This way the uploaded file will always be stored
# in the same location
breakpoint()
if self.filename:
try:
os.makedirs(directory)
Expand All @@ -66,5 +78,16 @@ def upload(self, id, max_size=10):
self.upload_file.close()
os.rename(tmp_filepath, filepath)
return

# The resource form only sets self.clear (via the input clear_upload)
# to True when an uploaded file is not replaced by another uploaded
# file, only if it is replaced by a link to file.
# If the uploaded file is replaced by a link, we should remove the
# previously uploaded file to clean up the file system.
if self.clear:
try:
os.remove(filepath)
except OSError as e:
pass


55 changes: 55 additions & 0 deletions ckanext/big_resources/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from flask import Blueprint
import ckan.model as model
import flask
import ckan.lib.helpers as h
import ckan.lib.uploader as uploader
import ckan.lib.base as base
import ckan.logic as logic
from ckan.common import _, g


big_resources = Blueprint("big_resources", __name__)
get_action = logic.get_action
NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized

@big_resources.route("/dataset/<id>/resource/<resource_id>/download")
@big_resources.route("/dataset/<id>/resource/<resource_id>/download/<filename>")
def download(id, resource_id, filename=None, package_type='dataset'):
"""
Provides a direct download by either redirecting the user to the url
stored or downloading an uploaded file directly.
"""
context = {
u'model': model,
u'session': model.Session,
u'user': g.user,
u'auth_user_obj': g.userobj
}

try:
rsc = get_action(u'resource_show')(context, {u'id': resource_id})
get_action(u'package_show')(context, {u'id': id})
except NotFound:
return base.abort(404, _(u'Resource not found'))
except NotAuthorized:
return base.abort(403, _(u'Not authorized to download resource'))

if rsc.get(u'url_type') == u'upload':
upload = uploader.get_resource_uploader(rsc)
filepath = upload.get_path(rsc[u'id'])
# breakpoint()
resp = flask.send_file(filepath)
if rsc.get(u'mimetype'):
resp.headers[u'Content-Type'] = rsc[u'mimetype']
return resp

elif u'url' not in rsc:
return base.abort(404, _(u'No download is available'))
return h.redirect_to(rsc[u'url'])




def get_blueprints():
return [big_resources]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests-toolbelt

0 comments on commit 30a7cd4

Please sign in to comment.