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