import os.path

from flask import request, jsonify, Blueprint, g
import uuid
import time
from flask_app.ConnectionLimiter import require_execution_timeout
from flask_app.general.format_change import download_file
from flask_app.general.读取文件.按页读取pdf import read_pdf_main
from flask_app.routes.utils import validate_and_setup_logger
from flask_app.routes.货物标解析main import preprocess_files


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'})

        # 生成唯一文件名
        filename = os.path.join(output_folder,'ztbfile.pdf')
        file_path,file_type=download_file(file_url, filename)
        # print(file_path)
        # 调用预处理函数
        result = read_pdf_main(pdf_path=file_path)

        # 处理结果
        if not result:
            return jsonify({'error': 'File processing failed'})
        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)}'})
