Option not to cache uploaded image

This commit is contained in:
Ville Ranki 2021-07-19 22:44:10 +03:00
parent 40aa62282d
commit f66c76e09c
1 changed files with 8 additions and 3 deletions

7
bot.py
View File

@ -78,7 +78,7 @@ class Bot:
self.logger.debug("Logger initialized") self.logger.debug("Logger initialized")
async def upload_and_send_image(self, room, url, text=None, blob=False, blob_content_type="image/png"): async def upload_and_send_image(self, room, url, text=None, blob=False, blob_content_type="image/png", no_cache=False):
""" """
:param room: A MatrixRoom the image should be send to after uploading :param room: A MatrixRoom the image should be send to after uploading
@ -86,18 +86,23 @@ class Bot:
:param text: A textual representation of the image :param text: A textual representation of the image
:param blob: Flag to indicate if the second param is an url or a binary content :param blob: Flag to indicate if the second param is an url or a binary content
:param blob_content_type: Content type of the image in case of binary content :param blob_content_type: Content type of the image in case of binary content
:param no_cache: Set to true if you want to bypass cache and always re-upload the file
:return: :return:
""" """
cache_key = url cache_key = url
if blob: ## url is bytes, cannot be used a key for cache if blob: ## url is bytes, cannot be used a key for cache
cache_key = hashlib.md5(url).hexdigest() cache_key = hashlib.md5(url).hexdigest()
if no_cache:
cache_key = None
try: try:
matrix_uri, mimetype, w, h, size = self.uri_cache[cache_key] matrix_uri, mimetype, w, h, size = self.uri_cache[cache_key]
except KeyError: except KeyError:
try: try:
res = await self.upload_image(url, blob, blob_content_type) res = await self.upload_image(url, blob, blob_content_type)
matrix_uri, mimetype, w, h, size = res matrix_uri, mimetype, w, h, size = res
if not no_cache:
self.uri_cache[cache_key] = list(res) self.uri_cache[cache_key] = list(res)
self.save_settings() self.save_settings()
except UploadFailed: except UploadFailed: