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