From c67ad47ba22b5d0ce04c4225970aa615cae55d46 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Fri, 21 Sep 2012 12:16:36 +0100 Subject: [PATCH] resource proxy to serve resources resources from the same domain --- ckan/config/routing.py | 2 ++ ckan/controllers/package.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/ckan/config/routing.py b/ckan/config/routing.py index 071688fc512..9de72af4f8b 100644 --- a/ckan/config/routing.py +++ b/ckan/config/routing.py @@ -217,6 +217,8 @@ def make_map(): action='resource_datapreview') m.connect('/dataset/{id}/resource/{resource_id}/pdfpreview', action='resource_pdfpreview') + m.connect('/dataset/{id}/resource/{resource_id}/proxy', + action='resource_proxy') # group map.redirect('/groups', '/group') diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index f5cfe22d923..fa92e7f5ff9 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1,6 +1,7 @@ import logging from urllib import urlencode import datetime +import urllib2 from pylons import config from pylons.i18n import _ @@ -1307,6 +1308,9 @@ def resource_pdfpreview(self, id, resource_id): try: c.resource = get_action('resource_show')(context, {'id': resource_id}) + c.resource['url'] = h.url_for(controller='package', + action='resource_proxy', id=id, resource_id=resource_id) + c.package = get_action('package_show')(context, {'id': id}) c.resource_json = json.dumps(c.resource) except NotFound: @@ -1314,3 +1318,16 @@ def resource_pdfpreview(self, id, resource_id): except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % id) return render('package/resource_pdfpreview.html') + + def resource_proxy(self, resource_id): + context = {'model': model, 'session': model.Session, + 'user': c.user or c.author} + resource = get_action('resource_show')(context, + {'id': resource_id}) + url = resource['url'] + + req = urllib2.urlopen(url) + response.headers = req.headers + + import shutil + shutil.copyfileobj(req, response)