summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-07-23 15:59:32 +0100
committerErik Johnston <erik@matrix.org>2015-07-23 15:59:53 +0100
commit459085184ce80c67584bee4e5d3bc43add99bb0b (patch)
tree55927c3ac8c2054fca104a7c24e580a22c0634a2 /synapse
parentFix remote thumbnailing (diff)
downloadsynapse-459085184ce80c67584bee4e5d3bc43add99bb0b.tar.xz
Factor out thumbnail()
Diffstat (limited to 'synapse')
-rw-r--r--synapse/rest/media/v1/base_resource.py96
1 files changed, 40 insertions, 56 deletions
diff --git a/synapse/rest/media/v1/base_resource.py b/synapse/rest/media/v1/base_resource.py
index 74c0cd093c..093ba847d3 100644
--- a/synapse/rest/media/v1/base_resource.py
+++ b/synapse/rest/media/v1/base_resource.py
@@ -225,41 +225,44 @@ class BaseMediaResource(Resource):
         else:
             return ()
 
-    @defer.inlineCallbacks
-    def _generate_local_exact_thumbnail(self, media_id, t_width, t_height,
-                                        t_method, t_type):
-        input_path = self.filepaths.local_media_filepath(media_id)
+    def _generate_thumbnail(self, input_path, t_path, t_width, t_height,
+                            t_method, t_type):
+        thumbnailer = Thumbnailer(input_path)
+        m_width = thumbnailer.width
+        m_height = thumbnailer.height
 
-        def thumbnail():
-            thumbnailer = Thumbnailer(input_path)
-            m_width = thumbnailer.width
-            m_height = thumbnailer.height
+        if m_width * m_height >= self.max_image_pixels:
+            logger.info(
+                "Image too large to thumbnail %r x %r > %r",
+                m_width, m_height, self.max_image_pixels
+            )
+            return
 
-            if m_width * m_height >= self.max_image_pixels:
-                logger.info(
-                    "Image too large to thumbnail %r x %r > %r",
-                    m_width, m_height, self.max_image_pixels
-                )
-                return
+        if t_method == "crop":
+            t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
+        elif t_method == "scale":
+            t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
+        else:
+            t_len = None
 
-            t_path = self.filepaths.local_media_thumbnail(
-                media_id, t_width, t_height, t_type, t_method
-            )
-            self._makedirs(t_path)
+        return t_len
 
-            if t_method == "crop":
-                t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
-            elif t_method == "scale":
-                t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
-            else:
-                t_len = None
+    @defer.inlineCallbacks
+    def _generate_local_exact_thumbnail(self, media_id, t_width, t_height,
+                                        t_method, t_type):
+        input_path = self.filepaths.local_media_filepath(media_id)
 
-            return t_len, t_path
+        t_path = self.filepaths.local_media_thumbnail(
+            media_id, t_width, t_height, t_type, t_method
+        )
+        self._makedirs(t_path)
 
-        res = yield threads.deferToThread(thumbnail)
+        t_len = yield threads.deferToThread(
+            self._generate_thumbnail,
+            input_path, t_path, t_width, t_height, t_method, t_type
+        )
 
-        if res:
-            t_len, t_path = res
+        if t_len:
             yield self.store.store_local_thumbnail(
                 media_id, t_width, t_height, t_type, t_method, t_len
             )
@@ -271,36 +274,17 @@ class BaseMediaResource(Resource):
                                          t_width, t_height, t_method, t_type):
         input_path = self.filepaths.remote_media_filepath(server_name, file_id)
 
-        def thumbnail():
-            thumbnailer = Thumbnailer(input_path)
-            m_width = thumbnailer.width
-            m_height = thumbnailer.height
-
-            if m_width * m_height >= self.max_image_pixels:
-                logger.info(
-                    "Image too large to thumbnail %r x %r > %r",
-                    m_width, m_height, self.max_image_pixels
-                )
-                return
-
-            t_path = self.filepaths.remote_media_thumbnail(
-                server_name, file_id, t_width, t_height, t_type, t_method
-            )
-            self._makedirs(t_path)
-
-            if t_method == "crop":
-                t_len = thumbnailer.crop(t_path, t_width, t_height, t_type)
-            elif t_method == "scale":
-                t_len = thumbnailer.scale(t_path, t_width, t_height, t_type)
-            else:
-                t_len = None
-
-            return t_path, t_len
+        t_path = self.filepaths.remote_media_thumbnail(
+            server_name, file_id, t_width, t_height, t_type, t_method
+        )
+        self._makedirs(t_path)
 
-        res = yield threads.deferToThread(thumbnail)
+        t_len = yield threads.deferToThread(
+            self._generate_thumbnail,
+            input_path, t_path, t_width, t_height, t_method, t_type
+        )
 
-        if res:
-            t_path, t_len = res
+        if t_len:
             yield self.store.store_remote_media_thumbnail(
                 server_name, media_id, file_id,
                 t_width, t_height, t_type, t_method, t_len