Show More
@@ -1,220 +1,220 b'' | |||
|
1 | 1 | import re |
|
2 | 2 | |
|
3 | 3 | from django.contrib.staticfiles import finders |
|
4 | 4 | from django.contrib.staticfiles.templatetags.staticfiles import static |
|
5 | 5 | from django.core.files.images import get_image_dimensions |
|
6 | 6 | from django.template.defaultfilters import filesizeformat |
|
7 | 7 | from django.core.urlresolvers import reverse |
|
8 | 8 | from django.utils.translation import ugettext_lazy as _, ungettext_lazy |
|
9 | 9 | |
|
10 | 10 | from boards.utils import get_domain, cached_result |
|
11 | 11 | from boards import settings |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | FILE_STUB_IMAGE = 'images/file.png' |
|
15 | 15 | FILE_STUB_URL = 'url' |
|
16 | 16 | FILE_FILEFORMAT = 'images/fileformats/{}.png' |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | FILE_TYPES_VIDEO = ( |
|
20 | 20 | 'webm', |
|
21 | 21 | 'mp4', |
|
22 | 22 | 'mpeg', |
|
23 | 23 | 'ogv', |
|
24 | 24 | ) |
|
25 | 25 | FILE_TYPE_SVG = 'svg' |
|
26 | 26 | FILE_TYPES_AUDIO = ( |
|
27 | 27 | 'ogg', |
|
28 | 28 | 'mp3', |
|
29 | 29 | 'opus', |
|
30 | 30 | ) |
|
31 | 31 | FILE_TYPES_IMAGE = ( |
|
32 | 32 | 'jpeg', |
|
33 | 33 | 'jpg', |
|
34 | 34 | 'png', |
|
35 | 35 | 'bmp', |
|
36 | 36 | 'gif', |
|
37 | 37 | ) |
|
38 | 38 | |
|
39 | 39 | PLAIN_FILE_FORMATS = { |
|
40 | 40 | 'zip': 'archive', |
|
41 | 41 | 'tar': 'archive', |
|
42 | 42 | 'gz': 'archive', |
|
43 | 43 | 'mid' : 'midi', |
|
44 | 44 | } |
|
45 | 45 | |
|
46 | 46 | URL_PROTOCOLS = { |
|
47 | 47 | 'magnet': 'magnet', |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | CSS_CLASS_IMAGE = 'image' |
|
51 | 51 | CSS_CLASS_THUMB = 'thumb' |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def get_viewers(): |
|
55 | 55 | return AbstractViewer.__subclasses__() |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def get_static_dimensions(filename): |
|
59 | 59 | file_path = finders.find(filename) |
|
60 | 60 | return get_image_dimensions(file_path) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | # TODO Move this to utils |
|
64 | 64 | def file_exists(filename): |
|
65 | 65 | return finders.find(filename) is not None |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | class AbstractViewer: |
|
69 | 69 | def __init__(self, file, file_type, hash, url): |
|
70 | 70 | self.file = file |
|
71 | 71 | self.file_type = file_type |
|
72 | 72 | self.hash = hash |
|
73 | 73 | self.url = url |
|
74 | 74 | |
|
75 | 75 | @staticmethod |
|
76 | 76 | def supports(file_type): |
|
77 | 77 | return True |
|
78 | 78 | |
|
79 | 79 | def get_view(self): |
|
80 | 80 | search_host = settings.get('External', 'ImageSearchHost') |
|
81 | 81 | if search_host: |
|
82 | 82 | search_url = search_host + self.file.url |
|
83 | 83 | else: |
|
84 | 84 | search_url = '' |
|
85 | 85 | |
|
86 | 86 | return '<div class="image">'\ |
|
87 | 87 | '{}'\ |
|
88 | 88 | '<div class="image-metadata"><a href="{}" download >{}, {}</a>'\ |
|
89 | 89 | ' <a class="file-menu" href="#" data-type="{}" data-search-url="{}" data-hash="{}">π </a></div>'\ |
|
90 | 90 | '</div>'.format(self.get_format_view(), self.file.url, |
|
91 | 91 | self.file_type, filesizeformat(self.file.size), |
|
92 | 92 | self.file_type, search_url, self.hash) |
|
93 | 93 | |
|
94 | 94 | def get_format_view(self): |
|
95 | 95 | image_name = PLAIN_FILE_FORMATS.get(self.file_type, self.file_type) |
|
96 | 96 | file_name = FILE_FILEFORMAT.format(image_name) |
|
97 | 97 | |
|
98 | 98 | if file_exists(file_name): |
|
99 | 99 | image = file_name |
|
100 | 100 | else: |
|
101 | 101 | image = FILE_STUB_IMAGE |
|
102 | 102 | |
|
103 | 103 | w, h = get_static_dimensions(image) |
|
104 | 104 | |
|
105 | 105 | return '<a href="{}">'\ |
|
106 | 106 | '<img class="url-image" src="{}" width="{}" height="{}"/>'\ |
|
107 | 107 | '</a>'.format(self.file.url, static(image), w, h) |
|
108 | 108 | |
|
109 | 109 | |
|
110 | 110 | class VideoViewer(AbstractViewer): |
|
111 | 111 | @staticmethod |
|
112 | 112 | def supports(file_type): |
|
113 | 113 | return file_type in FILE_TYPES_VIDEO |
|
114 | 114 | |
|
115 | 115 | def get_format_view(self): |
|
116 | 116 | return '<video width="200" height="150" controls src="{}"></video>'\ |
|
117 | 117 | .format(self.file.url) |
|
118 | 118 | |
|
119 | 119 | |
|
120 | 120 | class AudioViewer(AbstractViewer): |
|
121 | 121 | @staticmethod |
|
122 | 122 | def supports(file_type): |
|
123 | 123 | return file_type in FILE_TYPES_AUDIO |
|
124 | 124 | |
|
125 | 125 | def get_format_view(self): |
|
126 | 126 | return '<audio controls src="{}"></audio>'.format(self.file.url) |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | class SvgViewer(AbstractViewer): |
|
130 | 130 | @staticmethod |
|
131 | 131 | def supports(file_type): |
|
132 | 132 | return file_type == FILE_TYPE_SVG |
|
133 | 133 | |
|
134 | 134 | def get_format_view(self): |
|
135 | 135 | return '<a class="thumb" href="{}">'\ |
|
136 | 136 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ |
|
137 | 137 | '</a>'.format(self.file.url, self.file.url) |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | class ImageViewer(AbstractViewer): |
|
141 | 141 | @staticmethod |
|
142 | 142 | def supports(file_type): |
|
143 | 143 | return file_type in FILE_TYPES_IMAGE |
|
144 | 144 | |
|
145 | 145 | def get_format_view(self): |
|
146 | 146 | metadata = '{}, {}'.format(self.file.name.split('.')[-1], |
|
147 | 147 | filesizeformat(self.file.size)) |
|
148 |
width, height = get_image_dimensions(self.file. |
|
|
148 | width, height = get_image_dimensions(self.file.path) | |
|
149 | 149 | preview_path = self.file.path.replace('.', '.200x150.') |
|
150 | 150 | pre_width, pre_height = get_image_dimensions(preview_path) |
|
151 | 151 | |
|
152 | 152 | split = self.file.url.rsplit('.', 1) |
|
153 | 153 | w, h = 200, 150 |
|
154 | 154 | thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1]) |
|
155 | 155 | |
|
156 | 156 | return '<a class="{}" href="{full}">' \ |
|
157 | 157 | '<img class="post-image-preview"' \ |
|
158 | 158 | ' src="{}"' \ |
|
159 | 159 | ' alt="{}"' \ |
|
160 | 160 | ' width="{}"' \ |
|
161 | 161 | ' height="{}"' \ |
|
162 | 162 | ' data-width="{}"' \ |
|
163 | 163 | ' data-height="{}" />' \ |
|
164 | 164 | '</a>' \ |
|
165 | 165 | .format(CSS_CLASS_THUMB, |
|
166 | 166 | thumb_url, |
|
167 | 167 | self.hash, |
|
168 | 168 | str(pre_width), |
|
169 | 169 | str(pre_height), str(width), str(height), |
|
170 | 170 | full=self.file.url, image_meta=metadata) |
|
171 | 171 | |
|
172 | 172 | |
|
173 | 173 | class UrlViewer(AbstractViewer): |
|
174 | 174 | @staticmethod |
|
175 | 175 | def supports(file_type): |
|
176 | 176 | return file_type is None |
|
177 | 177 | |
|
178 | 178 | def get_view(self): |
|
179 | 179 | return '<div class="image">' \ |
|
180 | 180 | '{}' \ |
|
181 | 181 | '<div class="image-metadata">{}</div>' \ |
|
182 | 182 | '</div>'.format(self.get_format_view(), get_domain(self.url)) |
|
183 | 183 | |
|
184 | 184 | def get_format_view(self): |
|
185 | 185 | protocol = self.url.split(':')[0] |
|
186 | 186 | |
|
187 | 187 | domain = get_domain(self.url) |
|
188 | 188 | |
|
189 | 189 | if protocol in URL_PROTOCOLS: |
|
190 | 190 | url_image_name = URL_PROTOCOLS.get(protocol) |
|
191 | 191 | elif domain: |
|
192 | 192 | url_image_name = self._find_image_for_domains(domain) or FILE_STUB_URL |
|
193 | 193 | else: |
|
194 | 194 | url_image_name = FILE_STUB_URL |
|
195 | 195 | |
|
196 | 196 | image_path = 'images/{}.png'.format(url_image_name) |
|
197 | 197 | image = static(image_path) |
|
198 | 198 | w, h = get_static_dimensions(image_path) |
|
199 | 199 | |
|
200 | 200 | return '<a href="{}">' \ |
|
201 | 201 | '<img class="url-image" src="{}" width="{}" height="{}"/>' \ |
|
202 | 202 | '</a>'.format(self.url, image, w, h) |
|
203 | 203 | |
|
204 | 204 | @cached_result() |
|
205 | 205 | def _find_image_for_domains(self, domain): |
|
206 | 206 | """ |
|
207 | 207 | Searches for the domain image for every domain level except top. |
|
208 | 208 | E.g. for l3.example.co.uk it will search for l3.example.co.uk, then |
|
209 | 209 | example.co.uk, then co.uk |
|
210 | 210 | """ |
|
211 | 211 | levels = domain.split('.') |
|
212 | 212 | while len(levels) > 1: |
|
213 | 213 | domain = '.'.join(levels) |
|
214 | 214 | |
|
215 | 215 | filename = 'images/domains/{}.png'.format(domain) |
|
216 | 216 | if file_exists(filename): |
|
217 | 217 | return 'domains/' + domain |
|
218 | 218 | else: |
|
219 | 219 | del levels[0] |
|
220 | 220 |
General Comments 0
You need to be logged in to leave comments.
Login now