Show More
1 | NO CONTENT: new file 100644, binary diff hidden |
|
NO CONTENT: new file 100644, binary diff hidden |
@@ -1,170 +1,182 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 | } |
|
37 | } | |
38 |
|
38 | |||
39 | URL_PROTOCOLS = { |
|
39 | URL_PROTOCOLS = { | |
40 | 'magnet': 'magnet', |
|
40 | 'magnet': 'magnet', | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
|
43 | URL_DOMAINS = { | |||
|
44 | 'meduza.io': 'meduza', | |||
|
45 | } | |||
|
46 | ||||
43 | CSS_CLASS_IMAGE = 'image' |
|
47 | CSS_CLASS_IMAGE = 'image' | |
44 | CSS_CLASS_THUMB = 'thumb' |
|
48 | CSS_CLASS_THUMB = 'thumb' | |
45 |
|
49 | |||
46 |
|
50 | |||
47 | def get_viewers(): |
|
51 | def get_viewers(): | |
48 | return AbstractViewer.__subclasses__() |
|
52 | return AbstractViewer.__subclasses__() | |
49 |
|
53 | |||
50 |
|
54 | |||
51 | def get_static_dimensions(filename): |
|
55 | def get_static_dimensions(filename): | |
52 | file_path = finders.find(filename) |
|
56 | file_path = finders.find(filename) | |
53 | return get_image_dimensions(file_path) |
|
57 | return get_image_dimensions(file_path) | |
54 |
|
58 | |||
55 |
|
59 | |||
56 | class AbstractViewer: |
|
60 | class AbstractViewer: | |
57 | def __init__(self, file, file_type, hash, url): |
|
61 | def __init__(self, file, file_type, hash, url): | |
58 | self.file = file |
|
62 | self.file = file | |
59 | self.file_type = file_type |
|
63 | self.file_type = file_type | |
60 | self.hash = hash |
|
64 | self.hash = hash | |
61 | self.url = url |
|
65 | self.url = url | |
62 |
|
66 | |||
63 | @staticmethod |
|
67 | @staticmethod | |
64 | def supports(file_type): |
|
68 | def supports(file_type): | |
65 | return True |
|
69 | return True | |
66 |
|
70 | |||
67 | def get_view(self): |
|
71 | def get_view(self): | |
68 | return '<div class="image">'\ |
|
72 | return '<div class="image">'\ | |
69 | '{}'\ |
|
73 | '{}'\ | |
70 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ |
|
74 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ | |
71 | '</div>'.format(self.get_format_view(), self.file.url, |
|
75 | '</div>'.format(self.get_format_view(), self.file.url, | |
72 | self.file_type, filesizeformat(self.file.size)) |
|
76 | self.file_type, filesizeformat(self.file.size)) | |
73 |
|
77 | |||
74 | def get_format_view(self): |
|
78 | def get_format_view(self): | |
75 | if self.file_type in PLAIN_FILE_FORMATS: |
|
79 | if self.file_type in PLAIN_FILE_FORMATS: | |
76 | image = 'images/fileformats/{}.png'.format( |
|
80 | image = 'images/fileformats/{}.png'.format( | |
77 | PLAIN_FILE_FORMATS[self.file_type]) |
|
81 | PLAIN_FILE_FORMATS[self.file_type]) | |
78 | else: |
|
82 | else: | |
79 | image = FILE_STUB_IMAGE |
|
83 | image = FILE_STUB_IMAGE | |
80 |
|
84 | |||
81 | w, h = get_static_dimensions(image) |
|
85 | w, h = get_static_dimensions(image) | |
82 |
|
86 | |||
83 | return '<a href="{}">'\ |
|
87 | return '<a href="{}">'\ | |
84 | '<img class="url-image" src="{}" width="{}" height="{}"/>'\ |
|
88 | '<img class="url-image" src="{}" width="{}" height="{}"/>'\ | |
85 | '</a>'.format(self.file.url, static(image), w, h) |
|
89 | '</a>'.format(self.file.url, static(image), w, h) | |
86 |
|
90 | |||
87 |
|
91 | |||
88 | class VideoViewer(AbstractViewer): |
|
92 | class VideoViewer(AbstractViewer): | |
89 | @staticmethod |
|
93 | @staticmethod | |
90 | def supports(file_type): |
|
94 | def supports(file_type): | |
91 | return file_type in FILE_TYPES_VIDEO |
|
95 | return file_type in FILE_TYPES_VIDEO | |
92 |
|
96 | |||
93 | def get_format_view(self): |
|
97 | def get_format_view(self): | |
94 | return '<video width="200" height="150" controls src="{}"></video>'\ |
|
98 | return '<video width="200" height="150" controls src="{}"></video>'\ | |
95 | .format(self.file.url) |
|
99 | .format(self.file.url) | |
96 |
|
100 | |||
97 |
|
101 | |||
98 | class AudioViewer(AbstractViewer): |
|
102 | class AudioViewer(AbstractViewer): | |
99 | @staticmethod |
|
103 | @staticmethod | |
100 | def supports(file_type): |
|
104 | def supports(file_type): | |
101 | return file_type in FILE_TYPES_AUDIO |
|
105 | return file_type in FILE_TYPES_AUDIO | |
102 |
|
106 | |||
103 | def get_format_view(self): |
|
107 | def get_format_view(self): | |
104 | return '<audio controls src="{}"></audio>'.format(self.file.url) |
|
108 | return '<audio controls src="{}"></audio>'.format(self.file.url) | |
105 |
|
109 | |||
106 |
|
110 | |||
107 | class SvgViewer(AbstractViewer): |
|
111 | class SvgViewer(AbstractViewer): | |
108 | @staticmethod |
|
112 | @staticmethod | |
109 | def supports(file_type): |
|
113 | def supports(file_type): | |
110 | return file_type == FILE_TYPE_SVG |
|
114 | return file_type == FILE_TYPE_SVG | |
111 |
|
115 | |||
112 | def get_format_view(self): |
|
116 | def get_format_view(self): | |
113 | return '<a class="thumb" href="{}">'\ |
|
117 | return '<a class="thumb" href="{}">'\ | |
114 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ |
|
118 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ | |
115 | '</a>'.format(self.file.url, self.file.url) |
|
119 | '</a>'.format(self.file.url, self.file.url) | |
116 |
|
120 | |||
117 |
|
121 | |||
118 | class ImageViewer(AbstractViewer): |
|
122 | class ImageViewer(AbstractViewer): | |
119 | @staticmethod |
|
123 | @staticmethod | |
120 | def supports(file_type): |
|
124 | def supports(file_type): | |
121 | return file_type in FILE_TYPES_IMAGE |
|
125 | return file_type in FILE_TYPES_IMAGE | |
122 |
|
126 | |||
123 | def get_format_view(self): |
|
127 | def get_format_view(self): | |
124 | metadata = '{}, {}'.format(self.file.name.split('.')[-1], |
|
128 | metadata = '{}, {}'.format(self.file.name.split('.')[-1], | |
125 | filesizeformat(self.file.size)) |
|
129 | filesizeformat(self.file.size)) | |
126 | width, height = get_image_dimensions(self.file.file) |
|
130 | width, height = get_image_dimensions(self.file.file) | |
127 | preview_path = self.file.path.replace('.', '.200x150.') |
|
131 | preview_path = self.file.path.replace('.', '.200x150.') | |
128 | pre_width, pre_height = get_image_dimensions(preview_path) |
|
132 | pre_width, pre_height = get_image_dimensions(preview_path) | |
129 |
|
133 | |||
130 | split = self.file.url.rsplit('.', 1) |
|
134 | split = self.file.url.rsplit('.', 1) | |
131 | w, h = 200, 150 |
|
135 | w, h = 200, 150 | |
132 | thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1]) |
|
136 | thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1]) | |
133 |
|
137 | |||
134 | return '<a class="{}" href="{full}">' \ |
|
138 | return '<a class="{}" href="{full}">' \ | |
135 | '<img class="post-image-preview"' \ |
|
139 | '<img class="post-image-preview"' \ | |
136 | ' src="{}"' \ |
|
140 | ' src="{}"' \ | |
137 | ' alt="{}"' \ |
|
141 | ' alt="{}"' \ | |
138 | ' width="{}"' \ |
|
142 | ' width="{}"' \ | |
139 | ' height="{}"' \ |
|
143 | ' height="{}"' \ | |
140 | ' data-width="{}"' \ |
|
144 | ' data-width="{}"' \ | |
141 | ' data-height="{}" />' \ |
|
145 | ' data-height="{}" />' \ | |
142 | '</a>' \ |
|
146 | '</a>' \ | |
143 | .format(CSS_CLASS_THUMB, |
|
147 | .format(CSS_CLASS_THUMB, | |
144 | thumb_url, |
|
148 | thumb_url, | |
145 | self.hash, |
|
149 | self.hash, | |
146 | str(pre_width), |
|
150 | str(pre_width), | |
147 | str(pre_height), str(width), str(height), |
|
151 | str(pre_height), str(width), str(height), | |
148 | full=self.file.url, image_meta=metadata) |
|
152 | full=self.file.url, image_meta=metadata) | |
149 |
|
153 | |||
150 |
|
154 | |||
151 | class UrlViewer(AbstractViewer): |
|
155 | class UrlViewer(AbstractViewer): | |
152 | @staticmethod |
|
156 | @staticmethod | |
153 | def supports(file_type): |
|
157 | def supports(file_type): | |
154 | return file_type is None |
|
158 | return file_type is None | |
155 |
|
159 | |||
156 | def get_view(self): |
|
160 | def get_view(self): | |
157 | return '<div class="image">' \ |
|
161 | return '<div class="image">' \ | |
158 | '{}' \ |
|
162 | '{}' \ | |
159 | '</div>'.format(self.get_format_view()) |
|
163 | '</div>'.format(self.get_format_view()) | |
160 |
|
164 | |||
161 | def get_format_view(self): |
|
165 | def get_format_view(self): | |
162 | protocol = self.url.split('://')[0] |
|
166 | protocol = self.url.split('://')[0] | |
163 | url_image_name = URL_PROTOCOLS.get(protocol, FILE_STUB_URL) |
|
167 | domain = self.url.split('/')[2] | |
|
168 | ||||
|
169 | if protocol in URL_PROTOCOLS: | |||
|
170 | url_image_name = URL_PROTOCOLS.get(protocol) | |||
|
171 | elif domain in URL_DOMAINS: | |||
|
172 | url_image_name = URL_DOMAINS.get(domain) | |||
|
173 | else: | |||
|
174 | url_image_name = FILE_STUB_URL | |||
|
175 | ||||
164 | image = static('images/' + url_image_name + '.png') |
|
176 | image = static('images/' + url_image_name + '.png') | |
165 |
|
177 | |||
166 | w, h = get_static_dimensions('images/' + url_image_name + '.png') |
|
178 | w, h = get_static_dimensions('images/' + url_image_name + '.png') | |
167 |
|
179 | |||
168 | return '<a href="{}">' \ |
|
180 | return '<a href="{}">' \ | |
169 | '<img class="url-image" src="{}" width="{}" height="{}"/>' \ |
|
181 | '<img class="url-image" src="{}" width="{}" height="{}"/>' \ | |
170 | '</a>'.format(self.url, image, w, h) |
|
182 | '</a>'.format(self.url, image, w, h) |
General Comments 0
You need to be logged in to leave comments.
Login now