2025-02-17 16:24:43 +08:00
|
|
|
import multiprocessing
|
2025-02-17 12:34:47 +08:00
|
|
|
import os.path
|
2025-02-17 11:30:11 +08:00
|
|
|
from flask import request, jsonify, Blueprint, g
|
|
|
|
from flask_app.ConnectionLimiter import require_execution_timeout
|
|
|
|
from flask_app.general.format_change import download_file
|
2025-02-17 16:10:38 +08:00
|
|
|
from flask_app.general.读取文件.按页读取pdf import read_pdf_main
|
2025-02-17 11:30:11 +08:00
|
|
|
from flask_app.routes.utils import validate_and_setup_logger
|
|
|
|
|
|
|
|
test_readpdf_bp = Blueprint('test_readpdf', __name__)
|
|
|
|
@test_readpdf_bp.route('/test_readpdf', methods=['POST'])
|
|
|
|
@validate_and_setup_logger
|
|
|
|
@require_execution_timeout(timeout=1800)
|
|
|
|
def process_file():
|
|
|
|
logger = g.logger
|
|
|
|
output_folder = g.output_folder
|
|
|
|
try:
|
|
|
|
# 解析请求参数
|
|
|
|
data = request.json
|
|
|
|
file_url = data.get('file_url')
|
|
|
|
# 参数校验
|
|
|
|
if not file_url:
|
|
|
|
return jsonify({'error': 'Missing file_url parameter'})
|
|
|
|
|
|
|
|
# 生成唯一文件名
|
2025-02-17 12:34:47 +08:00
|
|
|
filename = os.path.join(output_folder,'ztbfile.pdf')
|
2025-02-17 11:30:11 +08:00
|
|
|
file_path,file_type=download_file(file_url, filename)
|
2025-02-17 14:46:46 +08:00
|
|
|
# print(file_path)
|
|
|
|
# 调用预处理函数
|
2025-02-17 16:24:43 +08:00
|
|
|
with multiprocessing.Pool(processes=1) as pool:
|
|
|
|
# 调用 apply 或 apply_async 执行子进程任务
|
|
|
|
result = pool.apply(read_pdf_main, args=(file_path,))
|
2025-02-17 16:10:38 +08:00
|
|
|
|
2025-02-17 14:46:46 +08:00
|
|
|
# 处理结果
|
2025-02-17 16:10:38 +08:00
|
|
|
if not result:
|
|
|
|
return jsonify({'error': 'File processing failed'})
|
2025-02-17 11:30:11 +08:00
|
|
|
response_data={
|
|
|
|
"处理结果":"yes"
|
|
|
|
}
|
|
|
|
return jsonify(response_data)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
# app.logger.error(f"Processing error: {str(e)}")
|
|
|
|
return jsonify({'error': f'Internal server error: {str(e)}'})
|