fix cache for blob

This commit is contained in:
Andrea Spacca 2021-05-15 11:05:41 +02:00 committed by xPMo
parent c1dff18c60
commit 4e42b14e50
1 changed files with 10 additions and 4 deletions

10
bot.py
View File

@ -16,6 +16,7 @@ import urllib.parse
import logging import logging
import logging.config import logging.config
import datetime import datetime
import hashlib
from importlib import reload from importlib import reload
from io import BytesIO from io import BytesIO
from PIL import Image from PIL import Image
@ -84,13 +85,17 @@ class Bot:
: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
:return: :return:
""" """
cache_key = url
if blob: ## url is bytes, cannot be used a key for cache
cache_key = hashlib.md5(url).hexdigest()
try: try:
matrix_uri, mimetype, w, h, size = self.uri_cache[url] matrix_uri, mimetype, w, h, size = self.uri_cache[cache_key]
except KeyError: except KeyError:
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 matrix_uri: if matrix_uri:
self.uri_cache[url] = list(res) self.uri_cache[cache_key] = list(res)
self.save_settings() self.save_settings()
else: else:
return await self.send_text(room, "sorry. something went wrong uploading the image to matrix server :(") return await self.send_text(room, "sorry. something went wrong uploading the image to matrix server :(")
@ -222,6 +227,7 @@ class Bot:
"thumbnail_url": None, "thumbnail_url": None,
}, },
} }
if mimetype: if mimetype:
msg["info"]["mimetype"] = mimetype msg["info"]["mimetype"] = mimetype
if width: if width: