x
This commit is contained in:
@@ -5,6 +5,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
|
||||
from services.auth_service import get_current_active_user
|
||||
from services.redis_task_manager import redis_task_manager
|
||||
from services.processing_service import processing_service
|
||||
from models.database import User
|
||||
from utils.logger import get_logger
|
||||
|
||||
@@ -281,6 +282,7 @@ async def export_mold_results(
|
||||
):
|
||||
body = await request.json()
|
||||
task_id = body.get("task_id")
|
||||
scheme_id = body.get("scheme_id")
|
||||
formats = body.get("formats", ["step", "stl"])
|
||||
components = body.get("components", ["cavity", "core"])
|
||||
|
||||
@@ -291,16 +293,17 @@ async def export_mold_results(
|
||||
if not task_data:
|
||||
raise HTTPException(404, "任务不存在")
|
||||
|
||||
cavity_shapes = task_data.get("cavity_shapes")
|
||||
cavity_shapes = processing_service.get_export_shapes(
|
||||
task_id,
|
||||
scheme_id or task_data.get("best_scheme_id"),
|
||||
)
|
||||
filename = task_data.get("filename", f"mold_{task_id}")
|
||||
|
||||
if not cavity_shapes:
|
||||
file_path = task_data.get("file_path")
|
||||
if file_path and os.path.exists(str(file_path)):
|
||||
cavity_shapes = await _reparse_stp_for_export(str(file_path), task_data.get("material", "ABS"))
|
||||
|
||||
if not cavity_shapes:
|
||||
raise HTTPException(400, "该任务尚未完成模具生成或形状数据不可用")
|
||||
raise HTTPException(
|
||||
409,
|
||||
"导出缓存已失效或任务尚未完成,请重新分析后再导出以保证方案一致性",
|
||||
)
|
||||
|
||||
exporter = _get_cached("cad_exporter")
|
||||
if not exporter:
|
||||
@@ -352,17 +355,3 @@ async def get_export_recommendations(
|
||||
return {"status": "success", "data": result}
|
||||
|
||||
|
||||
async def _reparse_stp_for_export(file_path: str, material: str = "ABS") -> dict:
|
||||
try:
|
||||
from core.stp_parser import STPParser
|
||||
from core.mold_generator import MoldCavityGenerator
|
||||
stp_parser = STPParser()
|
||||
shape = stp_parser.load_step_file(Path(file_path))
|
||||
mold_gen = MoldCavityGenerator(shrinkage_rate=0.005)
|
||||
mold_gen.set_material(material)
|
||||
cavity_result = mold_gen.generate_mold_cavities(shape)
|
||||
logger.info(f"重新解析 STP 用于导出: {file_path}")
|
||||
return cavity_result
|
||||
except Exception as e:
|
||||
logger.warning(f"重新解析 STP 导出失败: {e}")
|
||||
return None
|
||||
|
||||
+34
-10
@@ -1,6 +1,5 @@
|
||||
# api/v1/upload_router.py
|
||||
from fastapi import APIRouter, UploadFile, File, HTTPException, BackgroundTasks, Depends
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, UploadFile, File, HTTPException, BackgroundTasks, Depends, Form
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
@@ -27,14 +26,25 @@ file_handler = FileHandler()
|
||||
async def upload_stp(
|
||||
background_tasks: BackgroundTasks,
|
||||
file: UploadFile = File(...),
|
||||
material: Optional[str] = "ABS",
|
||||
material: str = Form(...),
|
||||
draft_angle: float = Form(...),
|
||||
shrinkage_rate: float = Form(...),
|
||||
parting_precision: float = Form(...),
|
||||
cavity_match: int = Form(...),
|
||||
db_session: AsyncSession = Depends(get_db_session),
|
||||
current_user: User = Depends(get_current_active_user)
|
||||
):
|
||||
"""上传STP文件并存储到数据库"""
|
||||
process_params = {
|
||||
"material": material,
|
||||
"draft_angle": float(draft_angle),
|
||||
"shrinkage_rate": float(shrinkage_rate),
|
||||
"parting_precision": float(parting_precision),
|
||||
"cavity_match": int(cavity_match),
|
||||
}
|
||||
logger.info(
|
||||
f"[UPLOAD] 用户={current_user.username}(id={current_user.id}) "
|
||||
f"文件={file.filename} 材料={material} "
|
||||
f"文件={file.filename} 参数={process_params} "
|
||||
f"大小={file.size if hasattr(file, 'size') else 'unknown'}"
|
||||
)
|
||||
|
||||
@@ -44,7 +54,11 @@ async def upload_stp(
|
||||
|
||||
task_id = str(uuid.uuid4())
|
||||
|
||||
file_path, file_size = await file_handler.save_uploaded_file(file)
|
||||
try:
|
||||
file_path, file_size, file_meta = await file_handler.save_uploaded_file(file)
|
||||
except ValueError as exc:
|
||||
logger.warning(f"[UPLOAD] 拒绝非法文件: {file.filename}, 原因={exc}")
|
||||
raise HTTPException(400, str(exc)) from exc
|
||||
logger.info(f"[UPLOAD] 文件已保存: {file_path} ({file_size} bytes), task_id={task_id}")
|
||||
|
||||
storage_service = StorageIntegrationService()
|
||||
@@ -52,12 +66,17 @@ async def upload_stp(
|
||||
stp_file = await storage_service.save_stp_file(
|
||||
session=db_session,
|
||||
file_path=file_path,
|
||||
original_filename=file.filename,
|
||||
original_filename=file_meta["safe_original_name"],
|
||||
user_id=current_user.id
|
||||
)
|
||||
logger.info(f"[UPLOAD] STP文件已存入RustFS+PG: stp_file.id={stp_file.id}")
|
||||
|
||||
await storage_service.create_processing_task(db_session, task_id, stp_file.id)
|
||||
await storage_service.create_processing_task(
|
||||
db_session,
|
||||
task_id,
|
||||
stp_file.id,
|
||||
parameters=process_params,
|
||||
)
|
||||
|
||||
task_info = create_task_info(
|
||||
task_id=task_id,
|
||||
@@ -67,11 +86,14 @@ async def upload_stp(
|
||||
file_size=file_size,
|
||||
upload_time=str(datetime.now())
|
||||
)
|
||||
task_info["material"] = material
|
||||
task_info["parameters"] = process_params
|
||||
task_info["file_hash"] = file_meta["sha256"]
|
||||
await redis_task_manager.set_task(task_id, task_info)
|
||||
|
||||
background_tasks.add_task(
|
||||
processing_service.process_file_with_storage,
|
||||
task_id, file_path, stp_file.id, material
|
||||
task_id, file_path, stp_file.id, process_params
|
||||
)
|
||||
logger.info(f"[UPLOAD] 后台处理已调度: task_id={task_id}")
|
||||
|
||||
@@ -83,6 +105,8 @@ async def upload_stp(
|
||||
"filename": file.filename,
|
||||
"size": file_size,
|
||||
"pythonocc_available": True,
|
||||
"database_file_id": stp_file.id
|
||||
}
|
||||
"database_file_id": stp_file.id,
|
||||
"sha256": file_meta["sha256"],
|
||||
},
|
||||
"parameters": process_params,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user