core, bridge: new imageGeneratePreview helped method to generate a thumbnail
This commit is contained in:
parent
8777bd07cb
commit
3686d7e036
2 changed files with 90 additions and 0 deletions
|
|
@ -135,6 +135,7 @@ class Const(object):
|
|||
|
||||
MESS_KEY_ATTACHMENTS = "attachments"
|
||||
MESS_KEY_ATTACHMENTS_MEDIA_TYPE = "media_type"
|
||||
MESS_KEY_ATTACHMENTS_PREVIEW = "preview"
|
||||
|
||||
# File encryption algorithms
|
||||
ENC_AES_GCM = "AES-GCM"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ from glob import glob
|
|||
import sys
|
||||
import os.path
|
||||
import uuid
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import sat
|
||||
from sat.core.i18n import _, D_, languageSwitch
|
||||
from sat.core import patches
|
||||
|
|
@ -173,6 +175,7 @@ class SAT(service.Service):
|
|||
self.bridge.register_method("namespacesGet", self.getNamespaces)
|
||||
self.bridge.register_method("imageCheck", self._imageCheck)
|
||||
self.bridge.register_method("imageResize", self._imageResize)
|
||||
self.bridge.register_method("imageGeneratePreview", self._imageGeneratePreview)
|
||||
|
||||
self.memory.initialized.addCallback(lambda __: defer.ensureDeferred(self._postMemoryInit()))
|
||||
|
||||
|
|
@ -684,6 +687,45 @@ class SAT(service.Service):
|
|||
d.addCallback(lambda new_image_path: str(new_image_path))
|
||||
return d
|
||||
|
||||
def _imageGeneratePreview(self, path, profile_key):
|
||||
client = self.getClient(profile_key)
|
||||
d = defer.ensureDeferred(self.imageGeneratePreview(client, Path(path)))
|
||||
d.addCallback(lambda preview_path: str(preview_path))
|
||||
return d
|
||||
|
||||
async def imageGeneratePreview(self, client, path):
|
||||
"""Helper method to generate in cache a preview of an image
|
||||
|
||||
@param path(Path): path to the image
|
||||
@return (Path): path to the generated preview
|
||||
"""
|
||||
report = images.checkImage(self, path, max_size=(300, 300))
|
||||
|
||||
if not report['too_large']:
|
||||
# in the unlikely case that image is already smaller than a preview
|
||||
preview_path = path
|
||||
else:
|
||||
# we use hash as id, to re-use potentially existing preview
|
||||
path_hash = hashlib.sha256(str(path).encode()).hexdigest()
|
||||
uid = f"{path.stem}_{path_hash}_preview"
|
||||
filename = f"{uid}{path.suffix.lower()}"
|
||||
metadata = client.cache.getMetadata(uid=uid)
|
||||
if metadata is not None:
|
||||
preview_path = metadata['path']
|
||||
else:
|
||||
with client.cache.cacheData(
|
||||
source='HOST_PREVIEW',
|
||||
uid=uid,
|
||||
filename=filename) as cache_f:
|
||||
|
||||
preview_path = await images.resizeImage(
|
||||
path,
|
||||
new_size=report['recommended_size'],
|
||||
dest=cache_f
|
||||
)
|
||||
|
||||
return preview_path
|
||||
|
||||
# local dirs
|
||||
|
||||
def getLocalPath(self, client, dir_name, *extra_path, **kwargs):
|
||||
|
|
|
|||
Loading…
Reference in a new issue