summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-17 12:53:13 -0700
committerAndrew Godwin2022-12-17 12:53:13 -0700
commit2eb07a5f5fce5075369c0fdc3548b1bbd25f3362 (patch)
treea5d33445f8abdb633ec7a3e338625e335fa7ec30
parent24a4fbe1f820e6ce13a969f39dd7676894987532 (diff)
downloadtakahe-2eb07a5f5fce5075369c0fdc3548b1bbd25f3362.tar.gz
takahe-2eb07a5f5fce5075369c0fdc3548b1bbd25f3362.tar.bz2
takahe-2eb07a5f5fce5075369c0fdc3548b1bbd25f3362.zip
Download and store user avatars locally if small
-rw-r--r--takahe/settings.py4
-rw-r--r--users/models/identity.py12
2 files changed, 16 insertions, 0 deletions
diff --git a/takahe/settings.py b/takahe/settings.py
index 3017293..d284e0e 100644
--- a/takahe/settings.py
+++ b/takahe/settings.py
@@ -107,6 +107,10 @@ class Settings(BaseSettings):
#: is necessary for compatibility with Mastodon’s image proxy.
MEDIA_MAX_IMAGE_FILESIZE_MB: int = 10
+ #: Maximum filesize for Avatars. Remote avatars larger than this size will
+ #: not be fetched and served from media, but served through the image proxy.
+ AVATAR_MAX_IMAGE_FILESIZE_KB: int = 1000
+
#: Maximum filesize for Emoji. Attempting to upload Local Emoji larger than this size will be
#: blocked. Remote Emoji larger than this size will not be fetched and served from media, but
#: served through the image proxy.
diff --git a/users/models/identity.py b/users/models/identity.py
index 7c3fa35..c8a154c 100644
--- a/users/models/identity.py
+++ b/users/models/identity.py
@@ -5,12 +5,14 @@ from urllib.parse import urlparse
import httpx
import urlman
from asgiref.sync import async_to_sync, sync_to_async
+from django.conf import settings
from django.db import IntegrityError, models
from django.template.defaultfilters import linebreaks_filter
from django.utils import timezone
from django.utils.functional import lazy
from core.exceptions import ActorMismatchError
+from core.files import get_remote_file
from core.html import sanitize_post, strip_html
from core.ld import canonicalise, format_ld_date, get_list, media_type_from_filename
from core.models import Config
@@ -42,6 +44,16 @@ class IdentityStates(StateGraph):
return cls.updated
# Run the actor fetch and progress to updated if it succeeds
if await identity.fetch_actor():
+ # Also stash their icon if we can
+ if identity.icon_uri:
+ file, mimetype = await get_remote_file(
+ identity.icon_uri,
+ timeout=settings.SETUP.REMOTE_TIMEOUT,
+ max_size=settings.SETUP.AVATAR_MAX_IMAGE_FILESIZE_KB * 1024,
+ )
+ if file:
+ identity.icon = file
+ await sync_to_async(identity.save)()
return cls.updated
@classmethod