|
|
from django.template.defaultfilters import filesizeformat
|
|
|
from django.templatetags.static import static
|
|
|
|
|
|
FILE_STUB_IMAGE = 'images/file.png'
|
|
|
|
|
|
FILE_TYPE_WEBM = 'webm'
|
|
|
FILE_TYPE_SVG = 'svg'
|
|
|
FILE_TYPES_AUDIO = (
|
|
|
'ogg',
|
|
|
'mp3',
|
|
|
)
|
|
|
|
|
|
|
|
|
class AbstractViewer:
|
|
|
def __init__(self, file, file_type):
|
|
|
self.file = file
|
|
|
self.file_type = file_type
|
|
|
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return True
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image"><a href="{}">'\
|
|
|
'<img src="{}" width="200" height="150"/>'\
|
|
|
'</a>'\
|
|
|
'<div class="image-metadata">{}, {}</div>'\
|
|
|
'</div>'.format(self.file.url, static(FILE_STUB_IMAGE),
|
|
|
self.file_type, filesizeformat(self.file.size))
|
|
|
|
|
|
|
|
|
class WebmViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type == FILE_TYPE_WEBM
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image">'\
|
|
|
'<video width="200" height="150" controls src="{}"/>'\
|
|
|
'</div>'.format(self.file.url)
|
|
|
|
|
|
|
|
|
class AudioViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type in FILE_TYPES_AUDIO
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image">' \
|
|
|
'<audio controls src="{}"/>' \
|
|
|
'</div>'.format(self.file.url)
|
|
|
|
|
|
|
|
|
class SvgViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type == FILE_TYPE_SVG
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image">'\
|
|
|
'<a class="thumb" href="{}">'\
|
|
|
'<img class="post-image-preview" width="200" height="150" src="{}"/>'\
|
|
|
'</a>'\
|
|
|
'</div>'.format(self.file.url, self.file.url)
|
|
|
|