viewers.py
84 lines
| 2.2 KiB
| text/x-python
|
PythonLexer
neko259
|
r1273 | from django.template.defaultfilters import filesizeformat | ||
neko259
|
r1290 | from django.contrib.staticfiles.templatetags.staticfiles import static | ||
neko259
|
r1273 | |||
neko259
|
r1279 | FILE_STUB_IMAGE = 'images/file.png' | ||
neko259
|
r1307 | FILE_TYPES_VIDEO = ( | ||
'webm', | ||||
'mp4', | ||||
neko259
|
r1342 | 'mpeg', | ||
neko259
|
r1307 | ) | ||
neko259
|
r1284 | FILE_TYPE_SVG = 'svg' | ||
neko259
|
r1279 | FILE_TYPES_AUDIO = ( | ||
'ogg', | ||||
'mp3', | ||||
neko259
|
r1339 | 'opus', | ||
neko259
|
r1279 | ) | ||
neko259
|
r1326 | PLAIN_FILE_FORMATS = { | ||
'pdf': 'pdf', | ||||
'djvu': 'djvu', | ||||
'txt': 'txt', | ||||
} | ||||
neko259
|
r1273 | |||
neko259
|
r1286 | def get_viewers(): | ||
return AbstractViewer.__subclasses__() | ||||
neko259
|
r1273 | class AbstractViewer: | ||
def __init__(self, file, file_type): | ||||
self.file = file | ||||
self.file_type = file_type | ||||
@staticmethod | ||||
def supports(file_type): | ||||
neko259
|
r1279 | return True | ||
neko259
|
r1273 | |||
def get_view(self): | ||||
neko259
|
r1300 | return '<div class="image">'\ | ||
'{}'\ | ||||
neko259
|
r1308 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ | ||
'</div>'.format(self.get_format_view(), self.file.url, | ||||
neko259
|
r1300 | self.file_type, filesizeformat(self.file.size)) | ||
def get_format_view(self): | ||||
neko259
|
r1326 | if self.file_type in PLAIN_FILE_FORMATS: | ||
image = 'images/fileformats/{}.png'.format( | ||||
PLAIN_FILE_FORMATS[self.file_type]) | ||||
else: | ||||
image = FILE_STUB_IMAGE | ||||
neko259
|
r1300 | return '<a href="{}">'\ | ||
neko259
|
r1275 | '<img src="{}" width="200" height="150"/>'\ | ||
neko259
|
r1326 | '</a>'.format(self.file.url, static(image)) | ||
neko259
|
r1273 | |||
neko259
|
r1307 | class VideoViewer(AbstractViewer): | ||
neko259
|
r1273 | @staticmethod | ||
def supports(file_type): | ||||
neko259
|
r1307 | return file_type in FILE_TYPES_VIDEO | ||
neko259
|
r1273 | |||
neko259
|
r1300 | def get_format_view(self): | ||
return '<video width="200" height="150" controls src="{}"></video>'\ | ||||
.format(self.file.url) | ||||
neko259
|
r1273 | |||
neko259
|
r1279 | |||
class AudioViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type in FILE_TYPES_AUDIO | ||||
neko259
|
r1300 | def get_format_view(self): | ||
return '<audio controls src="{}"></audio>'.format(self.file.url) | ||||
neko259
|
r1284 | |||
class SvgViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type == FILE_TYPE_SVG | ||||
neko259
|
r1300 | 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) | ||||