Changed media processing delegation to a 'sniffing' method

- Added sniff handlers to all media plugins
  All of them except audio returning False for ANYTHING
  at the moment.
This commit is contained in:
Joar Wandborg 2012-02-15 01:15:29 +01:00
parent 5a34a80d0a
commit ec4261a449
10 changed files with 55 additions and 12 deletions

View File

@ -16,10 +16,13 @@
import os
import sys
import logging
import tempfile
from mediagoblin import mg_globals
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
_log = logging.getLogger(__name__)
class FileTypeNotSupported(Exception):
pass
@ -33,7 +36,17 @@ def sniff_media(media):
Iterate through the enabled media types and find those suited
for a certain file.
'''
pass
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):
return media_type, manager
raise FileTypeNotSupported(
# TODO: Provide information on which file types are supported
_(u'Sorry, I don\'t support that file type :('))
def get_media_types():

View File

@ -14,13 +14,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin.media_types.ascii.processing import process_ascii
from mediagoblin.media_types.ascii.processing import process_ascii, \
sniff_handler
MEDIA_MANAGER = {
"human_readable": "ASCII",
"processor": process_ascii, # alternately a string,
# 'mediagoblin.media_types.image.processing'?
"sniff_handler": sniff_handler,
"display_template": "mediagoblin/media_displays/ascii.html",
"default_thumb": "images/media_thumbs/ascii.jpg",
"accepted_extensions": [

View File

@ -24,6 +24,9 @@ from mediagoblin.media_types.ascii import asciitoimage
_log = logging.getLogger(__name__)
def sniff_handler(media_file, **kw):
return False
def process_ascii(entry):
'''
Code to process a txt file

View File

@ -25,8 +25,15 @@ from mediagoblin.media_types.audio.transcoders import AudioTranscoder
_log = logging.getLogger()
def sniff_handler(media):
return True
def sniff_handler(media_file, **kw):
transcoder = AudioTranscoder()
try:
data = transcoder.discover(media_file.name)
if data.is_audio == True and data.is_video == False:
return True
except:
return False
def process_audio(entry):
audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']

View File

@ -62,8 +62,10 @@ class AudioTranscoder(object):
# Instantiate MainLoop
self._loop = gobject.MainLoop()
self._failed = None
def discover(self, src):
self._src_path = src
_log.info('Discovering {0}'.format(src))
self._discovery_path = src
@ -74,14 +76,17 @@ class AudioTranscoder(object):
self._loop.run() # Run MainLoop
if self._failed:
raise self._failed
# Once MainLoop has returned, return discovery data
return self._discovery_data
return getattr(self, '_discovery_data', False)
def __on_discovered(self, data, is_media):
if not is_media:
self.halt()
self._failed = BadMediaFail()
_log.error('Could not discover {0}'.format(self._src_path))
raise BadMediaFail()
self.halt()
_log.debug('Discovered: {0}'.format(data.__dict__))
@ -91,6 +96,7 @@ class AudioTranscoder(object):
self.halt()
def transcode(self, src, dst, **kw):
_log.info('Transcoding {0} into {1}'.format(src, dst))
self._discovery_data = kw.get('data', self.discover(src))
self.__on_progress = kw.get('progress_callback')

View File

@ -14,13 +14,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin.media_types.image.processing import process_image
from mediagoblin.media_types.image.processing import process_image, \
sniff_handler
MEDIA_MANAGER = {
"human_readable": "Image",
"processor": process_image, # alternately a string,
# 'mediagoblin.media_types.image.processing'?
"sniff_handler": sniff_handler,
"display_template": "mediagoblin/media_displays/image.html",
"default_thumb": "images/media_thumbs/image.jpg",
"accepted_extensions": ["jpg", "jpeg", "png", "gif", "tiff"]}

View File

@ -23,6 +23,9 @@ from mediagoblin.processing import BadMediaFail, \
from mediagoblin.tools.exif import exif_fix_image_orientation, \
extract_exif, clean_exif, get_gps_data, get_useful
def sniff_handler(media_file, **kw):
return False
def process_image(entry):
"""
Code to process an image

View File

@ -14,13 +14,15 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin.media_types.video.processing import process_video
from mediagoblin.media_types.video.processing import process_video, \
sniff_handler
MEDIA_MANAGER = {
"human_readable": "Video",
"processor": process_video, # alternately a string,
# 'mediagoblin.media_types.image.processing'?
"sniff_handler": sniff_handler,
"display_template": "mediagoblin/media_displays/video.html",
"default_thumb": "images/media_thumbs/video.jpg",
"accepted_extensions": [

View File

@ -28,6 +28,8 @@ logging.basicConfig()
_log = logging.getLogger(__name__)
_log.setLevel(logging.DEBUG)
def sniff_handler(media_file, **kw):
return False
def process_video(entry):
"""

View File

@ -35,7 +35,7 @@ from mediagoblin.decorators import require_active_login
from mediagoblin.submit import forms as submit_forms, security
from mediagoblin.processing import mark_entry_failed, ProcessMedia
from mediagoblin.messages import add_message, SUCCESS
from mediagoblin.media_types import get_media_type_and_manager, \
from mediagoblin.media_types import sniff_media, \
InvalidFileType, FileTypeNotSupported
@ -55,7 +55,11 @@ def submit_start(request):
else:
try:
filename = request.POST['file'].filename
media_type, media_manager = get_media_type_and_manager(filename)
# Sniff the submitted media to determine which
# media plugin should handle processing
media_type, media_manager = sniff_media(
request.POST['file'])
# create entry and save in database
entry = request.db.MediaEntry()
@ -164,7 +168,6 @@ def submit_start(request):
This section is intended to catch exceptions raised in
mediagobling.media_types
'''
if isinstance(e, InvalidFileType) or \
isinstance(e, FileTypeNotSupported):
submit_form.file.errors.append(