3434
3535
3636HEIGHT , WIDTH = 512 , 1024
37+ MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB upload limit
38+ MAX_IMAGE_DIMENSION = 10000 # max pixels per side
3739
3840app = fastapi .FastAPI ()
3941model_manager = app_utils .ModelManager ()
@@ -57,14 +59,27 @@ async def predict(
5759 A JSON encoded list of detections.
5860 """
5961 image_data = await image .read ()
62+ if len (image_data ) > MAX_FILE_SIZE :
63+ return fastapi .responses .JSONResponse (
64+ content = {'message' : 'Uploaded file exceeds the 10 MB size limit.' },
65+ status_code = 413 ,
66+ ) # Request Entity Too Large
6067 try :
6168 p_image = PIL .Image .open (io .BytesIO (image_data ))
69+ p_image .verify ()
70+ p_image = PIL .Image .open (io .BytesIO (image_data ))
6271 except (OSError , PIL .UnidentifiedImageError ):
6372 return fastapi .responses .JSONResponse (
6473 content = {'message' : 'Could not open image_data as an image.' },
6574 status_code = 400 ,
6675 ) # Bad Request
6776
77+ if p_image .width > MAX_IMAGE_DIMENSION or p_image .height > MAX_IMAGE_DIMENSION :
78+ return fastapi .responses .JSONResponse (
79+ content = {'message' : 'Image dimensions exceed the allowed limit.' },
80+ status_code = 400 ,
81+ ) # Bad Request
82+
6883 try :
6984 tf_image = tf .image .resize (
7085 p_image , (HEIGHT , WIDTH ), method = tf .image .ResizeMethod .AREA
0 commit comments