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