From 619044d285938991604e1049985fd181cc92cf84 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 24 Jul 2018 23:58:13 +0200 Subject: [PATCH] Tweak/fix the attachment caching --- app.py | 5 +++-- tasks.py | 5 ++++- utils/media.py | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 1ae4424..e39223f 100644 --- a/app.py +++ b/app.py @@ -225,8 +225,9 @@ def _get_file_url(url, size, kind): _GRIDFS_CACHE[k] = u return u - MEDIA_CACHE.cache(url, kind) - return _get_file_url(url, size, kind) + # MEDIA_CACHE.cache(url, kind) + app.logger.error("cache not available for {url}/{size}/{kind}") + return url @app.template_filter() diff --git a/tasks.py b/tasks.py index 1d2cf11..5bcce31 100644 --- a/tasks.py +++ b/tasks.py @@ -239,7 +239,10 @@ def cache_attachments(self, iri: str) -> None: attachment.get("mediaType", "").startswith("image/") or attachment.get("type") == ap.ActivityType.IMAGE.value ): - MEDIA_CACHE.cache(attachment["url"], Kind.ATTACHMENT) + try: + MEDIA_CACHE.cache(attachment["url"], Kind.ATTACHMENT) + except ValueError: + log.exception(f"failed to cache {attachment}") log.info(f"attachments cached for {iri}") diff --git a/utils/media.py b/utils/media.py index 6767514..66ad02a 100644 --- a/utils/media.py +++ b/utils/media.py @@ -15,6 +15,9 @@ def load(url, user_agent): """Initializes a `PIL.Image` from the URL.""" with requests.get(url, stream=True, headers={"User-Agent": user_agent}) as resp: resp.raise_for_status() + if not resp.headers.get('content-type').startswith('image/'): + raise ValueError(f"bad content-type {resp.headers.get('content-type')}") + resp.raw.decode_content = True return Image.open(BytesIO(resp.raw.read()))