Pass stream into ._error() calls in gallery.py.
[blog.git] / posts / gallery / gallery.py
index a5354fc67ca7274a1045ac46743952cbcc173561..774606129619d5d9b25354f65a77b89f68e0d623 100755 (executable)
@@ -216,7 +216,7 @@ class CGIGalleryServer (object):
         if not url.startswith(self._base_url):
             LOG.error('cannot convert {} to a relative URL of {}'.format(
                     url, self._base_url))
-            return self._error(404)
+            return self._error(404, stream=stream)
         if url == self._base_url:
             return None
         return url[len(self._base_url):]
@@ -270,7 +270,7 @@ class CGIGalleryServer (object):
         LOG.debug('index page')
         return self._directory(self._base_path, stream=stream)
 
-    def _thumb(self, image, max_width=None, max_height=None):
+    def _thumb(self, image, max_width=None, max_height=None, stream=None):
         if not _os_path.exists(self._cache_path):
             _os.makedirs(self._cache_path)
         dirname,filename = _os_path.split(image)
@@ -285,7 +285,7 @@ class CGIGalleryServer (object):
         image_path = _os_path.join(self._base_path, image)
         if not _os_path.isfile(image_path):
             LOG.error('image path for thumbnail does not exist')
-            return self._error(404)
+            return self._error(404, stream=stream)
         if (not _os_path.isfile(thumb_path)
             or _os_path.getmtime(image_path) > _os_path.getmtime(thumb_path)):
             invoke(['convert', '-format', 'png', '-strip', '-quality', '95',
@@ -294,7 +294,7 @@ class CGIGalleryServer (object):
                     thumb_path])
         return thumb_url
 
-    def _mp4(self, video, *args):
+    def _mp4(self, video, stream=None):
         if not video.endswith('.mov'):
             LOG.error("can't translate {} to MPEGv4".format(video))
         dirname,filename = _os_path.split(video)
@@ -307,7 +307,7 @@ class CGIGalleryServer (object):
         mp4_path = _os_path.join(cache_dir, mp4_filename)
         if not _os_path.isfile(video):
             LOG.error('source video path does not exist')
-            return self._error(404)
+            return self._error(404, stream=stream)
         if (not _os_path.isfile(mp4_path)
             or _os_path.getmtime(video) > _os_path.getmtime(mp4_path)):
             arg = ['ffmpeg', '-i', video, '-acodec', 'libfaac', '-aq', '200',
@@ -319,7 +319,7 @@ class CGIGalleryServer (object):
             invoke(arg)
         return self._url(mp4_url)
 
-    def _ogv(self, video, *args):
+    def _ogv(self, video, stream=None):
         if not video.endswith('.mov'):
             LOG.error("can't translate {} to Ogg Video".format(video))
         dirname,filename = _os_path.split(video)
@@ -332,7 +332,7 @@ class CGIGalleryServer (object):
         ogv_path = _os_path.join(cache_dir, ogv_filename)
         if not _os_path.isfile(video):
             LOG.error('source video path does not exist')
-            return self._error(404)
+            return self._error(404, stream=stream)
         if (not _os_path.isfile(ogv_path)
             or _os_path.getmtime(video) > _os_path.getmtime(ogv_path)):
             arg = ['ffmpeg2theora', '--optimize']
@@ -348,18 +348,19 @@ class CGIGalleryServer (object):
         except IOError:
             return None
 
-    def _get_image_video(self, path, fallback=None):
+    def _get_image_video(self, path, fallback=None, stream=None):
         base_path = image_base(path)
         for extension in VIDEO_EXTENSIONS:
             video_path = base_path + extension
             if _os_path.isfile(video_path):
-                return self._video(video_path, fallback=fallback)
+                return self._video(
+                    video_path, fallback=fallback, stream=stream)
         return None
 
-    def _captioned_video(self, path, href=None):
-        img = self._image(path, max_width=640, max_height=480)
+    def _captioned_video(self, path, href=None, stream=None):
+        img = self._image(path, max_width=640, max_height=480, stream=stream)
         caption = self._get_image_caption(path)
-        video = self._get_image_video(path, fallback=[img])
+        video = self._get_image_video(path, fallback=[img], stream=stream)
         content = []
         if video:
             content.extend(video)
@@ -374,7 +375,7 @@ class CGIGalleryServer (object):
             content.append('<p>{}</p>'.format(caption))
         return content
 
-    def _video(self, video, fallback=None, **kwargs):
+    def _video(self, video, fallback=None, stream=None, **kwargs):
         if fallback is None:
             fallback = [
                 '<p>Your browser does not support the &lt;video&gt; tag, try',
@@ -382,8 +383,8 @@ class CGIGalleryServer (object):
                 '</p>',
                 ]
         fallback = ['    '+line for line in fallback]
-        ogv = self._ogv(video)
-        mp4 = self._mp4(video)
+        ogv = self._ogv(video, stream=stream)
+        mp4 = self._mp4(video, stream=stream)
         return [
             '<p>',
             ('  <video preloads="none" controls="controls" '
@@ -431,7 +432,7 @@ class CGIGalleryServer (object):
         image = _random.choice(images)
         LOG.debug('selected random image {}'.format(image))
         page = self._image_page(image)
-        content = self._captioned_video(path=image, href=page)
+        content = self._captioned_video(path=image, href=page, stream=stream)
         self._response(content='\n'.join(content), stream=stream)
 
     def is_cached(self, url):
@@ -457,7 +458,7 @@ class CGIGalleryServer (object):
         except IOError, e:
             LOG.error('invalid url')
             LOG.error(e)
-            self._error(404)
+            self._error(404, stream=stream)
         if mime in ['video/ogg']:
             self._response_stream(
                 header=header, content=content, stream=stream)
@@ -468,7 +469,7 @@ class CGIGalleryServer (object):
         LOG.debug('HTML page')
         if not url.endswith('/'):
             LOG.error('HTML page URLs must end with a slash')
-            self._error(404)
+            self._error(404, stream=stream)
         abspath = _os_path.join(self._base_path, url)
         if _os_path.isdir(abspath):
             self._directory(path=abspath, page=page, stream=stream)
@@ -530,12 +531,13 @@ class CGIGalleryServer (object):
             content.append('</ul>')
         return content
 
-    def _directory_images(self, path, images):
+    def _directory_images(self, path, images, stream=None):
         content = ['<table style="margin-left: auto; margin-right: auto;">']
         column = 0
         for image in images:
             page = self._image_page(image)
-            img = self._image(image, max_width=300, max_height=300)
+            img = self._image(
+                image, max_width=300, max_height=300, stream=stream)
             link = self._link(page, img)
             if column == 0:
                 content.append('  <tr>')
@@ -563,7 +565,7 @@ class CGIGalleryServer (object):
             LOG.error(
                 'page out of bounds for this gallery 0 <= {:d} < {:d}'.format(
                     page, pages))
-            self._error(404)
+            self._error(404, stream=stream)
         first_image = images_per_page * page
         images = images[first_image:first_image+images_per_page]
         content = []
@@ -572,7 +574,8 @@ class CGIGalleryServer (object):
         nav = self._directory_page_navigation(path, page=page, pages=pages)
         content.extend(nav)
         content.extend(self._directory_subdirs(path))
-        content.extend(self._directory_images(path, images=images))
+        content.extend(self._directory_images(
+                path, images=images, stream=stream))
         content.extend(nav)
         content.extend(self.footer)
         self._response(content='\n'.join(content), stream=stream)
@@ -597,7 +600,7 @@ class CGIGalleryServer (object):
                 self._link(next_page, 'next'),
                 '</p>',
                 ])
-        content.extend(self._captioned_video(path))
+        content.extend(self._captioned_video(path, stream=stream))
         content.append('</div>')
         content.extend(self.footer)
         self._response(content='\n'.join(content), stream=stream)