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