changeset 3348:12c427156cac

component file sharing: generate thumbnails for videos
author Goffi <goffi@goffi.org>
date Tue, 25 Aug 2020 08:55:16 +0200
parents c8033a9357e7
children 2a7e36b69fd2
files sat/plugins/plugin_comp_file_sharing.py
diffstat 1 files changed, 27 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_file_sharing.py	Tue Aug 25 08:54:13 2020 +0200
+++ b/sat/plugins/plugin_comp_file_sharing.py	Tue Aug 25 08:55:16 2020 +0200
@@ -19,6 +19,7 @@
 import os
 import os.path
 import mimetypes
+import tempfile
 from functools import partial
 import shortuuid
 import unicodedata
@@ -29,6 +30,7 @@
 from sat.core import exceptions
 from sat.core.log import getLogger
 from sat.tools import stream
+from sat.tools import video
 from sat.tools.common import regex
 from sat.tools.common import uri
 from sat.tools.common import files_utils
@@ -399,6 +401,21 @@
         if not os.path.exists(path):
             os.makedirs(path)
 
+    async def generate_thumbnails(self, extra: dict, image_path: Path):
+        thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
+        for max_thumb_size in self._t.SIZES:
+            try:
+                thumb_size, thumb_id = await self._t.generateThumbnail(
+                    image_path,
+                    max_thumb_size,
+                    #  we keep thumbnails for 6 months
+                    60 * 60 * 24 * 31 * 6,
+                )
+            except Exception as e:
+                log.warning(_("Can't create thumbnail: {reason}").format(reason=e))
+                break
+            thumbnails.append({"id": thumb_id, "size": thumb_size})
+
     async def registerReceivedFile(
             self, client, peer_jid, file_data, file_path, public_id=None, extra=None):
         """Post file reception tasks
@@ -415,6 +432,7 @@
             mime_type = mimetypes.guess_type(name)[0]
 
         is_image = mime_type is not None and mime_type.startswith("image")
+        is_video = mime_type is not None and mime_type.startswith("video")
 
         if file_data.get("hash_algo") == HASH_ALGO:
             log.debug(_("Reusing already generated hash"))
@@ -440,21 +458,18 @@
                 )
             )
 
-
         if is_image:
-            thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
-            for max_thumb_size in self._t.SIZES:
+            await self.generate_thumbnails(extra, final_path)
+        elif is_video:
+            with tempfile.TemporaryDirectory() as tmp_dir:
+                thumb_path = Path(tmp_dir) / "thumbnail.jpg"
                 try:
-                    thumb_size, thumb_id = await self._t.generateThumbnail(
-                        final_path,
-                        max_thumb_size,
-                        #  we keep thumbnails for 6 months
-                        60 * 60 * 24 * 31 * 6,
-                    )
+                    await video.get_thumbnail(final_path, thumb_path)
                 except Exception as e:
-                    log.warning(_("Can't create thumbnail: {reason}").format(reason=e))
-                    break
-                thumbnails.append({"id": thumb_id, "size": thumb_size})
+                    log.warning(_("Can't get thumbnail for {final_path}: {e}").format(
+                        final_path=final_path, e=e))
+                else:
+                    await self.generate_thumbnails(extra, thumb_path)
 
         self.host.memory.setFile(
             client,