Switching the hook 'get_media_manager' to a more "directed" tuple-hook
[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
ec4261a4
JW
18import logging
19import tempfile
93bdab9d 20
58a94757 21from mediagoblin.tools.pluginapi import hook_handle
6506b1e2 22from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
26729e02 23
ec4261a4 24_log = logging.getLogger(__name__)
26729e02 25
93bdab9d
JW
26class FileTypeNotSupported(Exception):
27 pass
28
29class InvalidFileType(Exception):
30 pass
31
93bdab9d 32
6d37733a
E
33class MediaManagerBase(object):
34 "Base class for all media managers"
35
36 # Please override in actual media managers
37 media_fetch_order = None
38
95dab599
E
39 @staticmethod
40 def sniff_handler(*args, **kwargs):
41 return False
42
6d37733a
E
43 def __init__(self, entry):
44 self.entry = entry
45
46 def __getitem__(self, i):
47 return getattr(self, i)
48
49 def __contains__(self, i):
50 return hasattr(self, i)
51
52
5a34a80d
JW
53def sniff_media(media):
54 '''
55 Iterate through the enabled media types and find those suited
56 for a certain file.
57 '''
a9d84d4c
JW
58
59 try:
60 return get_media_type_and_manager(media.filename)
61 except FileTypeNotSupported:
62 _log.info('No media handler found by file extension. Doing it the expensive way...')
63 # Create a temporary file for sniffers suchs as GStreamer-based
64 # Audio video
65 media_file = tempfile.NamedTemporaryFile()
f1d06e1d
JW
66 media_file.write(media.stream.read())
67 media.stream.seek(0)
a9d84d4c 68
58a94757
RE
69 media_type = hook_handle('sniff_handler', media_file, media=media)
70 if media_type:
71 _log.info('{0} accepts the file'.format(media_type))
6403bc92 72 return media_type, hook_handle(('media_manager', media_type))
58a94757
RE
73 else:
74 _log.debug('{0} did not accept the file'.format(media_type))
ec4261a4
JW
75
76 raise FileTypeNotSupported(
77 # TODO: Provide information on which file types are supported
78 _(u'Sorry, I don\'t support that file type :('))
5a34a80d
JW
79
80
93bdab9d 81def get_media_type_and_manager(filename):
4535f759 82 '''
a9d84d4c
JW
83 Try to find the media type based on the file name, extension
84 specifically. This is used as a speedup, the sniffing functionality
85 then falls back on more in-depth bitsniffing of the source file.
4535f759 86 '''
a246ccca
JW
87 if filename.find('.') > 0:
88 # Get the file extension
89 ext = os.path.splitext(filename)[1].lower()
93bdab9d 90
58a94757
RE
91 # Omit the dot from the extension and match it against
92 # the media manager
93 if hook_handle('get_media_type_and_manager', ext[1:]):
94 return hook_handle('get_media_type_and_manager', ext[1:])
a246ccca 95 else:
a9d84d4c
JW
96 _log.info('File {0} has no file extension, let\'s hope the sniffers get it.'.format(
97 filename))
98
99 raise FileTypeNotSupported(
100 _(u'Sorry, I don\'t support that file type :('))