77
88from beartype import beartype
99from PIL import Image
10+ from werkzeug .datastructures import FileStorage , MultiDict
1011from werkzeug .formparser import MultiPartParser
1112
1213from mock_vws ._query_validators .exceptions import (
1819_LOGGER = logging .getLogger (name = __name__ )
1920
2021
21- @beartype
22- def validate_image_field_given (
22+ def _parse_multipart_files (
2323 * ,
2424 request_headers : Mapping [str , str ],
2525 request_body : bytes ,
26- ) -> None :
27- """Validate that the image field is given .
26+ ) -> MultiDict [ str , FileStorage ] :
27+ """Parse the multipart body and return the files section .
2828
2929 Args:
3030 request_headers: The headers sent with the request.
3131 request_body: The body of the request.
3232
33- Raises :
34- ImageNotGivenError: The image field is not given .
33+ Returns :
34+ The files parsed from the multipart body .
3535 """
3636 email_message = EmailMessage ()
3737 email_message ["Content-Type" ] = request_headers ["Content-Type" ]
@@ -42,6 +42,28 @@ def validate_image_field_given(
4242 boundary = boundary .encode (encoding = "utf-8" ),
4343 content_length = len (request_body ),
4444 )
45+ return files
46+
47+
48+ @beartype
49+ def validate_image_field_given (
50+ * ,
51+ request_headers : Mapping [str , str ],
52+ request_body : bytes ,
53+ ) -> None :
54+ """Validate that the image field is given.
55+
56+ Args:
57+ request_headers: The headers sent with the request.
58+ request_body: The body of the request.
59+
60+ Raises:
61+ ImageNotGivenError: The image field is not given.
62+ """
63+ files = _parse_multipart_files (
64+ request_headers = request_headers ,
65+ request_body = request_body ,
66+ )
4567 if files .get (key = "image" ) is not None :
4668 return
4769
@@ -64,14 +86,9 @@ def validate_image_file_size(
6486 Raises:
6587 RequestEntityTooLargeError: The image file size is too large.
6688 """
67- email_message = EmailMessage ()
68- email_message ["Content-Type" ] = request_headers ["Content-Type" ]
69- boundary = email_message .get_boundary (failobj = "" )
70- parser = MultiPartParser ()
71- _ , files = parser .parse (
72- stream = io .BytesIO (initial_bytes = request_body ),
73- boundary = boundary .encode (encoding = "utf-8" ),
74- content_length = len (request_body ),
89+ files = _parse_multipart_files (
90+ request_headers = request_headers ,
91+ request_body = request_body ,
7592 )
7693 image_part = files ["image" ]
7794 image_value = image_part .stream .read ()
@@ -105,14 +122,9 @@ def validate_image_dimensions(
105122 BadImageError: The image is given and is not within the maximum width
106123 and height limits.
107124 """
108- email_message = EmailMessage ()
109- email_message ["Content-Type" ] = request_headers ["Content-Type" ]
110- boundary = email_message .get_boundary (failobj = "" )
111- parser = MultiPartParser ()
112- _ , files = parser .parse (
113- stream = io .BytesIO (initial_bytes = request_body ),
114- boundary = boundary .encode (encoding = "utf-8" ),
115- content_length = len (request_body ),
125+ files = _parse_multipart_files (
126+ request_headers = request_headers ,
127+ request_body = request_body ,
116128 )
117129 image_part = files ["image" ]
118130 image_value = image_part .stream .read ()
@@ -142,14 +154,9 @@ def validate_image_format(
142154 Raises:
143155 BadImageError: The image is given and is not either a PNG or a JPEG.
144156 """
145- email_message = EmailMessage ()
146- email_message ["Content-Type" ] = request_headers ["Content-Type" ]
147- boundary = email_message .get_boundary (failobj = "" )
148- parser = MultiPartParser ()
149- _ , files = parser .parse (
150- stream = io .BytesIO (initial_bytes = request_body ),
151- boundary = boundary .encode (encoding = "utf-8" ),
152- content_length = len (request_body ),
157+ files = _parse_multipart_files (
158+ request_headers = request_headers ,
159+ request_body = request_body ,
153160 )
154161 image_part = files ["image" ]
155162 pil_image = Image .open (fp = image_part .stream )
@@ -175,17 +182,11 @@ def validate_image_is_image(
175182 Raises:
176183 BadImageError: Image data is given and it is not an image file.
177184 """
178- email_message = EmailMessage ()
179- email_message ["Content-Type" ] = request_headers ["Content-Type" ]
180- boundary = email_message .get_boundary (failobj = "" )
181- parser = MultiPartParser ()
182- _ , files = parser .parse (
183- stream = io .BytesIO (initial_bytes = request_body ),
184- boundary = boundary .encode (encoding = "utf-8" ),
185- content_length = len (request_body ),
185+ files = _parse_multipart_files (
186+ request_headers = request_headers ,
187+ request_body = request_body ,
186188 )
187- image_part = files ["image" ]
188- image_file = image_part .stream
189+ image_file = files ["image" ].stream
189190
190191 try :
191192 Image .open (fp = image_file )
0 commit comments