Tweak/fix the attachment caching

This commit is contained in:
Thomas Sileo 2018-07-24 23:58:13 +02:00
parent d544cf893e
commit 619044d285
3 changed files with 10 additions and 3 deletions

5
app.py
View file

@ -225,8 +225,9 @@ def _get_file_url(url, size, kind):
_GRIDFS_CACHE[k] = u _GRIDFS_CACHE[k] = u
return u return u
MEDIA_CACHE.cache(url, kind) # MEDIA_CACHE.cache(url, kind)
return _get_file_url(url, size, kind) app.logger.error("cache not available for {url}/{size}/{kind}")
return url
@app.template_filter() @app.template_filter()

View file

@ -239,7 +239,10 @@ def cache_attachments(self, iri: str) -> None:
attachment.get("mediaType", "").startswith("image/") attachment.get("mediaType", "").startswith("image/")
or attachment.get("type") == ap.ActivityType.IMAGE.value 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}") log.info(f"attachments cached for {iri}")

View file

@ -15,6 +15,9 @@ def load(url, user_agent):
"""Initializes a `PIL.Image` from the URL.""" """Initializes a `PIL.Image` from the URL."""
with requests.get(url, stream=True, headers={"User-Agent": user_agent}) as resp: with requests.get(url, stream=True, headers={"User-Agent": user_agent}) as resp:
resp.raise_for_status() 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 resp.raw.decode_content = True
return Image.open(BytesIO(resp.raw.read())) return Image.open(BytesIO(resp.raw.read()))