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