You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.7 KiB
129 lines
3.7 KiB
import os
|
|
import uuid
|
|
import threading
|
|
from flask import Flask, request, jsonify
|
|
from oss2 import Auth, Bucket
|
|
from A1_GenerateMarkdown import *
|
|
from A2_MarkdownToPptx import *
|
|
from A3_MarkdownToDocx import *
|
|
|
|
app = Flask(__name__)
|
|
|
|
# 内存存储任务状态
|
|
tasks = {}
|
|
# OSS配置
|
|
OSS_ACCESS_KEY = 'LTAI5t5jxkgJtRK8wew8fnbq'
|
|
OSS_SECRET_KEY = 'b8HXNGz7IkI3Dhv7BZx9BNBEZy1uku'
|
|
OSS_ENDPOINT = 'oss-cn-hangzhou.aliyuncs.com'
|
|
OSS_BUCKET_NAME = 'ylt'
|
|
# 或者更明确的写法(推荐)
|
|
OSS_PREFIX = f'https://{OSS_BUCKET_NAME}.{OSS_ENDPOINT.replace("https://", "")}'
|
|
|
|
# 初始化OSS客户端
|
|
oss_auth = Auth(OSS_ACCESS_KEY, OSS_SECRET_KEY)
|
|
oss_bucket = Bucket(oss_auth, OSS_ENDPOINT, OSS_BUCKET_NAME)
|
|
|
|
|
|
def async_generate_task(guid, course_name):
|
|
"""异步生成任务"""
|
|
try:
|
|
tasks[guid] = {'status': 'processing', 'files': {}}
|
|
|
|
# 生成Markdown
|
|
md_path = mdWorkingPath / f"{guid}.md"
|
|
generate_document(course_name, md_path)
|
|
|
|
# 生成PPTX
|
|
pptx_path = Path(f"/tmp/{guid}.pptx")
|
|
generate("通用", str(pptx_path), str(md_path))
|
|
pptx_url = upload_to_oss(pptx_path, f"HuangHai/pptx/{guid}.pptx")
|
|
tasks[guid]['files']['pptx'] = pptx_url
|
|
|
|
# 生成DOCX
|
|
docx_path = Path(f"/tmp/{guid}.docx")
|
|
convert_md_to_docx(
|
|
input_md=str(md_path),
|
|
output_docx=str(docx_path),
|
|
reference_template=Path(__file__).parent / "left-aligned-template.docx"
|
|
)
|
|
docx_url = upload_to_oss(docx_path, f"docx/{guid}.docx")
|
|
tasks[guid]['files']['docx'] = docx_url
|
|
|
|
# 清理临时文件
|
|
os.remove(md_path)
|
|
os.remove(pptx_path)
|
|
os.remove(docx_path)
|
|
|
|
tasks[guid]['status'] = 'completed'
|
|
except Exception as e:
|
|
tasks[guid]['status'] = 'failed'
|
|
tasks[guid]['error'] = str(e)
|
|
|
|
|
|
def upload_to_oss(local_path, object_name):
|
|
"""上传文件到OSS"""
|
|
# 添加路径格式校验
|
|
if object_name.startswith('/'):
|
|
object_name = object_name[1:]
|
|
oss_bucket.put_object_from_file(object_name, str(local_path))
|
|
return f"{OSS_PREFIX}/{object_name}"
|
|
|
|
|
|
@app.route('/submit', methods=['POST'])
|
|
def submit_task():
|
|
"""提交生成任务接口"""
|
|
course_name = request.json.get('course_name')
|
|
if not course_name:
|
|
return jsonify({'code': 400, 'msg': '课程名称不能为空'}), 400
|
|
|
|
guid = str(uuid.uuid4())
|
|
tasks[guid] = {'status': 'pending'} # 初始状态
|
|
|
|
# 启动异步任务
|
|
thread = threading.Thread(
|
|
target=async_generate_task,
|
|
args=(guid, course_name)
|
|
)
|
|
thread.start()
|
|
|
|
return jsonify({
|
|
'code': 0,
|
|
'msg': '任务已提交',
|
|
'task_id': guid
|
|
})
|
|
|
|
|
|
@app.route('/check/<guid>')
|
|
def check_task(guid):
|
|
"""查询任务状态接口"""
|
|
task = tasks.get(guid)
|
|
if not task:
|
|
return jsonify({'code': 404, 'msg': '任务不存在'}), 404
|
|
|
|
if task['status'] == 'completed':
|
|
return jsonify({
|
|
'code': 0,
|
|
'status': 'completed',
|
|
'files': task['files']
|
|
})
|
|
elif task['status'] == 'failed':
|
|
return jsonify({
|
|
'code': 500,
|
|
'status': 'failed',
|
|
'error': task.get('error', '未知错误')
|
|
})
|
|
else:
|
|
return jsonify({
|
|
'code': 0,
|
|
'status': 'processing',
|
|
'msg': '生成进行中,请稍后查询'
|
|
})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=6000, threaded=True)
|
|
|
|
'''
|
|
curl -X POST http://10.10.21.20:6000/submit -H "Content-Type: application/json" -d '{"course_name": "小学数学三角形内角和"}'
|
|
'''
|