##// END OF EJS Templates
Added ability to add links to specific domains and show the domain logo as an image substitute
neko259 -
r1695:6ee0d7fe default
parent child Browse files
Show More
1 NO CONTENT: new file 100644, binary diff hidden
@@ -1,170 +1,182 b''
1 1 import os
2 2
3 3 from django.core.files.images import get_image_dimensions
4 4 from django.template.defaultfilters import filesizeformat
5 5 from django.contrib.staticfiles.templatetags.staticfiles import static
6 6 from django.contrib.staticfiles import finders
7 7
8 8 FILE_STUB_IMAGE = 'images/file.png'
9 9 FILE_STUB_URL = 'url'
10 10
11 11 FILE_TYPES_VIDEO = (
12 12 'webm',
13 13 'mp4',
14 14 'mpeg',
15 15 'ogv',
16 16 )
17 17 FILE_TYPE_SVG = 'svg'
18 18 FILE_TYPES_AUDIO = (
19 19 'ogg',
20 20 'mp3',
21 21 'opus',
22 22 )
23 23 FILE_TYPES_IMAGE = (
24 24 'jpeg',
25 25 'jpg',
26 26 'png',
27 27 'bmp',
28 28 'gif',
29 29 )
30 30
31 31 PLAIN_FILE_FORMATS = {
32 32 'pdf': 'pdf',
33 33 'djvu': 'djvu',
34 34 'txt': 'txt',
35 35 'tex': 'tex',
36 36 'xcf': 'xcf',
37 37 }
38 38
39 39 URL_PROTOCOLS = {
40 40 'magnet': 'magnet',
41 41 }
42 42
43 URL_DOMAINS = {
44 'meduza.io': 'meduza',
45 }
46
43 47 CSS_CLASS_IMAGE = 'image'
44 48 CSS_CLASS_THUMB = 'thumb'
45 49
46 50
47 51 def get_viewers():
48 52 return AbstractViewer.__subclasses__()
49 53
50 54
51 55 def get_static_dimensions(filename):
52 56 file_path = finders.find(filename)
53 57 return get_image_dimensions(file_path)
54 58
55 59
56 60 class AbstractViewer:
57 61 def __init__(self, file, file_type, hash, url):
58 62 self.file = file
59 63 self.file_type = file_type
60 64 self.hash = hash
61 65 self.url = url
62 66
63 67 @staticmethod
64 68 def supports(file_type):
65 69 return True
66 70
67 71 def get_view(self):
68 72 return '<div class="image">'\
69 73 '{}'\
70 74 '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\
71 75 '</div>'.format(self.get_format_view(), self.file.url,
72 76 self.file_type, filesizeformat(self.file.size))
73 77
74 78 def get_format_view(self):
75 79 if self.file_type in PLAIN_FILE_FORMATS:
76 80 image = 'images/fileformats/{}.png'.format(
77 81 PLAIN_FILE_FORMATS[self.file_type])
78 82 else:
79 83 image = FILE_STUB_IMAGE
80 84
81 85 w, h = get_static_dimensions(image)
82 86
83 87 return '<a href="{}">'\
84 88 '<img class="url-image" src="{}" width="{}" height="{}"/>'\
85 89 '</a>'.format(self.file.url, static(image), w, h)
86 90
87 91
88 92 class VideoViewer(AbstractViewer):
89 93 @staticmethod
90 94 def supports(file_type):
91 95 return file_type in FILE_TYPES_VIDEO
92 96
93 97 def get_format_view(self):
94 98 return '<video width="200" height="150" controls src="{}"></video>'\
95 99 .format(self.file.url)
96 100
97 101
98 102 class AudioViewer(AbstractViewer):
99 103 @staticmethod
100 104 def supports(file_type):
101 105 return file_type in FILE_TYPES_AUDIO
102 106
103 107 def get_format_view(self):
104 108 return '<audio controls src="{}"></audio>'.format(self.file.url)
105 109
106 110
107 111 class SvgViewer(AbstractViewer):
108 112 @staticmethod
109 113 def supports(file_type):
110 114 return file_type == FILE_TYPE_SVG
111 115
112 116 def get_format_view(self):
113 117 return '<a class="thumb" href="{}">'\
114 118 '<img class="post-image-preview" width="200" height="150" src="{}" />'\
115 119 '</a>'.format(self.file.url, self.file.url)
116 120
117 121
118 122 class ImageViewer(AbstractViewer):
119 123 @staticmethod
120 124 def supports(file_type):
121 125 return file_type in FILE_TYPES_IMAGE
122 126
123 127 def get_format_view(self):
124 128 metadata = '{}, {}'.format(self.file.name.split('.')[-1],
125 129 filesizeformat(self.file.size))
126 130 width, height = get_image_dimensions(self.file.file)
127 131 preview_path = self.file.path.replace('.', '.200x150.')
128 132 pre_width, pre_height = get_image_dimensions(preview_path)
129 133
130 134 split = self.file.url.rsplit('.', 1)
131 135 w, h = 200, 150
132 136 thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1])
133 137
134 138 return '<a class="{}" href="{full}">' \
135 139 '<img class="post-image-preview"' \
136 140 ' src="{}"' \
137 141 ' alt="{}"' \
138 142 ' width="{}"' \
139 143 ' height="{}"' \
140 144 ' data-width="{}"' \
141 145 ' data-height="{}" />' \
142 146 '</a>' \
143 147 .format(CSS_CLASS_THUMB,
144 148 thumb_url,
145 149 self.hash,
146 150 str(pre_width),
147 151 str(pre_height), str(width), str(height),
148 152 full=self.file.url, image_meta=metadata)
149 153
150 154
151 155 class UrlViewer(AbstractViewer):
152 156 @staticmethod
153 157 def supports(file_type):
154 158 return file_type is None
155 159
156 160 def get_view(self):
157 161 return '<div class="image">' \
158 162 '{}' \
159 163 '</div>'.format(self.get_format_view())
160 164
161 165 def get_format_view(self):
162 166 protocol = self.url.split('://')[0]
163 url_image_name = URL_PROTOCOLS.get(protocol, FILE_STUB_URL)
167 domain = self.url.split('/')[2]
168
169 if protocol in URL_PROTOCOLS:
170 url_image_name = URL_PROTOCOLS.get(protocol)
171 elif domain in URL_DOMAINS:
172 url_image_name = URL_DOMAINS.get(domain)
173 else:
174 url_image_name = FILE_STUB_URL
175
164 176 image = static('images/' + url_image_name + '.png')
165 177
166 178 w, h = get_static_dimensions('images/' + url_image_name + '.png')
167 179
168 180 return '<a href="{}">' \
169 181 '<img class="url-image" src="{}" width="{}" height="{}"/>' \
170 182 '</a>'.format(self.url, image, w, h)
General Comments 0
You need to be logged in to leave comments. Login now