# HG changeset patch # User Goffi # Date 1598338516 -7200 # Node ID 12c427156cacfdeb431486a6ed68d7599a2634a7 # Parent c8033a9357e7ec3773b9cfb44fdb83c8ba06f9b1 component file sharing: generate thumbnails for videos diff -r c8033a9357e7 -r 12c427156cac sat/plugins/plugin_comp_file_sharing.py --- 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,