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