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