|
@@
-1,147
+1,149
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 = 'images/url.png'
|
|
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',
|
|
|
|
|
33
|
'xcf': 'xcf',
|
|
32
|
}
|
|
34
|
}
|
|
33
|
|
|
35
|
|
|
34
|
CSS_CLASS_IMAGE = 'image'
|
|
36
|
CSS_CLASS_IMAGE = 'image'
|
|
35
|
CSS_CLASS_THUMB = 'thumb'
|
|
37
|
CSS_CLASS_THUMB = 'thumb'
|
|
36
|
|
|
38
|
|
|
37
|
|
|
39
|
|
|
38
|
def get_viewers():
|
|
40
|
def get_viewers():
|
|
39
|
return AbstractViewer.__subclasses__()
|
|
41
|
return AbstractViewer.__subclasses__()
|
|
40
|
|
|
42
|
|
|
41
|
|
|
43
|
|
|
42
|
class AbstractViewer:
|
|
44
|
class AbstractViewer:
|
|
43
|
def __init__(self, file, file_type, hash, url):
|
|
45
|
def __init__(self, file, file_type, hash, url):
|
|
44
|
self.file = file
|
|
46
|
self.file = file
|
|
45
|
self.file_type = file_type
|
|
47
|
self.file_type = file_type
|
|
46
|
self.hash = hash
|
|
48
|
self.hash = hash
|
|
47
|
self.url = url
|
|
49
|
self.url = url
|
|
48
|
|
|
50
|
|
|
49
|
@staticmethod
|
|
51
|
@staticmethod
|
|
50
|
def supports(file_type):
|
|
52
|
def supports(file_type):
|
|
51
|
return True
|
|
53
|
return True
|
|
52
|
|
|
54
|
|
|
53
|
def get_view(self):
|
|
55
|
def get_view(self):
|
|
54
|
return '<div class="image">'\
|
|
56
|
return '<div class="image">'\
|
|
55
|
'{}'\
|
|
57
|
'{}'\
|
|
56
|
'<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\
|
|
58
|
'<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\
|
|
57
|
'</div>'.format(self.get_format_view(), self.file.url,
|
|
59
|
'</div>'.format(self.get_format_view(), self.file.url,
|
|
58
|
self.file_type, filesizeformat(self.file.size))
|
|
60
|
self.file_type, filesizeformat(self.file.size))
|
|
59
|
|
|
61
|
|
|
60
|
def get_format_view(self):
|
|
62
|
def get_format_view(self):
|
|
61
|
if self.file_type in PLAIN_FILE_FORMATS:
|
|
63
|
if self.file_type in PLAIN_FILE_FORMATS:
|
|
62
|
image = 'images/fileformats/{}.png'.format(
|
|
64
|
image = 'images/fileformats/{}.png'.format(
|
|
63
|
PLAIN_FILE_FORMATS[self.file_type])
|
|
65
|
PLAIN_FILE_FORMATS[self.file_type])
|
|
64
|
else:
|
|
66
|
else:
|
|
65
|
image = FILE_STUB_IMAGE
|
|
67
|
image = FILE_STUB_IMAGE
|
|
66
|
|
|
68
|
|
|
67
|
return '<a href="{}">'\
|
|
69
|
return '<a href="{}">'\
|
|
68
|
'<img src="{}" width="200" height="150"/>'\
|
|
70
|
'<img src="{}" width="200" height="150"/>'\
|
|
69
|
'</a>'.format(self.file.url, static(image))
|
|
71
|
'</a>'.format(self.file.url, static(image))
|
|
70
|
|
|
72
|
|
|
71
|
|
|
73
|
|
|
72
|
class VideoViewer(AbstractViewer):
|
|
74
|
class VideoViewer(AbstractViewer):
|
|
73
|
@staticmethod
|
|
75
|
@staticmethod
|
|
74
|
def supports(file_type):
|
|
76
|
def supports(file_type):
|
|
75
|
return file_type in FILE_TYPES_VIDEO
|
|
77
|
return file_type in FILE_TYPES_VIDEO
|
|
76
|
|
|
78
|
|
|
77
|
def get_format_view(self):
|
|
79
|
def get_format_view(self):
|
|
78
|
return '<video width="200" height="150" controls src="{}"></video>'\
|
|
80
|
return '<video width="200" height="150" controls src="{}"></video>'\
|
|
79
|
.format(self.file.url)
|
|
81
|
.format(self.file.url)
|
|
80
|
|
|
82
|
|
|
81
|
|
|
83
|
|
|
82
|
class AudioViewer(AbstractViewer):
|
|
84
|
class AudioViewer(AbstractViewer):
|
|
83
|
@staticmethod
|
|
85
|
@staticmethod
|
|
84
|
def supports(file_type):
|
|
86
|
def supports(file_type):
|
|
85
|
return file_type in FILE_TYPES_AUDIO
|
|
87
|
return file_type in FILE_TYPES_AUDIO
|
|
86
|
|
|
88
|
|
|
87
|
def get_format_view(self):
|
|
89
|
def get_format_view(self):
|
|
88
|
return '<audio controls src="{}"></audio>'.format(self.file.url)
|
|
90
|
return '<audio controls src="{}"></audio>'.format(self.file.url)
|
|
89
|
|
|
91
|
|
|
90
|
|
|
92
|
|
|
91
|
class SvgViewer(AbstractViewer):
|
|
93
|
class SvgViewer(AbstractViewer):
|
|
92
|
@staticmethod
|
|
94
|
@staticmethod
|
|
93
|
def supports(file_type):
|
|
95
|
def supports(file_type):
|
|
94
|
return file_type == FILE_TYPE_SVG
|
|
96
|
return file_type == FILE_TYPE_SVG
|
|
95
|
|
|
97
|
|
|
96
|
def get_format_view(self):
|
|
98
|
def get_format_view(self):
|
|
97
|
return '<a class="thumb" href="{}">'\
|
|
99
|
return '<a class="thumb" href="{}">'\
|
|
98
|
'<img class="post-image-preview" width="200" height="150" src="{}" />'\
|
|
100
|
'<img class="post-image-preview" width="200" height="150" src="{}" />'\
|
|
99
|
'</a>'.format(self.file.url, self.file.url)
|
|
101
|
'</a>'.format(self.file.url, self.file.url)
|
|
100
|
|
|
102
|
|
|
101
|
|
|
103
|
|
|
102
|
class ImageViewer(AbstractViewer):
|
|
104
|
class ImageViewer(AbstractViewer):
|
|
103
|
@staticmethod
|
|
105
|
@staticmethod
|
|
104
|
def supports(file_type):
|
|
106
|
def supports(file_type):
|
|
105
|
return file_type in FILE_TYPES_IMAGE
|
|
107
|
return file_type in FILE_TYPES_IMAGE
|
|
106
|
|
|
108
|
|
|
107
|
def get_format_view(self):
|
|
109
|
def get_format_view(self):
|
|
108
|
metadata = '{}, {}'.format(self.file.name.split('.')[-1],
|
|
110
|
metadata = '{}, {}'.format(self.file.name.split('.')[-1],
|
|
109
|
filesizeformat(self.file.size))
|
|
111
|
filesizeformat(self.file.size))
|
|
110
|
width, height = get_image_dimensions(self.file.file)
|
|
112
|
width, height = get_image_dimensions(self.file.file)
|
|
111
|
preview_path = self.file.path.replace('.', '.200x150.')
|
|
113
|
preview_path = self.file.path.replace('.', '.200x150.')
|
|
112
|
pre_width, pre_height = get_image_dimensions(preview_path)
|
|
114
|
pre_width, pre_height = get_image_dimensions(preview_path)
|
|
113
|
|
|
115
|
|
|
114
|
split = self.file.url.rsplit('.', 1)
|
|
116
|
split = self.file.url.rsplit('.', 1)
|
|
115
|
w, h = 200, 150
|
|
117
|
w, h = 200, 150
|
|
116
|
thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1])
|
|
118
|
thumb_url = '%s.%sx%s.%s' % (split[0], w, h, split[1])
|
|
117
|
|
|
119
|
|
|
118
|
return '<a class="{}" href="{full}">' \
|
|
120
|
return '<a class="{}" href="{full}">' \
|
|
119
|
'<img class="post-image-preview"' \
|
|
121
|
'<img class="post-image-preview"' \
|
|
120
|
' src="{}"' \
|
|
122
|
' src="{}"' \
|
|
121
|
' alt="{}"' \
|
|
123
|
' alt="{}"' \
|
|
122
|
' width="{}"' \
|
|
124
|
' width="{}"' \
|
|
123
|
' height="{}"' \
|
|
125
|
' height="{}"' \
|
|
124
|
' data-width="{}"' \
|
|
126
|
' data-width="{}"' \
|
|
125
|
' data-height="{}" />' \
|
|
127
|
' data-height="{}" />' \
|
|
126
|
'</a>' \
|
|
128
|
'</a>' \
|
|
127
|
.format(CSS_CLASS_THUMB,
|
|
129
|
.format(CSS_CLASS_THUMB,
|
|
128
|
thumb_url,
|
|
130
|
thumb_url,
|
|
129
|
self.hash,
|
|
131
|
self.hash,
|
|
130
|
str(pre_width),
|
|
132
|
str(pre_width),
|
|
131
|
str(pre_height), str(width), str(height),
|
|
133
|
str(pre_height), str(width), str(height),
|
|
132
|
full=self.file.url, image_meta=metadata)
|
|
134
|
full=self.file.url, image_meta=metadata)
|
|
133
|
|
|
135
|
|
|
134
|
|
|
136
|
|
|
135
|
class UrlViewer(AbstractViewer):
|
|
137
|
class UrlViewer(AbstractViewer):
|
|
136
|
@staticmethod
|
|
138
|
@staticmethod
|
|
137
|
def supports(file_type):
|
|
139
|
def supports(file_type):
|
|
138
|
return file_type is None
|
|
140
|
return file_type is None
|
|
139
|
|
|
141
|
|
|
140
|
def get_view(self):
|
|
142
|
def get_view(self):
|
|
141
|
return '<div class="image">' \
|
|
143
|
return '<div class="image">' \
|
|
142
|
'{}' \
|
|
144
|
'{}' \
|
|
143
|
'</div>'.format(self.get_format_view())
|
|
145
|
'</div>'.format(self.get_format_view())
|
|
144
|
def get_format_view(self):
|
|
146
|
def get_format_view(self):
|
|
145
|
return '<a href="{}">' \
|
|
147
|
return '<a href="{}">' \
|
|
146
|
'<img src="{}" width="200" height="150"/>' \
|
|
148
|
'<img src="{}" width="200" height="150"/>' \
|
|
147
|
'</a>'.format(self.url, static(FILE_STUB_URL))
|
|
149
|
'</a>'.format(self.url, static(FILE_STUB_URL))
|