|
| 1 | +from time import sleep |
| 2 | + |
1 | 3 | from models import db, Videos |
2 | 4 | from flask import ( |
3 | 5 | url_for, |
|
6 | 8 | redirect, |
7 | 9 | request, |
8 | 10 | send_from_directory, |
| 11 | + jsonify, |
9 | 12 | ) |
10 | 13 | from thegateway import thegateway_blueprint |
11 | 14 | from thegateway.mobiclip import validate_mobiclip, save_video_data, get_mobiclip_length |
12 | 15 | from thegateway.form import VideoForm |
13 | 16 | from thegateway.admin import oidc |
14 | 17 | from werkzeug.utils import redirect |
| 18 | +import threading |
| 19 | +import subprocess |
| 20 | + |
| 21 | + |
| 22 | +generate_status = { |
| 23 | + "completed": False, |
| 24 | + 'message': "", |
| 25 | + "in_progress": False, |
| 26 | +} |
15 | 27 |
|
16 | 28 |
|
17 | 29 | @thegateway_blueprint.route("/thegateway/videos/") |
@@ -77,3 +89,36 @@ def add_video(): |
77 | 89 | @oidc.require_login |
78 | 90 | def get_video_thumbnail(movie_id): |
79 | 91 | return send_from_directory("./assets/videos/", f"{movie_id}.img") |
| 92 | + |
| 93 | + |
| 94 | +@thegateway_blueprint.post("/thegateway/generate") |
| 95 | +@oidc.require_login |
| 96 | +def generate_videos(): |
| 97 | + def actually_generate_videos(): |
| 98 | + message = "Successfully generated thumbnails and videos!" |
| 99 | + # Sleep for a second to give the web app time to process the fact that another user isn't generating. |
| 100 | + sleep(1) |
| 101 | + generate_status["in_progress"] = True |
| 102 | + |
| 103 | + # Generate videos first |
| 104 | + if subprocess.run(["./cli", "2"]).returncode != 0: |
| 105 | + message = "Error generating videos." |
| 106 | + else: |
| 107 | + # Now thumbnails |
| 108 | + if subprocess.run(["./cli", "3"]).returncode != 0: |
| 109 | + message = "Error generating thumbnails." |
| 110 | + |
| 111 | + generate_status["completed"] = True |
| 112 | + generate_status["message"] = message |
| 113 | + generate_status["in_progress"] = False |
| 114 | + |
| 115 | + if not generate_status["in_progress"]: |
| 116 | + threading.Thread(target=actually_generate_videos, daemon=True).start() |
| 117 | + |
| 118 | + return jsonify(generate_status) |
| 119 | + |
| 120 | + |
| 121 | +@thegateway_blueprint.route("/thegateway/check_status") |
| 122 | +def check_status(): |
| 123 | + """Endpoint to check the current process status""" |
| 124 | + return jsonify(generate_status) |
0 commit comments