viewers.py
186 lines
| 5.1 KiB
| text/x-python
|
PythonLexer
|
r1715 | import re | ||
|
r1684 | |||
|
r1716 | from django.contrib.staticfiles import finders | ||
from django.contrib.staticfiles.templatetags.staticfiles import static | ||||
|
r1590 | from django.core.files.images import get_image_dimensions | ||
|
r1273 | from django.template.defaultfilters import filesizeformat | ||
|
r1716 | |||
from boards.models.attachment.domains import URL_DOMAINS | ||||
|
r1273 | |||
|
r1715 | REGEX_DOMAIN = re.compile(r'(\w+\.)*(\w+\.\w+)') | ||
|
r1279 | FILE_STUB_IMAGE = 'images/file.png' | ||
|
r1677 | FILE_STUB_URL = 'url' | ||
|
r1279 | |||
|
r1307 | FILE_TYPES_VIDEO = ( | ||
'webm', | ||||
'mp4', | ||||
|
r1342 | 'mpeg', | ||
|
r1493 | 'ogv', | ||
|
r1307 | ) | ||
|
r1284 | FILE_TYPE_SVG = 'svg' | ||
|
r1279 | FILE_TYPES_AUDIO = ( | ||
'ogg', | ||||
'mp3', | ||||
|
r1339 | 'opus', | ||
|
r1279 | ) | ||
|
r1590 | FILE_TYPES_IMAGE = ( | ||
'jpeg', | ||||
'jpg', | ||||
'png', | ||||
'bmp', | ||||
'gif', | ||||
) | ||||
|
r1279 | |||
|
r1326 | PLAIN_FILE_FORMATS = { | ||
'pdf': 'pdf', | ||||
'djvu': 'djvu', | ||||
'txt': 'txt', | ||||
|
r1665 | 'tex': 'tex', | ||
'xcf': 'xcf', | ||||
|
r1713 | 'zip': 'archive', | ||
'tar': 'archive', | ||||
'gz': 'archive', | ||||
|
r1326 | } | ||
|
r1677 | URL_PROTOCOLS = { | ||
'magnet': 'magnet', | ||||
} | ||||
|
r1590 | CSS_CLASS_IMAGE = 'image' | ||
CSS_CLASS_THUMB = 'thumb' | ||||
|
r1273 | |||
|
r1286 | def get_viewers(): | ||
return AbstractViewer.__subclasses__() | ||||
|
r1684 | def get_static_dimensions(filename): | ||
file_path = finders.find(filename) | ||||
return get_image_dimensions(file_path) | ||||
|
r1273 | class AbstractViewer: | ||
|
r1660 | def __init__(self, file, file_type, hash, url): | ||
|
r1273 | self.file = file | ||
self.file_type = file_type | ||||
|
r1596 | self.hash = hash | ||
|
r1660 | self.url = url | ||
|
r1273 | |||
@staticmethod | ||||
def supports(file_type): | ||||
|
r1279 | return True | ||
|
r1273 | |||
def get_view(self): | ||||
|
r1300 | return '<div class="image">'\ | ||
'{}'\ | ||||
|
r1308 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ | ||
'</div>'.format(self.get_format_view(), self.file.url, | ||||
|
r1300 | self.file_type, filesizeformat(self.file.size)) | ||
def get_format_view(self): | ||||
|
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 | ||||
|
r1684 | w, h = get_static_dimensions(image) | ||
|
r1300 | return '<a href="{}">'\ | ||
|
r1684 | '<img class="url-image" src="{}" width="{}" height="{}"/>'\ | ||
'</a>'.format(self.file.url, static(image), w, h) | ||||
|
r1273 | |||
|
r1307 | class VideoViewer(AbstractViewer): | ||
|
r1273 | @staticmethod | ||
def supports(file_type): | ||||
|
r1307 | return file_type in FILE_TYPES_VIDEO | ||
|
r1273 | |||
|
r1300 | def get_format_view(self): | ||
return '<video width="200" height="150" controls src="{}"></video>'\ | ||||
.format(self.file.url) | ||||
|
r1273 | |||
|
r1279 | |||
class AudioViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type in FILE_TYPES_AUDIO | ||||
|
r1300 | def get_format_view(self): | ||
return '<audio controls src="{}"></audio>'.format(self.file.url) | ||||
|
r1284 | |||
class SvgViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type == FILE_TYPE_SVG | ||||
|
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) | ||||
|
r1590 | |||
class ImageViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type in FILE_TYPES_IMAGE | ||||
def get_format_view(self): | ||||
metadata = '{}, {}'.format(self.file.name.split('.')[-1], | ||||
filesizeformat(self.file.size)) | ||||
width, height = get_image_dimensions(self.file.file) | ||||
preview_path = self.file.path.replace('.', '.200x150.') | ||||
pre_width, pre_height = get_image_dimensions(preview_path) | ||||
split = self.file.url.rsplit('.', 1) | ||||
w, h = 200, 150 | ||||
thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | ||||
return '<a class="{}" href="{full}">' \ | ||||
'<img class="post-image-preview"' \ | ||||
' src="{}"' \ | ||||
|
r1596 | ' alt="{}"' \ | ||
|
r1590 | ' width="{}"' \ | ||
' height="{}"' \ | ||||
' data-width="{}"' \ | ||||
' data-height="{}" />' \ | ||||
'</a>' \ | ||||
.format(CSS_CLASS_THUMB, | ||||
thumb_url, | ||||
|
r1596 | self.hash, | ||
|
r1590 | str(pre_width), | ||
str(pre_height), str(width), str(height), | ||||
full=self.file.url, image_meta=metadata) | ||||
|
r1660 | |||
class UrlViewer(AbstractViewer): | ||||
@staticmethod | ||||
def supports(file_type): | ||||
return file_type is None | ||||
def get_view(self): | ||||
return '<div class="image">' \ | ||||
'{}' \ | ||||
'</div>'.format(self.get_format_view()) | ||||
|
r1677 | |||
|
r1660 | def get_format_view(self): | ||
|
r1677 | protocol = self.url.split('://')[0] | ||
|
r1715 | full_domain = self.url.split('/')[2] | ||
domain = REGEX_DOMAIN.search(full_domain).group(2) | ||||
|
r1695 | |||
if protocol in URL_PROTOCOLS: | ||||
url_image_name = URL_PROTOCOLS.get(protocol) | ||||
elif domain in URL_DOMAINS: | ||||
|
r1712 | url_image_name = 'domains/' + URL_DOMAINS.get(domain) | ||
|
r1695 | else: | ||
url_image_name = FILE_STUB_URL | ||||
|
r1677 | image = static('images/' + url_image_name + '.png') | ||
|
r1684 | w, h = get_static_dimensions('images/' + url_image_name + '.png') | ||
|
r1660 | return '<a href="{}">' \ | ||
|
r1684 | '<img class="url-image" src="{}" width="{}" height="{}"/>' \ | ||
'</a>'.format(self.url, image, w, h) | ||||