Commit | Line | Data |
---|---|---|
93bdab9d | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
93bdab9d JW |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import os | |
18 | import sys | |
19 | ||
8aeb6738 | 20 | from mediagoblin import mg_globals |
6506b1e2 | 21 | from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ |
26729e02 JW |
22 | |
23 | ||
93bdab9d JW |
24 | class FileTypeNotSupported(Exception): |
25 | pass | |
26 | ||
27 | class InvalidFileType(Exception): | |
28 | pass | |
29 | ||
93bdab9d | 30 | |
5a34a80d JW |
31 | def sniff_media(media): |
32 | ''' | |
33 | Iterate through the enabled media types and find those suited | |
34 | for a certain file. | |
35 | ''' | |
36 | pass | |
37 | ||
38 | ||
93bdab9d | 39 | def get_media_types(): |
cfa96da7 | 40 | """ |
4535f759 | 41 | Generator, yields the available media types |
cfa96da7 CAW |
42 | """ |
43 | for media_type in mg_globals.app_config['media_types']: | |
93bdab9d JW |
44 | yield media_type |
45 | ||
46 | ||
47 | def get_media_managers(): | |
a63b640f | 48 | ''' |
4535f759 | 49 | Generator, yields all enabled media managers |
a63b640f | 50 | ''' |
93bdab9d | 51 | for media_type in get_media_types(): |
6506b1e2 | 52 | __import__(media_type) |
93bdab9d JW |
53 | |
54 | yield media_type, sys.modules[media_type].MEDIA_MANAGER | |
55 | ||
1f255101 | 56 | |
4535f759 JW |
57 | def get_media_manager(_media_type): |
58 | ''' | |
59 | Get the MEDIA_MANAGER based on a media type string | |
60 | ||
61 | Example:: | |
62 | get_media_type('mediagoblin.media_types.image') | |
63 | ''' | |
64 | if not _media_type: | |
65 | return False | |
66 | ||
93bdab9d JW |
67 | for media_type, manager in get_media_managers(): |
68 | if media_type in _media_type: | |
69 | return manager | |
70 | ||
cc4f83fa CAW |
71 | # Nope? Then raise an error |
72 | raise FileTypeNotSupported( | |
73 | "MediaManager not in enabled types. Check media_types in config?") | |
74 | ||
93bdab9d JW |
75 | |
76 | def get_media_type_and_manager(filename): | |
4535f759 JW |
77 | ''' |
78 | Get the media type and manager based on a filename | |
79 | ''' | |
a246ccca JW |
80 | if filename.find('.') > 0: |
81 | # Get the file extension | |
82 | ext = os.path.splitext(filename)[1].lower() | |
83 | else: | |
4601c30c | 84 | raise InvalidFileType( |
a246ccca JW |
85 | _(u'Could not extract any file extension from "{filename}"').format( |
86 | filename=filename)) | |
93bdab9d | 87 | |
a246ccca | 88 | for media_type, manager in get_media_managers(): |
4535f759 JW |
89 | # Omit the dot from the extension and match it against |
90 | # the media manager | |
93bdab9d JW |
91 | if ext[1:] in manager['accepted_extensions']: |
92 | return media_type, manager | |
a246ccca JW |
93 | else: |
94 | raise FileTypeNotSupported( | |
95 | # TODO: Provide information on which file types are supported | |
96 | _(u'Sorry, I don\'t support that file type :(')) |