Skip to content

Commit

Permalink
Check image 0 bytes and give 400 if invalid request
Browse files Browse the repository at this point in the history
  • Loading branch information
TimKoornstra committed Apr 16, 2024
1 parent ad30381 commit 9db010c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/api/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ def extract_request_data() -> Tuple[bytes, str, str, str, list]:

image_content = image_file.read()

# Check if the image content is empty or None
if image_content is None or len(image_content) == 0:
raise ValueError(
"The uploaded image is empty. Please upload a valid image file.")

# Extract other form data
group_id = request.form.get('group_id')
if not group_id:
Expand Down
14 changes: 13 additions & 1 deletion src/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ def predict() -> flask.Response:

# Add incoming request to queue
# Here, we're just queuing the raw data.
image_file, group_id, identifier, model, whitelist = extract_request_data()
try:
image_file, group_id, identifier, model, whitelist = extract_request_data()
except ValueError as e:
response = jsonify({
"status": "error",
"code": 400,
"message": str(e),
"timestamp": datetime.datetime.now().isoformat()
})

response.status_code = 400
logger.error("Error processing request: %s", str(e))
return response

logger.debug("Data received: %s, %s", group_id, identifier)
logger.debug("Adding %s to queue", identifier)
Expand Down

0 comments on commit 9db010c

Please sign in to comment.