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