mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-19 14:48:59 +00:00
Merge pull request #1123 from eatsleepdeploy/add-max-upload-size-config
Add max upload size config
This commit is contained in:
commit
ff45238667
7 changed files with 61 additions and 0 deletions
|
@ -9,6 +9,7 @@ from django.contrib.postgres.fields import ArrayField as DjangoArrayField
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.core.files.base import ContentFile
|
from django.core.files.base import ContentFile
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.forms import ClearableFileInput, ImageField
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
|
@ -332,6 +333,14 @@ class TagField(ManyToManyField):
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
class ClearableFileInputWithWarning(ClearableFileInput):
|
||||||
|
template_name = "widgets/clearable_file_input_with_warning.html"
|
||||||
|
|
||||||
|
|
||||||
|
class CustomImageField(ImageField):
|
||||||
|
widget = ClearableFileInputWithWarning
|
||||||
|
|
||||||
|
|
||||||
def image_serializer(value, alt):
|
def image_serializer(value, alt):
|
||||||
"""helper for serializing images"""
|
"""helper for serializing images"""
|
||||||
if value and hasattr(value, "url"):
|
if value and hasattr(value, "url"):
|
||||||
|
@ -395,6 +404,14 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
|
||||||
image_content = ContentFile(response.content)
|
image_content = ContentFile(response.content)
|
||||||
return [image_name, image_content]
|
return [image_name, image_content]
|
||||||
|
|
||||||
|
def formfield(self, **kwargs):
|
||||||
|
return super().formfield(
|
||||||
|
**{
|
||||||
|
"form_class": CustomImageField,
|
||||||
|
**kwargs,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
|
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):
|
||||||
"""activitypub-aware datetime field"""
|
"""activitypub-aware datetime field"""
|
||||||
|
|
|
@ -21,6 +21,7 @@ CELERY_TASK_SERIALIZER = "json"
|
||||||
CELERY_RESULT_SERIALIZER = "json"
|
CELERY_RESULT_SERIALIZER = "json"
|
||||||
|
|
||||||
# email
|
# email
|
||||||
|
EMAIL_BACKEND = env("EMAIL_BACKEND", "django.core.mail.backends.smtp.EmailBackend")
|
||||||
EMAIL_HOST = env("EMAIL_HOST")
|
EMAIL_HOST = env("EMAIL_HOST")
|
||||||
EMAIL_PORT = env("EMAIL_PORT", 587)
|
EMAIL_PORT = env("EMAIL_PORT", 587)
|
||||||
EMAIL_HOST_USER = env("EMAIL_HOST_USER")
|
EMAIL_HOST_USER = env("EMAIL_HOST_USER")
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
let BookWyrm = new class {
|
let BookWyrm = new class {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.MAX_FILE_SIZE_BYTES = 10 * 1000000
|
||||||
this.initOnDOMLoaded();
|
this.initOnDOMLoaded();
|
||||||
this.initReccuringTasks();
|
this.initReccuringTasks();
|
||||||
this.initEventListeners();
|
this.initEventListeners();
|
||||||
|
@ -32,15 +33,26 @@ let BookWyrm = new class {
|
||||||
'click',
|
'click',
|
||||||
this.back)
|
this.back)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
document.querySelectorAll('input[type="file"]')
|
||||||
|
.forEach(node => node.addEventListener(
|
||||||
|
'change',
|
||||||
|
this.disableIfTooLarge.bind(this)
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute code once the DOM is loaded.
|
* Execute code once the DOM is loaded.
|
||||||
*/
|
*/
|
||||||
initOnDOMLoaded() {
|
initOnDOMLoaded() {
|
||||||
|
const bookwyrm = this
|
||||||
|
|
||||||
window.addEventListener('DOMContentLoaded', function() {
|
window.addEventListener('DOMContentLoaded', function() {
|
||||||
document.querySelectorAll('.tab-group')
|
document.querySelectorAll('.tab-group')
|
||||||
.forEach(tabs => new TabGroup(tabs));
|
.forEach(tabs => new TabGroup(tabs));
|
||||||
|
document.querySelectorAll('input[type="file"]').forEach(
|
||||||
|
bookwyrm.disableIfTooLarge.bind(bookwyrm)
|
||||||
|
)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,4 +296,27 @@ let BookWyrm = new class {
|
||||||
node.classList.remove(classname);
|
node.classList.remove(classname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disableIfTooLarge(eventOrElement) {
|
||||||
|
const { addRemoveClass, MAX_FILE_SIZE_BYTES } = this
|
||||||
|
const element = eventOrElement.currentTarget || eventOrElement
|
||||||
|
|
||||||
|
const submits = element.form.querySelectorAll('[type="submit"]')
|
||||||
|
const warns = element.parentElement.querySelectorAll('.file-too-big')
|
||||||
|
const isTooBig = element.files &&
|
||||||
|
element.files[0] &&
|
||||||
|
element.files[0].size > MAX_FILE_SIZE_BYTES
|
||||||
|
|
||||||
|
if (isTooBig) {
|
||||||
|
submits.forEach(submitter => submitter.disabled = true)
|
||||||
|
warns.forEach(
|
||||||
|
sib => addRemoveClass(sib, 'is-hidden', false)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
submits.forEach(submitter => submitter.disabled = false)
|
||||||
|
warns.forEach(
|
||||||
|
sib => addRemoveClass(sib, 'is-hidden', true)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
{% load i18n %}
|
||||||
|
{% include "django/forms/widgets/clearable_file_input.html" %}
|
||||||
|
<span class="help file-cta is-hidden file-too-big">{% trans "File exceeds maximum size: 10MB" %}</span>
|
|
@ -1,3 +1,5 @@
|
||||||
|
include /etc/nginx/conf.d/server_config;
|
||||||
|
|
||||||
upstream web {
|
upstream web {
|
||||||
server web:8000;
|
server web:8000;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
include /etc/nginx/conf.d/server_config;
|
||||||
|
|
||||||
upstream web {
|
upstream web {
|
||||||
server web:8000;
|
server web:8000;
|
||||||
}
|
}
|
||||||
|
|
1
nginx/server_config
Normal file
1
nginx/server_config
Normal file
|
@ -0,0 +1 @@
|
||||||
|
client_max_body_size 10m;
|
Loading…
Reference in a new issue