aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/media_types
diff options
context:
space:
mode:
authorJoar Wandborg <git@wandborg.com>2012-03-22 01:27:19 +0100
committerJoar Wandborg <git@wandborg.com>2012-03-22 01:27:19 +0100
commita9d84d4cb79252549d472f9f2059b45bbce4f4f1 (patch)
treee3b2301a34cacef2d40a2542a63f06f490faf32c /mediagoblin/media_types
parent9743ce886ab3655662afaa0ef7b08791a65e37da (diff)
downloadmediagoblin-a9d84d4cb79252549d472f9f2059b45bbce4f4f1.tar.lz
mediagoblin-a9d84d4cb79252549d472f9f2059b45bbce4f4f1.tar.xz
mediagoblin-a9d84d4cb79252549d472f9f2059b45bbce4f4f1.zip
Faster sniffing
- Sniffing now goes through the old extension-based filter before doing it the bitsniffing way. - Refractored get_media_type_and_manager(filename). - Removed ogg extension from video accepted extensions, audio will take care of that. - Added custom audio player, still WIP,but working. - Added test for sniffing. This only tests for the mediagoblin.media_types.image type, as that is the only one enabled from start.
Diffstat (limited to 'mediagoblin/media_types')
-rw-r--r--mediagoblin/media_types/__init__.py56
-rw-r--r--mediagoblin/media_types/video/__init__.py2
2 files changed, 33 insertions, 25 deletions
diff --git a/mediagoblin/media_types/__init__.py b/mediagoblin/media_types/__init__.py
index db8c6f73..93d2319f 100644
--- a/mediagoblin/media_types/__init__.py
+++ b/mediagoblin/media_types/__init__.py
@@ -36,16 +36,24 @@ def sniff_media(media):
Iterate through the enabled media types and find those suited
for a certain file.
'''
- media_file = tempfile.NamedTemporaryFile()
- media_file.write(media.file.read())
- media.file.seek(0)
- for media_type, manager in get_media_managers():
- _log.info('Sniffing {0}'.format(media_type))
- if manager['sniff_handler'](media_file, media=media):
- _log.info('{0} accepts the file'.format(media_type))
- return media_type, manager
- else:
- _log.debug('{0} did not accept the file'.format(media_type))
+
+ try:
+ return get_media_type_and_manager(media.filename)
+ except FileTypeNotSupported:
+ _log.info('No media handler found by file extension. Doing it the expensive way...')
+ # Create a temporary file for sniffers suchs as GStreamer-based
+ # Audio video
+ media_file = tempfile.NamedTemporaryFile()
+ media_file.write(media.file.read())
+ media.file.seek(0)
+
+ for media_type, manager in get_media_managers():
+ _log.info('Sniffing {0}'.format(media_type))
+ if manager['sniff_handler'](media_file, media=media):
+ _log.info('{0} accepts the file'.format(media_type))
+ return media_type, manager
+ else:
+ _log.debug('{0} did not accept the file'.format(media_type))
raise FileTypeNotSupported(
# TODO: Provide information on which file types are supported
@@ -66,7 +74,7 @@ def get_media_managers():
'''
for media_type in get_media_types():
__import__(media_type)
-
+
yield media_type, sys.modules[media_type].MEDIA_MANAGER
@@ -91,22 +99,22 @@ def get_media_manager(_media_type):
def get_media_type_and_manager(filename):
'''
- Get the media type and manager based on a filename
+ Try to find the media type based on the file name, extension
+ specifically. This is used as a speedup, the sniffing functionality
+ then falls back on more in-depth bitsniffing of the source file.
'''
if filename.find('.') > 0:
# Get the file extension
ext = os.path.splitext(filename)[1].lower()
- else:
- raise InvalidFileType(
- _(u'Could not extract any file extension from "{filename}"').format(
- filename=filename))
- for media_type, manager in get_media_managers():
- # Omit the dot from the extension and match it against
- # the media manager
- if ext[1:] in manager['accepted_extensions']:
- return media_type, manager
+ for media_type, manager in get_media_managers():
+ # Omit the dot from the extension and match it against
+ # the media manager
+ if ext[1:] in manager['accepted_extensions']:
+ return media_type, manager
else:
- raise FileTypeNotSupported(
- # TODO: Provide information on which file types are supported
- _(u'Sorry, I don\'t support that file type :('))
+ _log.info('File {0} has no file extension, let\'s hope the sniffers get it.'.format(
+ filename))
+
+ raise FileTypeNotSupported(
+ _(u'Sorry, I don\'t support that file type :('))
diff --git a/mediagoblin/media_types/video/__init__.py b/mediagoblin/media_types/video/__init__.py
index 5351abe6..a53927d5 100644
--- a/mediagoblin/media_types/video/__init__.py
+++ b/mediagoblin/media_types/video/__init__.py
@@ -26,4 +26,4 @@ MEDIA_MANAGER = {
"display_template": "mediagoblin/media_displays/video.html",
"default_thumb": "images/media_thumbs/video.jpg",
"accepted_extensions": [
- "mp4", "mov", "webm", "avi", "3gp", "3gpp", "mkv", "ogv", "ogg"]}
+ "mp4", "mov", "webm", "avi", "3gp", "3gpp", "mkv", "ogv"]}