Skip to content

Commit

Permalink
Fix some minor bugs with the file and image change view
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Sep 25, 2023
1 parent a42f9fc commit b656de2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ CHANGELOG
unreleased
==========

* feat: limit uploaded image area (width x height) to prevent decompression
bombs
* fix: Run validators on updated files in file change view
* fix: Update mime type if uploading file in file change view
* fix: Do not allow to remove the file field from an uplaoded file in
the admin interface
* fix: refactor upload checks into running validators in the admin
and adding clean methods for file and (abstract) image models.
* fix: ensure uniqueness of icon admin url names
* fix: Crash with django-storage if filer file does not have a
storage file attached

3.0.6 (2023-09-08)
==================
Expand Down
30 changes: 18 additions & 12 deletions filer/admin/fileadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils.safestring import mark_safe
from django.utils.timezone import now
from django.utils.translation import gettext as _
from easy_thumbnails.engine import NoSourceGenerator

from easy_thumbnails.exceptions import InvalidImageFormatError
from easy_thumbnails.files import get_thumbnailer
Expand All @@ -27,20 +28,25 @@ class Meta:
model = File
exclude = ()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.fields["file"].widget = forms.FileInput()

def clean(self):
from ..validation import validate_upload
cleaned_data = super().clean()

mime_type = mimetypes.guess_type(cleaned_data["file"].name)[0] or 'application/octet-stream'
file = cleaned_data["file"]
file.open("w+") # Allow for sanitizing upload
validate_upload(
file_name=cleaned_data["file"].name,
file=file.file,
owner=cleaned_data["owner"],
mime_type=mime_type,
)
file.open("r")
if "file" in self.changed_data and cleaned_data["file"]:
mime_type = mimetypes.guess_type(cleaned_data["file"].name)[0] or 'application/octet-stream'
file = cleaned_data["file"]
file.open("w+") # Allow for sanitizing upload
file.seek(0)
validate_upload(
file_name=cleaned_data["file"].name,
file=file.file,
owner=cleaned_data["owner"],
mime_type=mime_type,
)
file.open("r")
return self.cleaned_data


Expand Down Expand Up @@ -203,7 +209,7 @@ def icon_view(self, request, file_id: int, size: int) -> HttpResponse:
# Touch thumbnail to allow it to be prefetched for directory listing
EasyThumbnail.objects.filter(name=thumbnail.name).update(modified=now())
return HttpResponseRedirect(thumbnail.url)
except InvalidImageFormatError:
except (InvalidImageFormatError, NoSourceGenerator):
return HttpResponseRedirect(staticfiles_storage.url('filer/icons/file-missing.svg'))


Expand Down
2 changes: 1 addition & 1 deletion filer/fields/multistorage_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def exists(self):
"""
Returns ``True`` if underlying file exists in storage.
"""
return self.storage.exists(self.name)
return self.name and self.storage.exists(self.name)


class MultiStorageFileField(easy_thumbnails_fields.ThumbnailerField):
Expand Down
4 changes: 3 additions & 1 deletion filer/models/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def clean(self):
# the image gets attached to a folder and saved. We also
# send the error msg in the JSON and also post the message
# so that they know what is wrong with the image they uploaded
if not self.file:
return

Check warning on line 134 in filer/models/abstract.py

View check run for this annotation

Codecov / codecov/patch

filer/models/abstract.py#L134

Added line #L134 was not covered by tests

if self._width is None or self._height is None:
pixels = 2 * FILER_MAX_IMAGE_PIXELS + 1
Expand All @@ -144,7 +146,7 @@ def clean(self):
msg = _(
"Image format not recognized or image size exceeds limit of %(max_pixels)d million "
"pixels by a factor of two or more. Check file format or resize image to "
"%(width)d x %(height)d) resolution or lower."
"%(width)d x %(height)d resolution or lower."
) % dict(max_pixels=FILER_MAX_IMAGE_PIXELS // 1000000, width=res_x, height=res_y)
raise ValidationError(str(msg), code="image_size")

Expand Down

0 comments on commit b656de2

Please sign in to comment.