component file sharing: generate thumbnails for videos
This commit is contained in:
parent
644277a659
commit
33eab526b9
1 changed files with 27 additions and 12 deletions
|
|
@ -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.constants import Const as C
|
|||
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 @@ class FileSharing:
|
|||
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 @@ class FileSharing:
|
|||
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 @@ class FileSharing:
|
|||
)
|
||||
)
|
||||
|
||||
|
||||
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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue