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