From ce3d65455d89b434270c51919a84a3f02fc581f9 Mon Sep 17 00:00:00 2001
From: Jerry Yan <792602257@qq.com>
Date: Mon, 18 Apr 2022 09:17:05 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B7=A5=E4=BD=9C=E6=B5=81=E9=87=8D=E5=81=9A?=
 =?UTF-8?q?=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 controller/api/workflow_blueprint.py | 51 +++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 8 deletions(-)

diff --git a/controller/api/workflow_blueprint.py b/controller/api/workflow_blueprint.py
index 63a2e55..d37e87e 100644
--- a/controller/api/workflow_blueprint.py
+++ b/controller/api/workflow_blueprint.py
@@ -1,6 +1,9 @@
+import threading
+
 from flask import Blueprint, jsonify
 
 from model.Workflow import Workflow
+from worker.danmaku import do_workflow
 from model import db
 
 blueprint = Blueprint("api_workflow", __name__, url_prefix="/api/workflow")
@@ -18,15 +21,47 @@ def get_workflow_info(workflow_id):
     return jsonify(workflow)
 
 
-@blueprint.put("/<int:workflow_id>/done")
-def done_editing(workflow_id):
+@blueprint.post("/<int:workflow_id>/edit")
+def start_editing(workflow_id):
     workflow = Workflow.get(workflow_id)
+    if workflow is None:
+        response = jsonify({
+            "message": "Not Found"
+        })
+        response.status_code = 404
+        return response
+    workflow.editing = True
+    db.session.commit()
     return jsonify(workflow.to_dict())
 
 
-@blueprint.post("/<int:workflow_id>/queue")
-def add_to_queue(workflow_id):
-    # JOB_QUEUE.put(workflow_item)
-    return jsonify({
-        'id': workflow_id,
-    })
+@blueprint.delete("/<int:workflow_id>/edit")
+def done_editing(workflow_id):
+    workflow = Workflow.get(workflow_id)
+    if workflow is None:
+        response = jsonify({
+            "message": "Not Found"
+        })
+        response.status_code = 404
+        return response
+    workflow.editing = False
+    db.session.commit()
+    return jsonify(workflow.to_dict())
+
+
+@blueprint.post("/<int:workflow_id>/do")
+def do_workflow(workflow_id):
+    workflow = Workflow.get(workflow_id)
+    if workflow is None:
+        response = jsonify({
+            "message": "Not Found"
+        })
+        response.status_code = 404
+        return response
+    if len(workflow.video_clips) > 0 and len(workflow.danmaku_clips) > 0:
+        threading.Thread(target=do_workflow, args=(
+            workflow.video_clips[0].full_path,
+            workflow.danmaku_clips[0].full_path,
+            *[clip.full_path for clip in workflow.danmaku_clips[1:]]
+        )).start()
+    return jsonify(workflow.to_dict())