Make max media upload size configurable

This commit is contained in:
Avi Flax 2022-12-04 20:20:50 -05:00 committed by GitHub
parent 3f8045f412
commit 4493eefb76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -1,4 +1,5 @@
from django import forms from django import forms
from django.conf import settings
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -158,10 +159,12 @@ class ImageUpload(FormView):
def clean_image(self): def clean_image(self):
value = self.cleaned_data["image"] value = self.cleaned_data["image"]
if value.size > 1024 * 1024 * 10: max_mb = settings.SETUP.MEDIA_MAX_IMAGE_FILESIZE_MB
max_bytes = max_mb * 1024 * 1024
if value.size > max_bytes:
# Erase the file from our data to stop trying to show it again # Erase the file from our data to stop trying to show it again
self.files = {} self.files = {}
raise forms.ValidationError("File must be 10MB or less") raise forms.ValidationError(f"File must be {max_mb}MB or less")
return value return value
def form_invalid(self, form): def form_invalid(self, form):

View file

@ -94,6 +94,11 @@ class Settings(BaseSettings):
MEDIA_ROOT: str = str(BASE_DIR / "media") MEDIA_ROOT: str = str(BASE_DIR / "media")
MEDIA_BACKEND: Optional[MediaBackendUrl] = None MEDIA_BACKEND: Optional[MediaBackendUrl] = None
#: Maximum filesize when uploading images. Increasing this may increase memory utilization
#: because all images with a dimension greater than 2000px are resized to meet that limit, which
#: is necessary for compatibility with Mastodons image proxy.
MEDIA_MAX_IMAGE_FILESIZE_MB: int = 10
#: Request timeouts to use when talking to other servers Either #: Request timeouts to use when talking to other servers Either
#: float or tuple of floats for (connect, read, write, pool) #: float or tuple of floats for (connect, read, write, pool)
REMOTE_TIMEOUT: float | tuple[float, float, float, float] = 5.0 REMOTE_TIMEOUT: float | tuple[float, float, float, float] = 5.0