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