Show More
@@ -6,14 +6,15 b' from random import random' | |||
|
6 | 6 | |
|
7 | 7 | from django.db import models |
|
8 | 8 | |
|
9 | from boards.models.attachment.viewers import AbstractViewer, WebmViewer | |
|
10 | ||
|
9 | from boards.models.attachment.viewers import AbstractViewer, WebmViewer, \ | |
|
10 | AudioViewer | |
|
11 | 11 | |
|
12 | 12 | FILES_DIRECTORY = 'files/' |
|
13 | 13 | FILE_EXTENSION_DELIMITER = '.' |
|
14 | 14 | |
|
15 | 15 | VIEWERS = ( |
|
16 | 16 | WebmViewer, |
|
17 | AudioViewer, | |
|
17 | 18 | ) |
|
18 | 19 | |
|
19 | 20 |
@@ -1,6 +1,14 b'' | |||
|
1 | 1 | from django.template.defaultfilters import filesizeformat |
|
2 | 2 | from django.templatetags.static import static |
|
3 | 3 | |
|
4 | FILE_STUB_IMAGE = 'images/file.png' | |
|
5 | ||
|
6 | FILE_TYPE_WEBM = 'webm' | |
|
7 | FILE_TYPES_AUDIO = ( | |
|
8 | 'ogg', | |
|
9 | 'mp3', | |
|
10 | ) | |
|
11 | ||
|
4 | 12 | |
|
5 | 13 | class AbstractViewer: |
|
6 | 14 | def __init__(self, file, file_type): |
@@ -8,33 +16,35 b' class AbstractViewer:' | |||
|
8 | 16 | self.file_type = file_type |
|
9 | 17 | |
|
10 | 18 | @staticmethod |
|
11 | def get_viewer(file_type, file): | |
|
12 | for viewer in VIEWERS: | |
|
13 | if viewer.supports(file_type): | |
|
14 | return viewer(file) | |
|
15 | return AbstractViewer(file) | |
|
16 | ||
|
17 | @staticmethod | |
|
18 | 19 | def supports(file_type): |
|
19 |
return |
|
|
20 | return True | |
|
20 | 21 | |
|
21 | 22 | def get_view(self): |
|
22 | 23 | return '<div class="image"><a href="{}">'\ |
|
23 | 24 | '<img src="{}" width="200" height="150"/>'\ |
|
24 | 25 | '</a>'\ |
|
25 | 26 | '<div class="image-metadata">{}, {}</div>'\ |
|
26 |
'</div>'.format(self.file.url, static( |
|
|
27 | filesizeformat(self.file.size)) | |
|
27 | '</div>'.format(self.file.url, static(FILE_STUB_IMAGE), | |
|
28 | self.file_type, filesizeformat(self.file.size)) | |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | class WebmViewer(AbstractViewer): |
|
31 | 32 | @staticmethod |
|
32 | 33 | def supports(file_type): |
|
33 |
return file_type == |
|
|
34 | return file_type == FILE_TYPE_WEBM | |
|
34 | 35 | |
|
35 | 36 | def get_view(self): |
|
36 | 37 | return '<div class="image">'\ |
|
37 | '<video width="200" height="150" controls/>'\ | |
|
38 | '<source src="{}">'\ | |
|
38 | '<video width="200" height="150" controls src="{}"/>'\ | |
|
39 | 39 | '</div>'.format(self.file.url) |
|
40 | 40 | |
|
41 | ||
|
42 | class AudioViewer(AbstractViewer): | |
|
43 | @staticmethod | |
|
44 | def supports(file_type): | |
|
45 | return file_type in FILE_TYPES_AUDIO | |
|
46 | ||
|
47 | def get_view(self): | |
|
48 | return '<div class="image">' \ | |
|
49 | '<audio controls src="{}"/>' \ | |
|
50 | '</div>'.format(self.file.url) |
General Comments 0
You need to be logged in to leave comments.
Login now