mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-12-22 13:14:28 +00:00
Tweak/fix the attachment caching
This commit is contained in:
parent
d544cf893e
commit
619044d285
3 changed files with 10 additions and 3 deletions
5
app.py
5
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()
|
||||
|
|
5
tasks.py
5
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}")
|
||||
|
||||
|
|
|
@ -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()))
|
||||
|
||||
|
|
Loading…
Reference in a new issue