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:
parent
5a34a80d0a
commit
ec4261a449
@ -16,10 +16,13 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import logging
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from mediagoblin import mg_globals
|
from mediagoblin import mg_globals
|
||||||
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
|
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
|
||||||
|
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class FileTypeNotSupported(Exception):
|
class FileTypeNotSupported(Exception):
|
||||||
pass
|
pass
|
||||||
@ -33,7 +36,17 @@ def sniff_media(media):
|
|||||||
Iterate through the enabled media types and find those suited
|
Iterate through the enabled media types and find those suited
|
||||||
for a certain file.
|
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():
|
def get_media_types():
|
||||||
|
@ -14,13 +14,15 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# 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 = {
|
MEDIA_MANAGER = {
|
||||||
"human_readable": "ASCII",
|
"human_readable": "ASCII",
|
||||||
"processor": process_ascii, # alternately a string,
|
"processor": process_ascii, # alternately a string,
|
||||||
# 'mediagoblin.media_types.image.processing'?
|
# 'mediagoblin.media_types.image.processing'?
|
||||||
|
"sniff_handler": sniff_handler,
|
||||||
"display_template": "mediagoblin/media_displays/ascii.html",
|
"display_template": "mediagoblin/media_displays/ascii.html",
|
||||||
"default_thumb": "images/media_thumbs/ascii.jpg",
|
"default_thumb": "images/media_thumbs/ascii.jpg",
|
||||||
"accepted_extensions": [
|
"accepted_extensions": [
|
||||||
|
@ -24,6 +24,9 @@ from mediagoblin.media_types.ascii import asciitoimage
|
|||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def sniff_handler(media_file, **kw):
|
||||||
|
return False
|
||||||
|
|
||||||
def process_ascii(entry):
|
def process_ascii(entry):
|
||||||
'''
|
'''
|
||||||
Code to process a txt file
|
Code to process a txt file
|
||||||
|
@ -25,8 +25,15 @@ from mediagoblin.media_types.audio.transcoders import AudioTranscoder
|
|||||||
|
|
||||||
_log = logging.getLogger()
|
_log = logging.getLogger()
|
||||||
|
|
||||||
def sniff_handler(media):
|
def sniff_handler(media_file, **kw):
|
||||||
return True
|
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):
|
def process_audio(entry):
|
||||||
audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
|
audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
|
||||||
|
@ -62,8 +62,10 @@ class AudioTranscoder(object):
|
|||||||
|
|
||||||
# Instantiate MainLoop
|
# Instantiate MainLoop
|
||||||
self._loop = gobject.MainLoop()
|
self._loop = gobject.MainLoop()
|
||||||
|
self._failed = None
|
||||||
|
|
||||||
def discover(self, src):
|
def discover(self, src):
|
||||||
|
self._src_path = src
|
||||||
_log.info('Discovering {0}'.format(src))
|
_log.info('Discovering {0}'.format(src))
|
||||||
self._discovery_path = src
|
self._discovery_path = src
|
||||||
|
|
||||||
@ -74,14 +76,17 @@ class AudioTranscoder(object):
|
|||||||
|
|
||||||
self._loop.run() # Run MainLoop
|
self._loop.run() # Run MainLoop
|
||||||
|
|
||||||
|
if self._failed:
|
||||||
|
raise self._failed
|
||||||
|
|
||||||
# Once MainLoop has returned, return discovery data
|
# Once MainLoop has returned, return discovery data
|
||||||
return self._discovery_data
|
return getattr(self, '_discovery_data', False)
|
||||||
|
|
||||||
def __on_discovered(self, data, is_media):
|
def __on_discovered(self, data, is_media):
|
||||||
if not is_media:
|
if not is_media:
|
||||||
self.halt()
|
self._failed = BadMediaFail()
|
||||||
_log.error('Could not discover {0}'.format(self._src_path))
|
_log.error('Could not discover {0}'.format(self._src_path))
|
||||||
raise BadMediaFail()
|
self.halt()
|
||||||
|
|
||||||
_log.debug('Discovered: {0}'.format(data.__dict__))
|
_log.debug('Discovered: {0}'.format(data.__dict__))
|
||||||
|
|
||||||
@ -91,6 +96,7 @@ class AudioTranscoder(object):
|
|||||||
self.halt()
|
self.halt()
|
||||||
|
|
||||||
def transcode(self, src, dst, **kw):
|
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._discovery_data = kw.get('data', self.discover(src))
|
||||||
|
|
||||||
self.__on_progress = kw.get('progress_callback')
|
self.__on_progress = kw.get('progress_callback')
|
||||||
|
@ -14,13 +14,15 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# 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 = {
|
MEDIA_MANAGER = {
|
||||||
"human_readable": "Image",
|
"human_readable": "Image",
|
||||||
"processor": process_image, # alternately a string,
|
"processor": process_image, # alternately a string,
|
||||||
# 'mediagoblin.media_types.image.processing'?
|
# 'mediagoblin.media_types.image.processing'?
|
||||||
|
"sniff_handler": sniff_handler,
|
||||||
"display_template": "mediagoblin/media_displays/image.html",
|
"display_template": "mediagoblin/media_displays/image.html",
|
||||||
"default_thumb": "images/media_thumbs/image.jpg",
|
"default_thumb": "images/media_thumbs/image.jpg",
|
||||||
"accepted_extensions": ["jpg", "jpeg", "png", "gif", "tiff"]}
|
"accepted_extensions": ["jpg", "jpeg", "png", "gif", "tiff"]}
|
||||||
|
@ -23,6 +23,9 @@ from mediagoblin.processing import BadMediaFail, \
|
|||||||
from mediagoblin.tools.exif import exif_fix_image_orientation, \
|
from mediagoblin.tools.exif import exif_fix_image_orientation, \
|
||||||
extract_exif, clean_exif, get_gps_data, get_useful
|
extract_exif, clean_exif, get_gps_data, get_useful
|
||||||
|
|
||||||
|
def sniff_handler(media_file, **kw):
|
||||||
|
return False
|
||||||
|
|
||||||
def process_image(entry):
|
def process_image(entry):
|
||||||
"""
|
"""
|
||||||
Code to process an image
|
Code to process an image
|
||||||
|
@ -14,13 +14,15 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# 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 = {
|
MEDIA_MANAGER = {
|
||||||
"human_readable": "Video",
|
"human_readable": "Video",
|
||||||
"processor": process_video, # alternately a string,
|
"processor": process_video, # alternately a string,
|
||||||
# 'mediagoblin.media_types.image.processing'?
|
# 'mediagoblin.media_types.image.processing'?
|
||||||
|
"sniff_handler": sniff_handler,
|
||||||
"display_template": "mediagoblin/media_displays/video.html",
|
"display_template": "mediagoblin/media_displays/video.html",
|
||||||
"default_thumb": "images/media_thumbs/video.jpg",
|
"default_thumb": "images/media_thumbs/video.jpg",
|
||||||
"accepted_extensions": [
|
"accepted_extensions": [
|
||||||
|
@ -28,6 +28,8 @@ logging.basicConfig()
|
|||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
_log.setLevel(logging.DEBUG)
|
_log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
def sniff_handler(media_file, **kw):
|
||||||
|
return False
|
||||||
|
|
||||||
def process_video(entry):
|
def process_video(entry):
|
||||||
"""
|
"""
|
||||||
|
@ -35,7 +35,7 @@ from mediagoblin.decorators import require_active_login
|
|||||||
from mediagoblin.submit import forms as submit_forms, security
|
from mediagoblin.submit import forms as submit_forms, security
|
||||||
from mediagoblin.processing import mark_entry_failed, ProcessMedia
|
from mediagoblin.processing import mark_entry_failed, ProcessMedia
|
||||||
from mediagoblin.messages import add_message, SUCCESS
|
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
|
InvalidFileType, FileTypeNotSupported
|
||||||
|
|
||||||
|
|
||||||
@ -55,7 +55,11 @@ def submit_start(request):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
filename = request.POST['file'].filename
|
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
|
# create entry and save in database
|
||||||
entry = request.db.MediaEntry()
|
entry = request.db.MediaEntry()
|
||||||
@ -164,7 +168,6 @@ def submit_start(request):
|
|||||||
This section is intended to catch exceptions raised in
|
This section is intended to catch exceptions raised in
|
||||||
mediagobling.media_types
|
mediagobling.media_types
|
||||||
'''
|
'''
|
||||||
|
|
||||||
if isinstance(e, InvalidFileType) or \
|
if isinstance(e, InvalidFileType) or \
|
||||||
isinstance(e, FileTypeNotSupported):
|
isinstance(e, FileTypeNotSupported):
|
||||||
submit_form.file.errors.append(
|
submit_form.file.errors.append(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user