Work to add Youtube downloads, video and image uploads, and the player script
This commit is contained in:
1 parent
a3f55a8c35
commit
92d6473e1d
15 files changed
+1047
-79
No files matched your search
@@ -0,0 +1,81 @@
|
||||
from pathlib import Path
|
||||
import sqlite3
|
||||
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
|
||||
|
||||
|
||||
MAX_VIDEO_SIZE = 5 * (1024 ** 3)
|
||||
# Multipart boundaries and the name field are not part of the video file's size.
|
||||
MULTIPART_OVERHEAD = 1024 ** 2
|
||||
VIDEO_EXTENSIONS = {'.mp4', '.m4v', '.mkv', '.mov', '.webm', '.avi', '.wmv',
|
||||
'.mpeg', '.mpg', '.ts', '.mts', '.m2ts', '.3gp', '.ogv'}
|
||||
|
||||
upload_video_bp = Blueprint('upload_video', __name__, url_prefix='/upload', template_folder='.')
|
||||
|
||||
|
||||
@upload_video_bp.before_request
|
||||
def set_upload_limit():
|
||||
if request.method == 'POST':
|
||||
# Flask 3.1 supports a per-request limit, leaving other uploads unchanged.
|
||||
request.max_content_length = MAX_VIDEO_SIZE + MULTIPART_OVERHEAD
|
||||
|
||||
|
||||
@upload_video_bp.errorhandler(RequestEntityTooLarge)
|
||||
def upload_too_large(error):
|
||||
return jsonify(status='FAILED', error='The video must be no larger than 5GB.'), 413
|
||||
|
||||
|
||||
@upload_video_bp.route('/video', methods=['GET', 'POST'])
|
||||
def get_page():
|
||||
if request.method == 'GET':
|
||||
return render_template('upload_video.html', max_video_size=MAX_VIDEO_SIZE)
|
||||
return upload()
|
||||
|
||||
|
||||
def upload():
|
||||
name = request.form.get('name', '').strip()
|
||||
if not name:
|
||||
return jsonify(status='FAILED', error='Please enter a name for the video.'), 400
|
||||
files = request.files.getlist('files[]')
|
||||
if len(files) != 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()
|
||||
# Never use the media name or client filename as a storage path.
|
||||
token = uuid4().hex
|
||||
destination = storage / f'upload-{token}{extension}'
|
||||
partial = storage / f'upload-{token}.part'
|
||||
saved = False
|
||||
try:
|
||||
storage.mkdir(parents=True, exist_ok=True)
|
||||
size = 0
|
||||
with partial.open('xb') as output:
|
||||
while chunk := video.stream.read(1024 ** 2):
|
||||
size += len(chunk)
|
||||
if size > MAX_VIDEO_SIZE:
|
||||
raise RequestEntityTooLarge()
|
||||
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)])
|
||||
saved = True
|
||||
return jsonify(status='SUCCESS'), 200
|
||||
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
|
||||
finally:
|
||||
# Rejected/failed uploads must not leave partial files or uncatalogued videos.
|
||||
for path in ([partial] if saved else [partial, destination]):
|
||||
try:
|
||||
path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
current_app.logger.exception('Unable to clean up upload file %s', path)
|
||||
Reference in new issue
Block a user