|
1 | 1 | import logging
|
2 | 2 |
|
3 | 3 | from django.conf import settings
|
| 4 | +from django.core.checks import Warning, register as register_check |
4 | 5 | from django.core.exceptions import ValidationError
|
5 | 6 | from django.db import models
|
6 | 7 | from django.utils.functional import cached_property
|
7 | 8 | from django.utils.translation import gettext_lazy as _
|
8 | 9 |
|
| 10 | + |
9 | 11 | import easy_thumbnails.utils
|
10 | 12 | from easy_thumbnails.VIL import Image as VILImage
|
11 | 13 | from PIL.Image import MAX_IMAGE_PIXELS
|
|
24 | 26 | # as if we allow it, it will fail while thumbnailing (first in the admin thumbnails
|
25 | 27 | # and then in the page itself.
|
26 | 28 | # Refer this https://github.com/python-pillow/Pillow/blob/b723e9e62e4706a85f7e44cb42b3d838dae5e546/src/PIL/Image.py#L3148
|
27 |
| -FILER_MAX_IMAGE_PIXELS = min( |
28 |
| - getattr(settings, "FILER_MAX_IMAGE_PIXELS", MAX_IMAGE_PIXELS), |
29 |
| - MAX_IMAGE_PIXELS, |
30 |
| -) |
| 29 | +FILER_MAX_IMAGE_PIXELS = getattr(settings, "FILER_MAX_IMAGE_PIXELS", MAX_IMAGE_PIXELS) |
| 30 | +if MAX_IMAGE_PIXELS is not None: |
| 31 | + FILER_MAX_IMAGE_PIXELS = min(FILER_MAX_IMAGE_PIXELS, MAX_IMAGE_PIXELS) |
| 32 | + |
| 33 | + |
| 34 | +@register_check() |
| 35 | +def max_pixel_setting_check(app_configs, **kwargs): |
| 36 | + if not FILER_MAX_IMAGE_PIXELS: |
| 37 | + return [ |
| 38 | + Warning( |
| 39 | + "Both settings.FILER_MAX_IMAGE_PIXELS and PIL.Image.MAX_IMAGE_PIXELS are not set.", |
| 40 | + hint="Set FILER_MAX_IMAGE_PIXELS to a positive integer value in your settings.py. " |
| 41 | + "This setting is used to limit the maximum number of pixels an image can have " |
| 42 | + "to protect your site from memory bombs.", |
| 43 | + obj=settings, |
| 44 | + ) |
| 45 | + ] |
| 46 | + return [] |
31 | 47 |
|
32 | 48 |
|
33 | 49 | class BaseImage(File):
|
@@ -130,7 +146,7 @@ def clean(self):
|
130 | 146 | # the image gets attached to a folder and saved. We also
|
131 | 147 | # send the error msg in the JSON and also post the message
|
132 | 148 | # so that they know what is wrong with the image they uploaded
|
133 |
| - if not self.file: |
| 149 | + if not self.file or not FILER_MAX_IMAGE_PIXELS: |
134 | 150 | return
|
135 | 151 |
|
136 | 152 | if self._width is None or self._height is None:
|
|
0 commit comments