Skip to content

Commit

Permalink
Increase gpkg file size limit to 5mb
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Nov 4, 2024
1 parent 76a0427 commit 5f449ec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
5 changes: 4 additions & 1 deletion qgis-app/base/forms/processing_forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from base.validator import filesize_validator
from django import forms
from django.utils.translation import gettext_lazy as _
import os


class ResourceBaseReviewForm(forms.Form):
Expand Down Expand Up @@ -49,5 +50,7 @@ def clean_file(self):
"""

file = self.cleaned_data["file"]
if filesize_validator(file.file):
file_extension = os.path.splitext(file.name)[1]
is_gpkg = file_extension == ".gpkg"
if filesize_validator(file.file, is_gpkg):
return file
30 changes: 17 additions & 13 deletions qgis-app/base/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
from django.utils.translation import gettext_lazy as _

RESOURCE_MAX_SIZE = getattr(settings, "RESOURCE_MAX_SIZE", 1000000) # 1MB
ERROR_FILESIZE_TOO_BIG = ValidationError(
_("File is too big. Max size is %s Megabytes") % (RESOURCE_MAX_SIZE / 1000000)
)
GPKG_MAX_SIZE = getattr(settings, "GPKG_MAX_SIZE", 5000000) # 5MB


def filesize_validator(file) -> bool:
def filesize_validator(file, is_gpkg=False) -> bool:
"""File Size Validation"""
max_size = GPKG_MAX_SIZE if is_gpkg else RESOURCE_MAX_SIZE

error_filesize_too_big = ValidationError(
_("File is too big. Max size is %s Megabytes") % (max_size / 1000000)
)
try:
if file.getbuffer().nbytes > RESOURCE_MAX_SIZE:
raise ERROR_FILESIZE_TOO_BIG
if file.getbuffer().nbytes > max_size:
raise error_filesize_too_big
except AttributeError:
try:
file.seek(0, os.SEEK_END)
if file.seek(0, os.SEEK_END) > RESOURCE_MAX_SIZE:
raise ERROR_FILESIZE_TOO_BIG
if file.seek(0, os.SEEK_END) > max_size:
raise error_filesize_too_big
except AttributeError:
try:
if file.size > RESOURCE_MAX_SIZE:
raise ERROR_FILESIZE_TOO_BIG
if file.size > max_size:
raise error_filesize_too_big
except AttributeError:
try:
if file.len > RESOURCE_MAX_SIZE:
raise ERROR_FILESIZE_TOO_BIG
if file.len > max_size:
raise error_filesize_too_big
except Exception:
raise ERROR_FILESIZE_TOO_BIG
raise error_filesize_too_big
return True
2 changes: 1 addition & 1 deletion qgis-app/geopackages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Geopackage(Resource):
# file
file = models.FileField(
_("GeoPackage file"),
help_text=_("A GeoPackage file. The filesize must less than 1MB "),
help_text=_("A GeoPackage file. The filesize must less than 5MB "),
upload_to=GEOPACKAGES_STORAGE_PATH,
validators=[FileExtensionValidator(allowed_extensions=["gpkg", "zip"])],
null=False,
Expand Down
Binary file modified qgis-app/geopackages/tests/gpkgfiles/dummy_oversize.gpkg
Binary file not shown.
2 changes: 1 addition & 1 deletion qgis-app/geopackages/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_form_invalid_filesize(self):
form = UploadForm(data, file_data)
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors, {"file": ["File is too big. Max size is 1.0 Megabytes"]}
form.errors, {"file": ["File is too big. Max size is 5.0 Megabytes"]}
)


Expand Down

0 comments on commit 5f449ec

Please sign in to comment.