|
|
import re
|
|
|
|
|
|
from django.contrib.staticfiles import finders
|
|
|
from django.contrib.staticfiles.templatetags.staticfiles import static
|
|
|
from django.core.files.images import get_image_dimensions
|
|
|
from django.template.defaultfilters import filesizeformat
|
|
|
from django.core.urlresolvers import reverse
|
|
|
from django.utils.translation import ugettext_lazy as _, ungettext_lazy
|
|
|
|
|
|
from boards.utils import get_domain, cached_result
|
|
|
from boards import settings
|
|
|
|
|
|
|
|
|
FILE_STUB_IMAGE = 'images/file.png'
|
|
|
FILE_STUB_URL = 'url'
|
|
|
FILE_FILEFORMAT = 'images/fileformats/{}.png'
|
|
|
|
|
|
|
|
|
FILE_TYPES_VIDEO = (
|
|
|
'webm',
|
|
|
'mp4',
|
|
|
'mpeg',
|
|
|
'ogv',
|
|
|
)
|
|
|
FILE_TYPE_SVG = 'svg'
|
|
|
FILE_TYPES_AUDIO = (
|
|
|
'ogg',
|
|
|
'mp3',
|
|
|
'opus',
|
|
|
)
|
|
|
FILE_TYPES_IMAGE = (
|
|
|
'jpeg',
|
|
|
'jpg',
|
|
|
'png',
|
|
|
'bmp',
|
|
|
'gif',
|
|
|
)
|
|
|
|
|
|
PLAIN_FILE_FORMATS = {
|
|
|
'zip': 'archive',
|
|
|
'tar': 'archive',
|
|
|
'gz': 'archive',
|
|
|
'mid' : 'midi',
|
|
|
}
|
|
|
|
|
|
URL_PROTOCOLS = {
|
|
|
'magnet': 'magnet',
|
|
|
}
|
|
|
|
|
|
CSS_CLASS_IMAGE = 'image'
|
|
|
CSS_CLASS_THUMB = 'thumb'
|
|
|
|
|
|
|
|
|
def get_viewers():
|
|
|
return AbstractViewer.__subclasses__()
|
|
|
|
|
|
|
|
|
def get_static_dimensions(filename):
|
|
|
file_path = finders.find(filename)
|
|
|
return get_image_dimensions(file_path)
|
|
|
|
|
|
|
|
|
# TODO Move this to utils
|
|
|
def file_exists(filename):
|
|
|
return finders.find(filename) is not None
|
|
|
|
|
|
|
|
|
class AbstractViewer:
|
|
|
def __init__(self, file, file_type, hash, url):
|
|
|
self.file = file
|
|
|
self.file_type = file_type
|
|
|
self.hash = hash
|
|
|
self.url = url
|
|
|
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return True
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image" id="file{}">'\
|
|
|
'{}'\
|
|
|
'<div class="image-metadata"><a href="{}" download >{}, {}</a>'\
|
|
|
' <a class="file-menu" href="#">🔍 </a></div>'\
|
|
|
'</div>'.format(self.hash, self.get_format_view(), self.file.url,
|
|
|
self.file_type, filesizeformat(self.file.size))\
|
|
|
+ self._build_context_menu(self.get_context_menu())
|
|
|
|
|
|
def get_format_view(self):
|
|
|
image_name = PLAIN_FILE_FORMATS.get(self.file_type, self.file_type)
|
|
|
file_name = FILE_FILEFORMAT.format(image_name)
|
|
|
|
|
|
if file_exists(file_name):
|
|
|
image = file_name
|
|
|
else:
|
|
|
image = FILE_STUB_IMAGE
|
|
|
|
|
|
w, h = get_static_dimensions(image)
|
|
|
|
|
|
return '<a href="{}">'\
|
|
|
'<img class="url-image" src="{}" width="{}" height="{}"/>'\
|
|
|
'</a>'.format(self.file.url, static(image), w, h)
|
|
|
|
|
|
def get_context_menu(self):
|
|
|
items = []
|
|
|
items.append({
|
|
|
'name': 'duplicates',
|
|
|
'label': _('Duplicates search'),
|
|
|
'url': reverse('feed') + '?image_hash=' + self.hash,
|
|
|
})
|
|
|
|
|
|
return items
|
|
|
|
|
|
def _build_context_menu(self, items):
|
|
|
if not items:
|
|
|
return ''
|
|
|
|
|
|
cm_items = []
|
|
|
for item in items:
|
|
|
cm_items.append(('{}: {{name: "{}", callback: function(key, opt) {{window.location="{}";}}}}')\
|
|
|
.format(item['name'], item['label'], item['url']))
|
|
|
return '<script>$.contextMenu({{trigger: "left", selector: "#file{} .file-menu", items: {{ {} }} }});</script>'.format(self.hash, ', '.join(cm_items))
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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="{}"' \
|
|
|
' alt="{}"' \
|
|
|
' width="{}"' \
|
|
|
' height="{}"' \
|
|
|
' data-width="{}"' \
|
|
|
' data-height="{}" />' \
|
|
|
'</a>' \
|
|
|
.format(CSS_CLASS_THUMB,
|
|
|
thumb_url,
|
|
|
self.hash,
|
|
|
str(pre_width),
|
|
|
str(pre_height), str(width), str(height),
|
|
|
full=self.file.url, image_meta=metadata)
|
|
|
|
|
|
def get_context_menu(self):
|
|
|
items = super().get_context_menu()
|
|
|
|
|
|
image_url = settings.get('External', 'ImageSearchHost') + self.file.url
|
|
|
|
|
|
items.append({
|
|
|
'name': 'google',
|
|
|
'label': 'Google',
|
|
|
'url': 'https://www.google.com/searchbyimage?image_url={}'.format(image_url),
|
|
|
})
|
|
|
items.append({
|
|
|
'name': 'iqdb',
|
|
|
'label': 'iqdb',
|
|
|
'url': 'http://iqdb.org/?url={}'.format(image_url)
|
|
|
})
|
|
|
items.append({
|
|
|
'name': 'tineye',
|
|
|
'label': 'TinEye',
|
|
|
'url': 'http://tineye.com/search?url={}'.format(image_url)
|
|
|
})
|
|
|
|
|
|
return items
|
|
|
|
|
|
|
|
|
class UrlViewer(AbstractViewer):
|
|
|
@staticmethod
|
|
|
def supports(file_type):
|
|
|
return file_type is None
|
|
|
|
|
|
def get_view(self):
|
|
|
return '<div class="image">' \
|
|
|
'{}' \
|
|
|
'<div class="image-metadata">{}</div>' \
|
|
|
'</div>'.format(self.get_format_view(), get_domain(self.url))
|
|
|
|
|
|
def get_format_view(self):
|
|
|
protocol = self.url.split(':')[0]
|
|
|
|
|
|
domain = get_domain(self.url)
|
|
|
|
|
|
if protocol in URL_PROTOCOLS:
|
|
|
url_image_name = URL_PROTOCOLS.get(protocol)
|
|
|
elif domain:
|
|
|
url_image_name = self._find_image_for_domains(domain) or FILE_STUB_URL
|
|
|
else:
|
|
|
url_image_name = FILE_STUB_URL
|
|
|
|
|
|
image_path = 'images/{}.png'.format(url_image_name)
|
|
|
image = static(image_path)
|
|
|
w, h = get_static_dimensions(image_path)
|
|
|
|
|
|
return '<a href="{}">' \
|
|
|
'<img class="url-image" src="{}" width="{}" height="{}"/>' \
|
|
|
'</a>'.format(self.url, image, w, h)
|
|
|
|
|
|
def get_context_menu(self):
|
|
|
return []
|
|
|
|
|
|
@cached_result()
|
|
|
def _find_image_for_domains(self, domain):
|
|
|
"""
|
|
|
Searches for the domain image for every domain level except top.
|
|
|
E.g. for l3.example.co.uk it will search for l3.example.co.uk, then
|
|
|
example.co.uk, then co.uk
|
|
|
"""
|
|
|
levels = domain.split('.')
|
|
|
while len(levels) > 1:
|
|
|
domain = '.'.join(levels)
|
|
|
|
|
|
filename = 'images/domains/{}.png'.format(domain)
|
|
|
if file_exists(filename):
|
|
|
return 'domains/' + domain
|
|
|
else:
|
|
|
del levels[0]
|
|
|
|
|
|
|