From 191ac77d155138682c834f5b8780a2f92319fc7a Mon Sep 17 00:00:00 2001 From: cjw <792430652@qq.com> Date: Sun, 15 Mar 2026 13:33:47 +0800 Subject: [PATCH] init --- .gitignore | 8 +++++++- config/settings.py | 8 ++++---- docker-compose.yml | 9 ++++----- src/api/routes.py | 17 ++++++++++------- src/database/init_db.py | 2 -- src/services/auth_service.py | 4 +--- src/utils/file_handler.py | 9 +++++---- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index d278616..2a62d5a 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,10 @@ __pycache__/ # 项目临时文件 .DS_Store -*.log \ No newline at end of file +*.log +.env +logs/ +uploads/ +html_output/ +src/uploads/ +src/html_output/ diff --git a/config/settings.py b/config/settings.py index bc140f0..cf62343 100644 --- a/config/settings.py +++ b/config/settings.py @@ -28,9 +28,9 @@ class Settings: self.PARALLEL_PROCESSING = os.getenv('PARALLEL_PROCESSING', 'true').lower() == 'true' # RustFS 对象存储配置 (S3v4 API) - self.RUSTFS_ENDPOINT = os.getenv('RUSTFS_ENDPOINT', 'http://localhost:8080') - self.RUSTFS_ACCESS_KEY = os.getenv('RUSTFS_ACCESS_KEY', 'your-access-key') - self.RUSTFS_SECRET_KEY = os.getenv('RUSTFS_SECRET_KEY', 'your-secret-key') + self.RUSTFS_ENDPOINT = os.getenv('RUSTFS_ENDPOINT') or os.getenv('MINIO_ENDPOINT') or 'http://localhost:8080' + self.RUSTFS_ACCESS_KEY = os.getenv('RUSTFS_ACCESS_KEY') or os.getenv('MINIO_ACCESS_KEY') or 'your-access-key' + self.RUSTFS_SECRET_KEY = os.getenv('RUSTFS_SECRET_KEY') or os.getenv('MINIO_SECRET_KEY') or 'your-secret-key' self.RUSTFS_TIMEOUT = int(os.getenv('RUSTFS_TIMEOUT', '30')) # 预签名URL过期时间(秒) @@ -99,4 +99,4 @@ class Settings: # 创建全局配置实例 -settings = Settings() \ No newline at end of file +settings = Settings() diff --git a/docker-compose.yml b/docker-compose.yml index 4fb7000..08b9b93 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -61,11 +61,10 @@ services: - DB_NAME=${DB_NAME:-moldinsight} - DB_USER=${DB_USER:-moldinsight_user} - DB_PASSWORD=${DB_PASSWORD:-moldinsight_password} - # MinIO配置 - - MINIO_ENDPOINT=minio:9000 - - MINIO_ACCESS_KEY=${MINIO_ACCESS_KEY:-minioadmin} - - MINIO_SECRET_KEY=${MINIO_SECRET_KEY:-minioadmin} - - MINIO_SECURE=false + # RustFS配置 + - RUSTFS_ENDPOINT=http://minio:9000 + - RUSTFS_ACCESS_KEY=${MINIO_ACCESS_KEY:-minioadmin} + - RUSTFS_SECRET_KEY=${MINIO_SECRET_KEY:-minioadmin} depends_on: postgres: condition: service_healthy diff --git a/src/api/routes.py b/src/api/routes.py index 169c238..882725f 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -18,6 +18,8 @@ from core.mold_generator import MoldCavityGenerator from core.aluminum_foam_mold import AluminumFoamMoldGenerator from core.mold_quality_inspector import AluminumFoamMoldQualityInspector from core.mesh_generator import MeshGenerator +from services.auth_service import get_current_active_user +from models.database import User logger = get_logger(__name__) @@ -52,7 +54,8 @@ async def upload_stp( background_tasks: BackgroundTasks, file: UploadFile = File(...), material: Optional[str] = "ABS", - db_session: AsyncSession = Depends(get_db_session) + db_session: AsyncSession = Depends(get_db_session), + current_user: User = Depends(get_current_active_user) ): """上传STP文件并存储到数据库""" @@ -62,8 +65,7 @@ async def upload_stp( task_id = str(uuid.uuid4()) # 保存文件 - file_path = await file_handler.save_uploaded_file(file) - content = await file.read() + file_path, file_size = await file_handler.save_uploaded_file(file) # 创建存储集成服务实例 storage_service = StorageIntegrationService() @@ -72,7 +74,8 @@ 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.filename, + user_id=current_user.id ) # 创建处理任务记录 @@ -84,7 +87,7 @@ async def upload_stp( status=ProcessingStatus.PROCESSING, filename=file.filename, file_path=str(file_path), - file_size=len(content), + file_size=file_size, upload_time=str(datetime.now()) ) @@ -97,7 +100,7 @@ async def upload_stp( "message": "文件上传成功,开始处理并存储到数据库", "file_info": { "filename": file.filename, - "size": len(content), + "size": file_size, "pythonocc_available": True, "database_file_id": stp_file.id } @@ -857,4 +860,4 @@ async def process_file_core( tasks[task_id]["status"] = ProcessingStatus.FAILED tasks[task_id]["error"] = str(e) - tasks[task_id]["completed_at"] = str(datetime.now()) \ No newline at end of file + tasks[task_id]["completed_at"] = str(datetime.now()) diff --git a/src/database/init_db.py b/src/database/init_db.py index c5a657d..d492287 100644 --- a/src/database/init_db.py +++ b/src/database/init_db.py @@ -134,12 +134,10 @@ async def init_database(): print("数据库初始化成功!") print("=" * 60) print(f"管理员用户名: {settings.ADMIN_USERNAME}") - print(f"管理员密码: {settings.ADMIN_PASSWORD}") print(f"管理员邮箱: {settings.ADMIN_EMAIL}") print("=" * 60) print("可以在 .env 文件中修改管理员配置:") print(" ADMIN_USERNAME") - print(" ADMIN_PASSWORD") print(" ADMIN_EMAIL") print(" ADMIN_FULL_NAME") print("=" * 60) diff --git a/src/services/auth_service.py b/src/services/auth_service.py index e06655e..23c8b3e 100644 --- a/src/services/auth_service.py +++ b/src/services/auth_service.py @@ -116,8 +116,7 @@ async def create_user( username: str, email: str, password: str, - full_name: Optional[str] = None, - is_superuser: bool = False + full_name: Optional[str] = None ) -> User: hashed_password = get_password_hash(password) user = User( @@ -125,7 +124,6 @@ async def create_user( email=email, hashed_password=hashed_password, full_name=full_name, - is_superuser=is_superuser, is_active=True ) db_session.add(user) diff --git a/src/utils/file_handler.py b/src/utils/file_handler.py index 1752fab..cf76366 100644 --- a/src/utils/file_handler.py +++ b/src/utils/file_handler.py @@ -2,6 +2,7 @@ import aiofiles from pathlib import Path from fastapi import UploadFile +from typing import Tuple class FileHandler: @@ -9,15 +10,15 @@ class FileHandler: self.upload_dir = Path(upload_dir) self.upload_dir.mkdir(exist_ok=True) - async def save_uploaded_file(self, file: UploadFile) -> Path: + async def save_uploaded_file(self, file: UploadFile) -> Tuple[Path, int]: """保存上传的文件""" file_path = self.upload_dir / file.filename + content = await file.read() async with aiofiles.open(file_path, 'wb') as f: - content = await file.read() await f.write(content) - return file_path + return file_path, len(content) def cleanup_file(self, file_path: Path): """清理文件""" @@ -25,4 +26,4 @@ class FileHandler: if file_path.exists(): file_path.unlink() except Exception as e: - print(f"文件清理失败: {e}") \ No newline at end of file + print(f"文件清理失败: {e}")