Skip to content

Commit

Permalink
resource proxy to serve resources resources from the same domain
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 21, 2012
1 parent 9e0d72c commit c67ad47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ckan/config/routing.py
Expand Up @@ -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')
Expand Down
17 changes: 17 additions & 0 deletions 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 _
Expand Down Expand Up @@ -1307,10 +1308,26 @@ 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:
abort(404, _('Resource not found'))
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)

1 comment on commit c67ad47

@domoritz
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is only for demonstration purposes and is not intended to be merged into CKAN.

Please sign in to comment.