Merge remote-tracking branch 'refs/remotes/elrond/sql/final'
[mediagoblin.git] / mediagoblin / media_types / __init__.py
CommitLineData
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
17import os
18import sys
ec4261a4
JW
19import logging
20import tempfile
93bdab9d 21
8aeb6738 22from mediagoblin import mg_globals
6506b1e2 23from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
26729e02 24
ec4261a4 25_log = logging.getLogger(__name__)
26729e02 26
93bdab9d
JW
27class FileTypeNotSupported(Exception):
28 pass
29
30class InvalidFileType(Exception):
31 pass
32
93bdab9d 33
5a34a80d
JW
34def sniff_media(media):
35 '''
36 Iterate through the enabled media types and find those suited
37 for a certain file.
38 '''
a9d84d4c
JW
39
40 try:
41 return get_media_type_and_manager(media.filename)
42 except FileTypeNotSupported:
43 _log.info('No media handler found by file extension. Doing it the expensive way...')
44 # Create a temporary file for sniffers suchs as GStreamer-based
45 # Audio video
46 media_file = tempfile.NamedTemporaryFile()
47 media_file.write(media.file.read())
48 media.file.seek(0)
49
50 for media_type, manager in get_media_managers():
51 _log.info('Sniffing {0}'.format(media_type))
52 if manager['sniff_handler'](media_file, media=media):
53 _log.info('{0} accepts the file'.format(media_type))
54 return media_type, manager
55 else:
56 _log.debug('{0} did not accept the file'.format(media_type))
ec4261a4
JW
57
58 raise FileTypeNotSupported(
59 # TODO: Provide information on which file types are supported
60 _(u'Sorry, I don\'t support that file type :('))
5a34a80d
JW
61
62
93bdab9d 63def get_media_types():
cfa96da7 64 """
4535f759 65 Generator, yields the available media types
cfa96da7
CAW
66 """
67 for media_type in mg_globals.app_config['media_types']:
93bdab9d
JW
68 yield media_type
69
70
71def get_media_managers():
a63b640f 72 '''
4535f759 73 Generator, yields all enabled media managers
a63b640f 74 '''
93bdab9d 75 for media_type in get_media_types():
6506b1e2 76 __import__(media_type)
a9d84d4c 77
93bdab9d
JW
78 yield media_type, sys.modules[media_type].MEDIA_MANAGER
79
1f255101 80
4535f759
JW
81def get_media_manager(_media_type):
82 '''
83 Get the MEDIA_MANAGER based on a media type string
84
85 Example::
86 get_media_type('mediagoblin.media_types.image')
87 '''
88 if not _media_type:
89 return False
90
93bdab9d
JW
91 for media_type, manager in get_media_managers():
92 if media_type in _media_type:
93 return manager
94
cc4f83fa
CAW
95 # Nope? Then raise an error
96 raise FileTypeNotSupported(
97 "MediaManager not in enabled types. Check media_types in config?")
98
93bdab9d
JW
99
100def get_media_type_and_manager(filename):
4535f759 101 '''
a9d84d4c
JW
102 Try to find the media type based on the file name, extension
103 specifically. This is used as a speedup, the sniffing functionality
104 then falls back on more in-depth bitsniffing of the source file.
4535f759 105 '''
a246ccca
JW
106 if filename.find('.') > 0:
107 # Get the file extension
108 ext = os.path.splitext(filename)[1].lower()
93bdab9d 109
a9d84d4c
JW
110 for media_type, manager in get_media_managers():
111 # Omit the dot from the extension and match it against
112 # the media manager
113 if ext[1:] in manager['accepted_extensions']:
114 return media_type, manager
a246ccca 115 else:
a9d84d4c
JW
116 _log.info('File {0} has no file extension, let\'s hope the sniffers get it.'.format(
117 filename))
118
119 raise FileTypeNotSupported(
120 _(u'Sorry, I don\'t support that file type :('))