|
|
from django.template.defaultfilters import filesizeformat
|
|
|
from django.contrib.staticfiles.templatetags.staticfiles import static
|
|
|
|
|
|
FILE_STUB_IMAGE = 'images/file.png'
|
|
|
|
|
|
FILE_TYPES_VIDEO = (
|
|
|
'webm',
|
|
|
'mp4',
|
|
|
'mpeg',
|
|
|
)
|
|
|
FILE_TYPE_SVG = 'svg'
|
|
|
FILE_TYPES_AUDIO = (
|
|
|
'ogg',
|
|
|
'mp3',
|
|
|
'opus',
|
|
|
)
|
|
|
|
|
|
PLAIN_FILE_FORMATS = {
|
|
|
'pdf': 'pdf',
|
|
|
'djvu': 'djvu',
|
|
|
'txt': 'txt',
|
|
|
}
|
|
|
|
|
|
|
|
|
def get_viewers():
|
|
|
return AbstractViewer.__subclasses__()
|
|
|
|
|
|
|
|
|
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">'\
|
|
|
'{}'\
|
|
|
'<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\
|
|
|
'</div>'.format(self.get_format_view(), self.file.url,
|
|
|
self.file_type, filesizeformat(self.file.size))
|
|
|
|
|
|
def get_format_view(self):
|
|
|
if self.file_type in PLAIN_FILE_FORMATS:
|
|
|
image = 'images/fileformats/{}.png'.format(
|
|
|
PLAIN_FILE_FORMATS[self.file_type])
|
|
|
else:
|
|
|
image = FILE_STUB_IMAGE
|
|
|
|
|
|
return '<a href="{}">'\
|
|
|
'<img src="{}" width="200" height="150"/>'\
|
|
|
'</a>'.format(self.file.url, static(image))
|
|
|
|
|
|
|
|
|
class VideoViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type in FILE_TYPES_VIDEO
|
|
|
|
|
|
def get_format_view(self):
|
|
|
return '<video width="200" height="150" controls src="{}"></video>'\
|
|
|
.format(self.file.url)
|
|
|
|
|
|
|
|
|
class AudioViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type in FILE_TYPES_AUDIO
|
|
|
|
|
|
def get_format_view(self):
|
|
|
return '<audio controls src="{}"></audio>'.format(self.file.url)
|
|
|
|
|
|
|
|
|
class SvgViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type == FILE_TYPE_SVG
|
|
|
|
|
|
def get_format_view(self):
|
|
|
return '<a class="thumb" href="{}">'\
|
|
|
'<img class="post-image-preview" width="200" height="150" src="{}" />'\
|
|
|
'</a>'.format(self.file.url, self.file.url)
|
|
|
|