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