Video support is disabled by default, set enable_video to true to enable
[mediagoblin.git] / mediagoblin / media_types / __init__.py
CommitLineData
93bdab9d
JW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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
17import os
18import sys
19
8aeb6738 20from mediagoblin import mg_globals
26729e02
JW
21from mediagoblin.util import lazy_pass_to_ugettext as _
22
23
93bdab9d
JW
24class FileTypeNotSupported(Exception):
25 pass
26
27class InvalidFileType(Exception):
28 pass
29
a63b640f
JW
30# This should be more dynamic in the future. Perhaps put it in the .ini?
31# -- Joar
93bdab9d 32MEDIA_TYPES = [
8aeb6738
JW
33 'mediagoblin.media_types.image']
34
35if mg_globals.app_config['enable_video']:
36 MEDIA_TYPES.append('mediagoblin.media_types.video')
93bdab9d
JW
37
38
39def get_media_types():
a63b640f
JW
40 '''
41 Generator that returns the available media types
42 '''
93bdab9d
JW
43 for media_type in MEDIA_TYPES:
44 yield media_type
45
46
47def get_media_managers():
a63b640f
JW
48 '''
49 Generator that returns all available media managers
50 '''
93bdab9d 51 for media_type in get_media_types():
93bdab9d
JW
52 try:
53 __import__(media_type)
54 except ImportError as e:
a63b640f
JW
55 raise Exception(
56 _('ERROR: Could not import {media_type}: {exception}').format(
57 media_type=media_type,
58 exception=e))
93bdab9d
JW
59
60 yield media_type, sys.modules[media_type].MEDIA_MANAGER
61
1f255101 62
93bdab9d
JW
63def get_media_manager(_media_type = None):
64 for media_type, manager in get_media_managers():
65 if media_type in _media_type:
66 return manager
67
68
69def get_media_type_and_manager(filename):
70 for media_type, manager in get_media_managers():
71 if filename.find('.') > 0:
72 ext = os.path.splitext(filename)[1].lower()
73 else:
74 raise InvalidFileType(
a63b640f
JW
75 _('Could not find any file extension in "{filename}"').format(
76 filename=filename))
93bdab9d
JW
77
78 if ext[1:] in manager['accepted_extensions']:
79 return media_type, manager