Changed media processing delegation to a 'sniffing' method
[mediagoblin.git] / mediagoblin / media_types / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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
17 import os
18 import sys
19 import logging
20 import tempfile
21
22 from mediagoblin import mg_globals
23 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
24
25 _log = logging.getLogger(__name__)
26
27 class FileTypeNotSupported(Exception):
28 pass
29
30 class InvalidFileType(Exception):
31 pass
32
33
34 def sniff_media(media):
35 '''
36 Iterate through the enabled media types and find those suited
37 for a certain file.
38 '''
39 media_file = tempfile.NamedTemporaryFile()
40 media_file.write(media.file.read())
41 media.file.seek(0)
42 for media_type, manager in get_media_managers():
43 _log.info('Sniffing {0}'.format(media_type))
44 if manager['sniff_handler'](media_file, media=media):
45 return media_type, manager
46
47 raise FileTypeNotSupported(
48 # TODO: Provide information on which file types are supported
49 _(u'Sorry, I don\'t support that file type :('))
50
51
52 def get_media_types():
53 """
54 Generator, yields the available media types
55 """
56 for media_type in mg_globals.app_config['media_types']:
57 yield media_type
58
59
60 def get_media_managers():
61 '''
62 Generator, yields all enabled media managers
63 '''
64 for media_type in get_media_types():
65 __import__(media_type)
66
67 yield media_type, sys.modules[media_type].MEDIA_MANAGER
68
69
70 def get_media_manager(_media_type):
71 '''
72 Get the MEDIA_MANAGER based on a media type string
73
74 Example::
75 get_media_type('mediagoblin.media_types.image')
76 '''
77 if not _media_type:
78 return False
79
80 for media_type, manager in get_media_managers():
81 if media_type in _media_type:
82 return manager
83
84 # Nope? Then raise an error
85 raise FileTypeNotSupported(
86 "MediaManager not in enabled types. Check media_types in config?")
87
88
89 def get_media_type_and_manager(filename):
90 '''
91 Get the media type and manager based on a filename
92 '''
93 if filename.find('.') > 0:
94 # Get the file extension
95 ext = os.path.splitext(filename)[1].lower()
96 else:
97 raise InvalidFileType(
98 _(u'Could not extract any file extension from "{filename}"').format(
99 filename=filename))
100
101 for media_type, manager in get_media_managers():
102 # Omit the dot from the extension and match it against
103 # the media manager
104 if ext[1:] in manager['accepted_extensions']:
105 return media_type, manager
106 else:
107 raise FileTypeNotSupported(
108 # TODO: Provide information on which file types are supported
109 _(u'Sorry, I don\'t support that file type :('))