File tree Expand file tree Collapse file tree 4 files changed +20
-10
lines changed
Expand file tree Collapse file tree 4 files changed +20
-10
lines changed Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff line change 22from uuid import UUID
33
44import PIL .Image as Image
5+ from PIL .Image import MAX_IMAGE_PIXELS , DecompressionBombError
56from werkzeug .datastructures import FileStorage
67
78from 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_ (
46- "You can only upload pictures up to "
47- "a total pixel count of {MAX_PIXEL_PER_IMAGE}."
46+ "You can only upload pictures up to a total pixel count of "
47+ "{0} pixels." . format ( MAX_IMAGE_PIXELS )
4848 ),
49- {"MAX_PIXEL_PER_IMAGE" : max_pixel_per_image },
50- )
49+ ) from error
5150 del img
5251 file .seek (0 )
5352
Original file line number Diff line number Diff line change @@ -33,7 +33,6 @@ def config_keys():
3333 "wms-layers-esri-world-imagery-fallback" ,
3434 "wms-read-timeout" ,
3535 "max-nr-simultaneous-uploads" ,
36- "max_pixel_per_image" ,
3736 "yolo_cls" ,
3837 "yolo_osm_obj" ,
3938 "yolo_esri_obj" ,
Original file line number Diff line number Diff line change 1+ import PIL
12import pytest
23
4+ from sketch_map_tool .exceptions import UploadLimitsExceededError
35from 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):
4043def 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
You can’t perform that action at this time.
0 commit comments