Kill monkeypatching of ProcessingState.
[mediagoblin.git] / mediagoblin / media_types / audio / processing.py
index ef98e533fed38cfbcfb1b293d30eba75e4ace66f..5dffcaf98a53e359cc3b1f6785f04cf6323cba4f 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import logging
-import tempfile
+from tempfile import NamedTemporaryFile
 import os
 
 from mediagoblin import mg_globals as mgg
 from mediagoblin.processing import (create_pub_filepath, BadMediaFail,
-    FilenameBuilder)
+    FilenameBuilder, ProgressCallback)
 
 from mediagoblin.media_types.audio.transcoders import (AudioTranscoder,
     AudioThumbnailer)
 
 _log = logging.getLogger(__name__)
 
+
 def sniff_handler(media_file, **kw):
     try:
         transcoder = AudioTranscoder()
@@ -40,10 +41,16 @@ def sniff_handler(media_file, **kw):
 
     return False
 
-def process_audio(entry):
-    audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
 
-    workbench = mgg.workbench_manager.create_workbench()
+def process_audio(proc_state):
+    """Code to process uploaded audio. Will be run by celery.
+
+    A Workbench() represents a local tempory dir. It is automatically
+    cleaned up when this function exits.
+    """
+    entry = proc_state.entry
+    workbench = proc_state.workbench
+    audio_config = mgg.global_config['media_type:mediagoblin.media_types.audio']
 
     queued_filepath = entry.queued_media_file
     queued_filename = workbench.localized_file(
@@ -71,14 +78,16 @@ def process_audio(entry):
 
     transcoder = AudioTranscoder()
 
-    with tempfile.NamedTemporaryFile() as webm_audio_tmp:
+    with NamedTemporaryFile(dir=workbench.dir) as webm_audio_tmp:
+        progress_callback = ProgressCallback(entry)
 
         transcoder.transcode(
             queued_filename,
             webm_audio_tmp.name,
-            quality=audio_config['quality'])
+            quality=audio_config['quality'],
+            progress_callback=progress_callback)
 
-        data = transcoder.discover(webm_audio_tmp.name)
+        transcoder.discover(webm_audio_tmp.name)
 
         _log.debug('Saving medium...')
         mgg.public_store.get_file(webm_audio_filepath, 'wb').write(
@@ -95,16 +104,17 @@ def process_audio(entry):
                 original=os.path.splitext(
                     queued_filepath[-1])[0]))
 
-        with tempfile.NamedTemporaryFile(suffix='.wav') as wav_tmp:
-            _log.info('Creating WAV source for spectrogram')
+        with NamedTemporaryFile(dir=workbench.dir, suffix='.ogg') as wav_tmp:
+            _log.info('Creating OGG source for spectrogram')
             transcoder.transcode(
                 queued_filename,
                 wav_tmp.name,
-                mux_string='wavenc')
+                mux_string='vorbisenc quality={0} ! oggmux'.format(
+                    audio_config['quality']))
 
             thumbnailer = AudioThumbnailer()
 
-            with tempfile.NamedTemporaryFile(suffix='.jpg') as spectrogram_tmp:
+            with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as spectrogram_tmp:
                 thumbnailer.spectrogram(
                     wav_tmp.name,
                     spectrogram_tmp.name,
@@ -117,7 +127,7 @@ def process_audio(entry):
 
                 entry.media_files['spectrogram'] = spectrogram_filepath
 
-                with tempfile.NamedTemporaryFile(suffix='.jpg') as thumb_tmp:
+                with NamedTemporaryFile(dir=workbench.dir, suffix='.jpg') as thumb_tmp:
                     thumbnailer.thumbnail_spectrogram(
                         spectrogram_tmp.name,
                         thumb_tmp.name,
@@ -138,8 +148,3 @@ def process_audio(entry):
         entry.media_files['thumb'] = ['fake', 'thumb', 'path.jpg']
 
     mgg.queue_store.delete_file(queued_filepath)
-
-    entry.save()
-
-    # clean up workbench
-    workbench.destroy_self()