comparison sat/core/sat_main.py @ 3201:439e2f88c3a9

core, bridge: new `imageGeneratePreview` helped method to generate a thumbnail
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 18:48:06 +0100
parents 84a94b385760
children ae09989e9feb
comparison
equal deleted inserted replaced
3200:5c3bf37f2202 3201:439e2f88c3a9
18 18
19 from glob import glob 19 from glob import glob
20 import sys 20 import sys
21 import os.path 21 import os.path
22 import uuid 22 import uuid
23 import hashlib
24 from pathlib import Path
23 import sat 25 import sat
24 from sat.core.i18n import _, D_, languageSwitch 26 from sat.core.i18n import _, D_, languageSwitch
25 from sat.core import patches 27 from sat.core import patches
26 patches.apply() 28 patches.apply()
27 from twisted.application import service 29 from twisted.application import service
171 self.bridge.register_method("loadParamsTemplate", self.memory.load_xml) 173 self.bridge.register_method("loadParamsTemplate", self.memory.load_xml)
172 self.bridge.register_method("sessionInfosGet", self.getSessionInfos) 174 self.bridge.register_method("sessionInfosGet", self.getSessionInfos)
173 self.bridge.register_method("namespacesGet", self.getNamespaces) 175 self.bridge.register_method("namespacesGet", self.getNamespaces)
174 self.bridge.register_method("imageCheck", self._imageCheck) 176 self.bridge.register_method("imageCheck", self._imageCheck)
175 self.bridge.register_method("imageResize", self._imageResize) 177 self.bridge.register_method("imageResize", self._imageResize)
178 self.bridge.register_method("imageGeneratePreview", self._imageGeneratePreview)
176 179
177 self.memory.initialized.addCallback(lambda __: defer.ensureDeferred(self._postMemoryInit())) 180 self.memory.initialized.addCallback(lambda __: defer.ensureDeferred(self._postMemoryInit()))
178 181
179 @property 182 @property
180 def version(self): 183 def version(self):
682 def _imageResize(self, path, width, height): 685 def _imageResize(self, path, width, height):
683 d = images.resizeImage(path, (width, height)) 686 d = images.resizeImage(path, (width, height))
684 d.addCallback(lambda new_image_path: str(new_image_path)) 687 d.addCallback(lambda new_image_path: str(new_image_path))
685 return d 688 return d
686 689
690 def _imageGeneratePreview(self, path, profile_key):
691 client = self.getClient(profile_key)
692 d = defer.ensureDeferred(self.imageGeneratePreview(client, Path(path)))
693 d.addCallback(lambda preview_path: str(preview_path))
694 return d
695
696 async def imageGeneratePreview(self, client, path):
697 """Helper method to generate in cache a preview of an image
698
699 @param path(Path): path to the image
700 @return (Path): path to the generated preview
701 """
702 report = images.checkImage(self, path, max_size=(300, 300))
703
704 if not report['too_large']:
705 # in the unlikely case that image is already smaller than a preview
706 preview_path = path
707 else:
708 # we use hash as id, to re-use potentially existing preview
709 path_hash = hashlib.sha256(str(path).encode()).hexdigest()
710 uid = f"{path.stem}_{path_hash}_preview"
711 filename = f"{uid}{path.suffix.lower()}"
712 metadata = client.cache.getMetadata(uid=uid)
713 if metadata is not None:
714 preview_path = metadata['path']
715 else:
716 with client.cache.cacheData(
717 source='HOST_PREVIEW',
718 uid=uid,
719 filename=filename) as cache_f:
720
721 preview_path = await images.resizeImage(
722 path,
723 new_size=report['recommended_size'],
724 dest=cache_f
725 )
726
727 return preview_path
728
687 # local dirs 729 # local dirs
688 730
689 def getLocalPath(self, client, dir_name, *extra_path, **kwargs): 731 def getLocalPath(self, client, dir_name, *extra_path, **kwargs):
690 """retrieve path for local data 732 """retrieve path for local data
691 733