-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dcb6c1
commit 30a7cd4
Showing
3 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests-toolbelt |