Input validation work and error handling

This commit is contained in:
Bradley committed 2026-11-13 22:18:45 -05:00
1 parent 4e2520ee76
commit 554b7cc82c
20 files changed
+627 -96

No files matched your search

+24 -8
View File
@@ -5,6 +5,8 @@ from uuid import uuid4
from flask import Blueprint, request, render_template, jsonify, current_app
from werkzeug.exceptions import RequestEntityTooLarge
from utilities.tools import get_db
from utilities.validation import (ValidationError, ValidationUnavailable, ensure_name_available,
validate_media_name, validate_upload_content, write_transaction)
MAX_IMAGE_SIZE = 100 * (1024 ** 2)
@@ -33,19 +35,24 @@ def get_page():
def upload():
name = request.form.get('name', '').strip()
if not name:
return jsonify(status='FAILED', error='Please enter a name for the image.'), 400
try:
name = validate_media_name(request.form.get('name', ''))
except ValidationError as error:
return jsonify(status='FAILED', error=str(error)), 400
files = request.files.getlist('files[]')
if len(files) != 1 or not files[0].filename:
if len(files) != 1 or len(list(request.files.items(multi=True))) != 1 or not files[0].filename:
return jsonify(status='FAILED', error='Please select exactly one image file.'), 400
image = files[0]
extension = Path(image.filename).suffix.lower()
if extension not in IMAGE_EXTENSIONS:
return jsonify(status='FAILED', error='Please select a supported image file.'), 400
storage = (Path(current_app.config['CONFIG_DIRECTORY']) /
current_app.config['config'].get('image_upload_location', 'Images')).resolve()
try:
storage = (Path(current_app.config['CONFIG_DIRECTORY']) /
current_app.config['config'].get('image_upload_location', 'Images')).resolve()
except (OSError, RuntimeError):
current_app.logger.exception('Unable to resolve image storage location')
return jsonify(status='FAILED', error='Unable to access image storage. Please contact the administrator.'), 500
# Names are display metadata, never user-controlled storage paths.
token = uuid4().hex
destination = storage / f'upload-{token}{extension}'
@@ -62,10 +69,19 @@ def upload():
output.write(chunk)
if size == 0:
return jsonify(status='FAILED', error='The image file is empty.'), 400
partial.rename(destination)
get_db().insert('media', ['media_name', 'media_uri'], [name, str(destination)])
validate_upload_content(partial, 'image', extension)
db = get_db()
with write_transaction(db):
ensure_name_available(db.db, name)
partial.rename(destination)
db.insert('media', ['media_name', 'media_uri'], [name, str(destination)])
saved = True
return jsonify(status='SUCCESS'), 200
except ValidationError as error:
return jsonify(status='FAILED', error=str(error)), 400
except ValidationUnavailable as error:
current_app.logger.warning('Image validation unavailable: %s', error)
return jsonify(status='FAILED', error=str(error)), 503
except (OSError, sqlite3.Error):
current_app.logger.exception('Unable to save uploaded image')
return jsonify(status='FAILED', error='Unable to save the image. Please try again.'), 500