Skip to content

Commit 82f984f

Browse files
refactor: use internal PIL size limit as maximal file size limit
1 parent 291542d commit 82f984f

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

sketch_map_tool/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"wms-layers-esri-world-imagery-fallback": "world_imagery_fallback",
2222
"wms-read-timeout": 600,
2323
"max-nr-simultaneous-uploads": 100,
24-
"max_pixel_per_image": 10e8, # 10.000*10.000
2524
"yolo_cls": "SMT-CLS",
2625
"yolo_osm_obj": "SMT-OSM",
2726
"yolo_esri_obj": "SMT-ESRI",

sketch_map_tool/validators.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from uuid import UUID
33

44
import PIL.Image as Image
5+
from PIL.Image import DecompressionBombError
56
from werkzeug.datastructures import FileStorage
67

78
from sketch_map_tool import get_config_value
@@ -27,7 +28,6 @@ def validate_uploaded_sketchmaps(files: list[FileStorage]):
2728
"""Validation function for uploaded files."""
2829

2930
max_nr_simultaneous_uploads = int(get_config_value("max-nr-simultaneous-uploads"))
30-
max_pixel_per_image = int(get_config_value("max_pixel_per_image"))
3131

3232
if len(files) > max_nr_simultaneous_uploads:
3333
raise UploadLimitsExceededError(
@@ -38,16 +38,15 @@ def validate_uploaded_sketchmaps(files: list[FileStorage]):
3838
)
3939

4040
for file in files:
41-
img = Image.open(file)
42-
total_pxl_cnt = img.size[0] * img.size[1]
43-
if total_pxl_cnt > max_pixel_per_image:
41+
try:
42+
img = Image.open(file)
43+
except DecompressionBombError as error:
4444
raise UploadLimitsExceededError(
4545
N_(
4646
"You can only upload pictures up to "
47-
"a total pixel count of {MAX_PIXEL_PER_IMAGE}."
47+
"a total pixel count of 178956970 pixels (128 Mpx)."
4848
),
49-
{"MAX_PIXEL_PER_IMAGE": max_pixel_per_image},
50-
)
49+
) from error
5150
del img
5251
file.seek(0)
5352

tests/unit/test_validators.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import PIL
12
import pytest
23

4+
from sketch_map_tool.exceptions import UploadLimitsExceededError
35
from sketch_map_tool.validators import (
46
validate_bbox,
57
validate_type,
8+
validate_uploaded_sketchmaps,
69
validate_uuid,
710
)
811

@@ -40,3 +43,13 @@ def test_validate_bbox(bbox_wgs84_str):
4043
def test_validate_bbox_invalid(bbox_str_):
4144
with pytest.raises(ValueError):
4245
validate_bbox(bbox_str_)
46+
47+
48+
def test_validate_uploaded_sketchmaps(file):
49+
before = PIL.Image.MAX_IMAGE_PIXELS
50+
try:
51+
PIL.Image.MAX_IMAGE_PIXELS = 1
52+
with pytest.raises(UploadLimitsExceededError):
53+
validate_uploaded_sketchmaps([file])
54+
finally:
55+
PIL.Image.MAX_IMAGE_PIXELS = before

0 commit comments

Comments
 (0)