Show More
@@ -1,83 +1,84 b'' | |||||
1 | from django.template.defaultfilters import filesizeformat |
|
1 | from django.template.defaultfilters import filesizeformat | |
2 | from django.contrib.staticfiles.templatetags.staticfiles import static |
|
2 | from django.contrib.staticfiles.templatetags.staticfiles import static | |
3 |
|
3 | |||
4 | FILE_STUB_IMAGE = 'images/file.png' |
|
4 | FILE_STUB_IMAGE = 'images/file.png' | |
5 |
|
5 | |||
6 | FILE_TYPES_VIDEO = ( |
|
6 | FILE_TYPES_VIDEO = ( | |
7 | 'webm', |
|
7 | 'webm', | |
8 | 'mp4', |
|
8 | 'mp4', | |
|
9 | 'mpeg', | |||
9 | ) |
|
10 | ) | |
10 | FILE_TYPE_SVG = 'svg' |
|
11 | FILE_TYPE_SVG = 'svg' | |
11 | FILE_TYPES_AUDIO = ( |
|
12 | FILE_TYPES_AUDIO = ( | |
12 | 'ogg', |
|
13 | 'ogg', | |
13 | 'mp3', |
|
14 | 'mp3', | |
14 | 'opus', |
|
15 | 'opus', | |
15 | ) |
|
16 | ) | |
16 |
|
17 | |||
17 | PLAIN_FILE_FORMATS = { |
|
18 | PLAIN_FILE_FORMATS = { | |
18 | 'pdf': 'pdf', |
|
19 | 'pdf': 'pdf', | |
19 | 'djvu': 'djvu', |
|
20 | 'djvu': 'djvu', | |
20 | 'txt': 'txt', |
|
21 | 'txt': 'txt', | |
21 | } |
|
22 | } | |
22 |
|
23 | |||
23 |
|
24 | |||
24 | def get_viewers(): |
|
25 | def get_viewers(): | |
25 | return AbstractViewer.__subclasses__() |
|
26 | return AbstractViewer.__subclasses__() | |
26 |
|
27 | |||
27 |
|
28 | |||
28 | class AbstractViewer: |
|
29 | class AbstractViewer: | |
29 | def __init__(self, file, file_type): |
|
30 | def __init__(self, file, file_type): | |
30 | self.file = file |
|
31 | self.file = file | |
31 | self.file_type = file_type |
|
32 | self.file_type = file_type | |
32 |
|
33 | |||
33 | @staticmethod |
|
34 | @staticmethod | |
34 | def supports(file_type): |
|
35 | def supports(file_type): | |
35 | return True |
|
36 | return True | |
36 |
|
37 | |||
37 | def get_view(self): |
|
38 | def get_view(self): | |
38 | return '<div class="image">'\ |
|
39 | return '<div class="image">'\ | |
39 | '{}'\ |
|
40 | '{}'\ | |
40 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ |
|
41 | '<div class="image-metadata"><a href="{}" download >{}, {}</a></div>'\ | |
41 | '</div>'.format(self.get_format_view(), self.file.url, |
|
42 | '</div>'.format(self.get_format_view(), self.file.url, | |
42 | self.file_type, filesizeformat(self.file.size)) |
|
43 | self.file_type, filesizeformat(self.file.size)) | |
43 |
|
44 | |||
44 | def get_format_view(self): |
|
45 | def get_format_view(self): | |
45 | if self.file_type in PLAIN_FILE_FORMATS: |
|
46 | if self.file_type in PLAIN_FILE_FORMATS: | |
46 | image = 'images/fileformats/{}.png'.format( |
|
47 | image = 'images/fileformats/{}.png'.format( | |
47 | PLAIN_FILE_FORMATS[self.file_type]) |
|
48 | PLAIN_FILE_FORMATS[self.file_type]) | |
48 | else: |
|
49 | else: | |
49 | image = FILE_STUB_IMAGE |
|
50 | image = FILE_STUB_IMAGE | |
50 |
|
51 | |||
51 | return '<a href="{}">'\ |
|
52 | return '<a href="{}">'\ | |
52 | '<img src="{}" width="200" height="150"/>'\ |
|
53 | '<img src="{}" width="200" height="150"/>'\ | |
53 | '</a>'.format(self.file.url, static(image)) |
|
54 | '</a>'.format(self.file.url, static(image)) | |
54 |
|
55 | |||
55 |
|
56 | |||
56 | class VideoViewer(AbstractViewer): |
|
57 | class VideoViewer(AbstractViewer): | |
57 | @staticmethod |
|
58 | @staticmethod | |
58 | def supports(file_type): |
|
59 | def supports(file_type): | |
59 | return file_type in FILE_TYPES_VIDEO |
|
60 | return file_type in FILE_TYPES_VIDEO | |
60 |
|
61 | |||
61 | def get_format_view(self): |
|
62 | def get_format_view(self): | |
62 | return '<video width="200" height="150" controls src="{}"></video>'\ |
|
63 | return '<video width="200" height="150" controls src="{}"></video>'\ | |
63 | .format(self.file.url) |
|
64 | .format(self.file.url) | |
64 |
|
65 | |||
65 |
|
66 | |||
66 | class AudioViewer(AbstractViewer): |
|
67 | class AudioViewer(AbstractViewer): | |
67 | @staticmethod |
|
68 | @staticmethod | |
68 | def supports(file_type): |
|
69 | def supports(file_type): | |
69 | return file_type in FILE_TYPES_AUDIO |
|
70 | return file_type in FILE_TYPES_AUDIO | |
70 |
|
71 | |||
71 | def get_format_view(self): |
|
72 | def get_format_view(self): | |
72 | return '<audio controls src="{}"></audio>'.format(self.file.url) |
|
73 | return '<audio controls src="{}"></audio>'.format(self.file.url) | |
73 |
|
74 | |||
74 |
|
75 | |||
75 | class SvgViewer(AbstractViewer): |
|
76 | class SvgViewer(AbstractViewer): | |
76 | @staticmethod |
|
77 | @staticmethod | |
77 | def supports(file_type): |
|
78 | def supports(file_type): | |
78 | return file_type == FILE_TYPE_SVG |
|
79 | return file_type == FILE_TYPE_SVG | |
79 |
|
80 | |||
80 | def get_format_view(self): |
|
81 | def get_format_view(self): | |
81 | return '<a class="thumb" href="{}">'\ |
|
82 | return '<a class="thumb" href="{}">'\ | |
82 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ |
|
83 | '<img class="post-image-preview" width="200" height="150" src="{}" />'\ | |
83 | '</a>'.format(self.file.url, self.file.url) |
|
84 | '</a>'.format(self.file.url, self.file.url) |
General Comments 0
You need to be logged in to leave comments.
Login now