61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
import os
|
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
from util.flask import not_found_json_response, error_json_response
|
|
from workflow.video import get_video_real_duration, duration_str_to_float
|
|
from model import db
|
|
from model.VideoClip import VideoClip
|
|
|
|
blueprint = Blueprint("api_video_clip", __name__, url_prefix="/api/video_clip")
|
|
|
|
|
|
@blueprint.get("/<int:video_clip_id>")
|
|
def get_video_clip_info(video_clip_id):
|
|
video_clip = VideoClip.query.get(video_clip_id)
|
|
if video_clip is None:
|
|
return not_found_json_response(id=video_clip_id)
|
|
return jsonify(video_clip.to_json())
|
|
|
|
|
|
@blueprint.put("/<int:video_clip_id>")
|
|
def modify_video_clip(video_clip_id):
|
|
video_clip = VideoClip.query.get(video_clip_id)
|
|
if video_clip is None:
|
|
return not_found_json_response(id=video_clip_id)
|
|
payload = request.json
|
|
if "workflow_id" in payload:
|
|
try:
|
|
workflow_id = int(payload["workflow_id"])
|
|
video_clip.workflow_id = workflow_id
|
|
except ValueError:
|
|
return error_json_response("workflow_id is not a int", workflow_id=payload["workflow_id"])
|
|
if "base_path" in payload:
|
|
if not os.path.isdir(payload["base_path"]):
|
|
return error_json_response("base_path is not a dir", base_path=payload["base_path"])
|
|
video_clip.base_path = payload["base_path"]
|
|
if "file" in payload:
|
|
if os.path.isabs(payload["file"]):
|
|
video_clip.base_path = ""
|
|
video_clip.file = payload["file"]
|
|
# file exist check
|
|
if not os.path.isfile(video_clip.full_path):
|
|
return error_json_response("file not exist", full_path=video_clip.full_path)
|
|
# update duration
|
|
duration = duration_str_to_float(get_video_real_duration(video_clip.full_path))
|
|
video_clip.duration = duration
|
|
db.session.commit()
|
|
return jsonify(video_clip.to_json())
|
|
|
|
|
|
@blueprint.delete("/<int:video_clip_id>")
|
|
def delete_video_clip(video_clip_id):
|
|
video_clip = VideoClip.query.get(video_clip_id)
|
|
if video_clip is not None:
|
|
db.session.delete(video_clip)
|
|
db.session.commit()
|
|
return jsonify({
|
|
"id": video_clip_id,
|
|
"old_data": video_clip
|
|
})
|