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