refactor(api): 统一响应格式迁移 + 异步任务系统 + 状态码体系完善

- 将全部路由文件(12个)的 jsonify 响应迁移至 success_response/error_response 统一格式
- 修复 sync_routes.py error_response 参数错误(P0)
- 新增异步任务系统:task_registry + task_routes
- 新增状态码:TASK_NOT_FOUND(4014)、TASK_CONFLICT(4015)、REINDEX_ERROR(5040)
- 修正 task_routes/exam_pkg 中语义不匹配的状态码
- 更新 curl 测试手册、后端对接规范文档
- 添加缓存性能报告和 Redis 迁移计划
This commit is contained in:
lacerate551
2026-06-05 22:56:00 +08:00
parent 54a6815ad4
commit 183a57e7f1
19 changed files with 2659 additions and 548 deletions

View File

@@ -11,6 +11,8 @@
import os
import logging
from flask import Blueprint, send_file, jsonify, current_app
from core.status_codes import SUCCESS, BAD_REQUEST, NOT_FOUND, INTERNAL_ERROR
from api.response_utils import success_response, error_response
logger = logging.getLogger(__name__)
@@ -51,7 +53,7 @@ def get_image(image_id: str):
# 安全检查:防止路径遍历攻击
if '..' in image_id or '/' in image_id or '\\' in image_id:
return jsonify({"error": "无效的图片 ID"}), 400
return error_response("BAD_REQUEST", BAD_REQUEST, "无效的图片 ID", http_status=400)
images_path = get_images_base_path()
@@ -74,9 +76,9 @@ def get_image(image_id: str):
return send_file(image_path, mimetype=mimetype)
except Exception as e:
logger.error(f"读取图片异常: {e}")
return jsonify({"error": "读取图片失败"}), 500
return error_response("READ_ERROR", INTERNAL_ERROR, "读取图片失败", http_status=500)
return jsonify({"error": "图片不存在", "image_id": image_id}), 404
return error_response("NOT_FOUND", NOT_FOUND, "图片不存在", http_status=404, image_id=image_id)
@image_bp.route('/images/<image_id>/info', methods=['GET'])
@@ -96,7 +98,7 @@ def get_image_info(image_id: str):
# 安全检查
if '..' in image_id or '/' in image_id or '\\' in image_id:
return jsonify({"error": "无效的图片 ID"}), 400
return error_response("BAD_REQUEST", BAD_REQUEST, "无效的图片 ID", http_status=400)
images_path = get_images_base_path()
supported_formats = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp']
@@ -109,7 +111,7 @@ def get_image_info(image_id: str):
from PIL import Image
with Image.open(image_path) as img:
return jsonify({
return success_response(data={
"image_id": image_id,
"width": img.width,
"height": img.height,
@@ -120,16 +122,16 @@ def get_image_info(image_id: str):
})
except ImportError:
# PIL 未安装,返回基本信息
return jsonify({
return success_response(data={
"image_id": image_id,
"size_bytes": os.path.getsize(image_path),
"url": f"/images/{image_id}"
})
except Exception as e:
logger.error(f"读取图片信息异常: {e}")
return jsonify({"error": "读取图片信息失败"}), 500
return error_response("READ_ERROR", INTERNAL_ERROR, "读取图片信息失败", http_status=500)
return jsonify({"error": "图片不存在", "image_id": image_id}), 404
return error_response("NOT_FOUND", NOT_FOUND, "图片不存在", http_status=404, image_id=image_id)
@image_bp.route('/images/list', methods=['GET'])
@@ -152,7 +154,7 @@ def list_images():
images_path = get_images_base_path()
if not os.path.exists(images_path):
return jsonify({"images": [], "total": 0})
return success_response(data={"images": [], "total": 0})
supported_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'}
images = []
@@ -176,7 +178,7 @@ def list_images():
total = len(images)
images = images[offset:offset + limit]
return jsonify({
return success_response(data={
"images": images,
"total": total,
"limit": limit,
@@ -185,7 +187,7 @@ def list_images():
except Exception as e:
logger.error(f"列出图片异常: {e}")
return jsonify({"error": "列出图片失败"}), 500
return error_response("LIST_ERROR", INTERNAL_ERROR, "列出图片失败", http_status=500)
@image_bp.route('/images/stats', methods=['GET'])
@@ -199,7 +201,7 @@ def image_stats():
images_path = get_images_base_path()
if not os.path.exists(images_path):
return jsonify({
return success_response(data={
"total_images": 0,
"total_size_bytes": 0,
"supported_formats": ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp']
@@ -219,7 +221,7 @@ def image_stats():
total_size += os.path.getsize(filepath)
format_counts[ext] = format_counts.get(ext, 0) + 1
return jsonify({
return success_response(data={
"total_images": total_count,
"total_size_bytes": total_size,
"total_size_mb": round(total_size / (1024 * 1024), 2),
@@ -229,4 +231,4 @@ def image_stats():
except Exception as e:
logger.error(f"获取统计信息异常: {e}")
return jsonify({"error": "获取统计信息失败"}), 500
return error_response("STATS_ERROR", INTERNAL_ERROR, "获取统计信息失败", http_status=500)