Esempio n. 1
0
 def __init__(
     self,
     preview_builder_factory: PreviewBuilderFactory,
     cache_path: str,
     file_path: str,
     file_ext: str,
 ):
     self.mimetype = preview_builder_factory.get_file_mimetype(file_path, file_ext)
     self.builder = preview_builder_factory.get_preview_builder(self.mimetype)
     self.hash = hashlib.md5(file_path.encode("utf-8")).hexdigest()
     file_lock_path = os.path.join(cache_path, self.hash + LOCKFILE_EXTENSION)
     self.filelock = FileLock(file_lock_path, timeout=LOCK_DEFAULT_TIMEOUT)
Esempio n. 2
0
    def __init__(self,
                 cache_folder_path: str,
                 create_folder: bool = False) -> None:
        """
        :param cache_folder_path: path to the cache folder.
        This is where previews will be stored
        :param create_folder: if True, then create the cache folder
        if it does not exist
        """
        self.logger = logging.getLogger(LOGGER_NAME)
        cache_folder_path = os.path.join(cache_folder_path,
                                         "")  # add trailing slash
        # nopep8 see https://stackoverflow.com/questions/2736144/python-add-trailing-slash-to-directory-string-os-independently

        self.cache_path = cache_folder_path  # type: str
        self._factory = (
            PreviewBuilderFactory.get_instance()
        )  # nopep8 keep link to singleton instance as it will be often used

        if create_folder and not os.path.isdir(self.cache_path):
            try:
                os.makedirs(self.cache_path)
            except OSError:
                self.logger.error("cant create cache folder [{}]".format(
                    self.cache_path))
Esempio n. 3
0
 def get_mimetype(self, file_path):
     """
     Return detected mimetype of the file
     :param file_path: path of the file
     :return: mimetype of the file
     """
     return PreviewBuilderFactory().get_file_mimetype(file_path)
Esempio n. 4
0
 def get_mimetype(self, file_path: str, file_ext: str = "") -> str:
     """
     Return detected mimetype of the file
     :param file_path: path of the file
     :param file_ext: extension associated to the file. Eg 'jpg'. May be empty -
             it's usefull if the extension can't be found in file_path
     :return: mimetype of the file
     """
     return (PreviewBuilderFactory().get_instance().get_file_mimetype(
         file_path, file_ext))  # nopep8