x
This commit is contained in:
+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