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

+1 -1
View File
@@ -25,7 +25,7 @@
<fieldset id="uploadFields">
<div class="mb-3">
<label class="form-label" for="videoName">Video Name</label>
<input class="form-control" type="text" id="videoName" name="name" required>
<input class="form-control" type="text" id="videoName" name="name" maxlength="120" required>
</div>
<div class="mb-3">
<label class="form-label" for="videoFile">Video File</label>
+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_VIDEO_SIZE = 5 * (1024 ** 3)
@@ -36,19 +38,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 video.'), 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 video file.'), 400
video = files[0]
extension = Path(video.filename).suffix.lower()
if extension not in VIDEO_EXTENSIONS:
return jsonify(status='FAILED', error='Please select a supported video file.'), 400
storage = (Path(current_app.config['CONFIG_DIRECTORY']) /
current_app.config['config'].get('youtube_download_location', 'Videos')).resolve()
try:
storage = (Path(current_app.config['CONFIG_DIRECTORY']) /
current_app.config['config'].get('youtube_download_location', 'Videos')).resolve()
except (OSError, RuntimeError):
current_app.logger.exception('Unable to resolve video storage location')
return jsonify(status='FAILED', error='Unable to access video storage. Please contact the administrator.'), 500
# Never use the media name or client filename as a storage path.
token = uuid4().hex
destination = storage / f'upload-{token}{extension}'
@@ -65,10 +72,19 @@ def upload():
output.write(chunk)
if size == 0:
return jsonify(status='FAILED', error='The video file is empty.'), 400
partial.rename(destination)
get_db().insert('media', ['media_name', 'media_uri'], [name, str(destination)])
validate_upload_content(partial, 'video', 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('Video validation unavailable: %s', error)
return jsonify(status='FAILED', error=str(error)), 503
except (OSError, sqlite3.Error):
current_app.logger.exception('Unable to save uploaded video')
return jsonify(status='FAILED', error='Unable to save the video. Please try again.'), 500