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