Skip to content

Commit

Permalink
[#1251] fix pdf view
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Nov 6, 2013
1 parent 294ed98 commit f2e49d5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ckan/templates/dataviewer/base.html
@@ -1,11 +1,11 @@
{% extends "base.html" %}

{% block subtitle %}{{ h.dataset_display_name(c.package) }} - {{h.resource_display_name(c.resource) }}{% endblock %}
{% block subtitle %}{{ h.dataset_display_name(package) }} - {{h.resource_display_name(resource) }}{% endblock %}

{# remove any scripts #}
{% block scripts %}
<script>
var preload_resource = {{ h.literal(c.resource_json) }};
var preload_resource = {{ h.literal(h.dump_json(resource)) }};
</script>
{% endblock %}

Expand Down
32 changes: 15 additions & 17 deletions ckanext/pdfpreview/plugin.py
@@ -1,6 +1,7 @@
import logging

import ckan.plugins as p
import ckan.lib.datapreview as datapreview

log = logging.getLogger(__name__)

Expand All @@ -14,11 +15,14 @@ class PdfPreview(p.SingletonPlugin):
'''This extension previews PDFs. '''
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IResourcePreview, inherit=True)
p.implements(p.IResourceView, inherit=True)

PDF = ['pdf', 'x-pdf', 'acrobat', 'vnd.pdf']
proxy_is_enabled = False

def info(self):
return {'name': 'pdf', 'title': 'Pdf'}

def update_config(self, config):
p.toolkit.add_public_directory(config, 'theme/public')
p.toolkit.add_template_directory(config, 'theme/templates')
Expand All @@ -28,23 +32,17 @@ def configure(self, config):
enabled = config.get('ckan.resource_proxy_enabled', False)
self.proxy_is_enabled = enabled

def can_preview(self, data_dict):
def can_view(self, data_dict):
resource = data_dict['resource']
format_lower = resource['format'].lower()

proxy_enabled = p.plugin_loaded('resource_proxy')
same_domain = datapreview.on_same_domain(data_dict)

if format_lower in self.PDF:
if resource['on_same_domain'] or self.proxy_is_enabled:
return {'can_preview': True, 'quality': 2}
else:
return {'can_preview': False,
'fixable': 'Enable resource_proxy',
'quality': 2}
return {'can_preview': False}

def setup_template_variables(self, context, data_dict):
if (self.proxy_is_enabled
and not data_dict['resource']['on_same_domain']):
url = proxy.get_proxified_resource_url(data_dict)
p.toolkit.c.resource['url'] = url

def preview_template(self, context, data_dict):
if same_domain or proxy_enabled:
return True
return False

def view_template(self, context, data_dict):
return 'pdf.html'
2 changes: 1 addition & 1 deletion ckanext/pdfpreview/theme/public/preview_pdf.js
Expand Up @@ -6,7 +6,7 @@ ckan.module('pdfpreview', function () {
PDFJS.workerSrc = pdfjs_workerSrc;

var params = {
file: preload_resource['url']
file: resource_url
};

loadPdfJsView(params);
Expand Down
1 change: 1 addition & 0 deletions ckanext/pdfpreview/theme/templates/pdf.html
Expand Up @@ -171,6 +171,7 @@
{{ super() }}

<script>
var resource_url = "{{ h.view_resource_url(resource_view, resource, package) }}";
var pdfjs_workerSrc = "{{ h.urls_for_resource('ckanext-pdfpreview/vendor/pdfjs/pdf.js')[-1] }}";
</script>
{% resource 'ckanext-pdfpreview/vendor/pdfjs/pdf.js' %}
Expand Down
5 changes: 4 additions & 1 deletion ckanext/textpreview/plugin.py
Expand Up @@ -51,8 +51,11 @@ def can_view(self, data_dict):
format_lower = resource['format'].lower()
proxy_enabled = p.plugin_loaded('resource_proxy')
same_domain = datapreview.on_same_domain(data_dict)
if format_lower in self.jsonp_formats or proxy_enabled or same_domain:
if format_lower in self.jsonp_formats:
return True
if format_lower in self.no_jsonp_formats:
if proxy_enabled or same_domain:
return True
return False

def setup_template_variables(self, context, data_dict):
Expand Down

0 comments on commit f2e49d5

Please sign in to comment.