Skip to content

Commit

Permalink
[#4850] Preview for multimedia files
Browse files Browse the repository at this point in the history
Currently, there is no preview for multimedia files,
such as video or audio files.

This changes implements the audio_preview, with simple
HTML5 <audio> tag
  • Loading branch information
calexandr committed Nov 27, 2019
1 parent ef103f0 commit 557b7e0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
Empty file added ckanext/audioview/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions ckanext/audioview/plugin.py
@@ -0,0 +1,41 @@
# encoding: utf-8

from six import text_type
import ckan.plugins as p

ignore_empty = p.toolkit.get_validator('ignore_empty')

DEFAULT_AUDIO_FORMATS = 'wav ogg mp3'


class AudioView(p.SingletonPlugin):
'''This plugin makes views of audio resources, using an <audio> tag'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)

def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
self.formats = config.get(
'ckan.preview.audio_formats',
DEFAULT_AUDIO_FORMATS).split()

def info(self):
return {'name': 'audio_view',
'title': p.toolkit._('Audio'),
'icon': 'file-audio-o',
'schema': {'audio_url': [ignore_empty, text_type]},
'iframed': False,
'always_available': True,
'default_title': p.toolkit._('Audio'),
}

def can_view(self, data_dict):
return (data_dict['resource'].get('format', '').lower()
in self.formats)

def view_template(self, context, data_dict):
return 'audio_view.html'

def form_template(self, context, data_dict):
return 'audio_form.html'
3 changes: 3 additions & 0 deletions ckanext/audioview/theme/templates/audio_form.html
@@ -0,0 +1,3 @@
{% import 'macros/form.html' as form %}

{{ form.input('audio_url', id='field-audio_url', label=_('Audio url'), placeholder=_('eg. http://example.com/audio.mp3 (if blank uses resource url)'), value=data.audio_url, error=errors.audio_url, classes=['control-full', 'control-large']) }}
9 changes: 9 additions & 0 deletions ckanext/audioview/theme/templates/audio_view.html
@@ -0,0 +1,9 @@
{% set res_url = resource_view.get('audio_url') or resource.get('url') %}

<audio style="margin:auto; max-height:100%; display:block; min-width: 100%;"
controls src="{{ resource_view.audio_url or resource.get('url') }}">
{% trans %}
Your browser does not support the <code>audio</code> element.
But don't worry, you can <a href="{{ res_url }}" alt="video url">download it</a>.
{% endtrans %}
</audio>

0 comments on commit 557b7e0

Please sign in to comment.