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
Showing
5 changed files
with
79 additions
and
7 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
Empty file.
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,27 @@ | ||
import urllib2 | ||
from logging import getLogger | ||
|
||
import ckan.logic as logic | ||
import ckan.lib.base as base | ||
|
||
log = getLogger(__name__) | ||
|
||
|
||
@logic.side_effect_free | ||
def proxy_resource(context, data_dict): | ||
resource_id = data_dict['resource_id'] | ||
log.info('Proxify resource {id}'.format(id=resource_id)) | ||
resource = logic.get_action('resource_show')(context, {'id': resource_id}) | ||
url = resource['url'] | ||
req = urllib2.urlopen(url) | ||
base.response.headers = req.headers | ||
|
||
import shutil | ||
shutil.copyfileobj(req, base.response) | ||
|
||
|
||
class ProxyController(base.BaseController): | ||
def proxy_resource(self, data_dict): | ||
context = {'model': base.model, 'session': base.model.Session, | ||
'user': base.c.user or base.c.author} | ||
return proxy_resource(context, data_dict) |
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,41 @@ | ||
from logging import getLogger | ||
|
||
import ckan.plugins as p | ||
import ckan.lib.base as base | ||
|
||
import controller | ||
|
||
log = getLogger(__name__) | ||
|
||
|
||
def get_proxyfied_resource_url(data_dict): | ||
url = base.h.url_for( | ||
action='proxy_resource', | ||
id=data_dict['package']['name'], | ||
resource_id=data_dict['resource']['id']) | ||
log.info('Proxified url is {0}'.format(url)) | ||
return url | ||
|
||
|
||
class ResourceProxy(p.SingletonPlugin): | ||
"""A proxy for CKAN resources to get around the same | ||
origin policy for previews | ||
This extension implements two interfaces | ||
- ``IConfigurer`` allows to modify the configuration | ||
- ``IRoutes`` allows to add a route to the proxy action | ||
- ``IActions`` allows to add an action for the proxy | ||
""" | ||
p.implements(p.IConfigurer, inherit=True) | ||
p.implements(p.IRoutes, inherit=True) | ||
p.implements(p.IActions) | ||
|
||
def before_map(self, m): | ||
m.connect('/dataset/{id}/resource/{resource_id}/proxy', | ||
controller='ckanext.resourceproxy.controller:ProxyController', | ||
action='proxy_resource') | ||
return m | ||
|
||
def get_actions(self): | ||
return {'proxy_resource': controller.proxy_resource} |
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