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