forked from vrk-kpa/ckanext-cloudstorage
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from datopian/resoure-download
Enable resource download for ckan 2.9.4 running on azure
- Loading branch information
Showing
6 changed files
with
147 additions
and
45 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
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 @@ | ||
from .resource_download import resource_blueprint |
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,77 @@ | ||
import os.path | ||
|
||
from ckan.plugins.toolkit import c, _ | ||
from ckan import logic, model | ||
from ckan.lib import base, uploader | ||
import ckan.lib.helpers as h | ||
from flask import Blueprint | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
resource_blueprint = Blueprint('cloudresource', __name__) | ||
|
||
def resource_download(id, resource_id, filename= None): | ||
context = { | ||
'model': model, | ||
'session': model.Session, | ||
'user': c.user or c.author, | ||
'auth_user_obj': c.userobj | ||
} | ||
|
||
try: | ||
resource = logic.get_action('resource_show')( | ||
context, | ||
{ | ||
'id': resource_id | ||
} | ||
) | ||
except logic.NotFound: | ||
base.abort(404, _('Resource not found')) | ||
except logic.NotAuthorized: | ||
base.abort(401, _('Unauthorized to read resource {0}'.format(id))) | ||
|
||
# This isn't a file upload, so either redirect to the source | ||
# (if available) or error out. | ||
if resource.get('url_type') != 'upload': | ||
url = resource.get('url') | ||
if not url: | ||
base.abort(404, _('No download is available')) | ||
h.redirect_to(url) | ||
|
||
if filename is None: | ||
# No filename was provided so we'll try to get one from the url. | ||
filename = os.path.basename(resource['url']) | ||
|
||
upload = uploader.get_resource_uploader(resource) | ||
|
||
# if the client requests with a Content-Type header (e.g. Text preview) | ||
# we have to add the header to the signature | ||
try: | ||
content_type = getattr(c.pylons.request, "content_type", None) | ||
except AttributeError: | ||
content_type = None | ||
uploaded_url = upload.get_url_from_filename(resource['id'], filename, | ||
content_type=content_type) | ||
|
||
# The uploaded file is missing for some reason, such as the | ||
# provider being down. | ||
if uploaded_url is None: | ||
base.abort(404, _('No download is available')) | ||
|
||
return h.redirect_to(uploaded_url) | ||
|
||
|
||
|
||
resource_blueprint.add_url_rule( | ||
rule='/dataset/{id}/resource/{resource_id}/download', | ||
view_func = resource_download, | ||
methods= [u'GET'] | ||
) | ||
|
||
|
||
resource_blueprint.add_url_rule( | ||
rule=u'/dataset/<id>/resource/<resource_id>/download/<filename>', | ||
view_func = resource_download, | ||
methods= [u'GET'] | ||
) |
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