# Copyright (C) 2026 Mattia Zatelli # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ Endpoints for BIM project management These endpoints handle the complete lifecycle of BIM projects: - IFC file upload with validation - Processing status monitoring - Project metadata management - Access to processed information (elements, spaces, quantities) All endpoints are protected by authentication and isolated per tenant. """ import logging import hashlib from typing import List from fastapi import APIRouter, Depends, File, UploadFile, Form, HTTPException, Query, status, Request, BackgroundTasks from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select, and_, or_ from sqlalchemy.orm import selectinload from app.core.database import get_async_session from app.models import Project, ProjectStatus from app.schemas.project import ( ProjectUploadRequest, ProjectUploadResponse, ProjectStatusResponse, ProjectSummaryResponse, ProjectListRequest, ProjectListResponse, ProjectQuantitiesResponse, ProjectSpacesResponse, UploadInitRequest, UploadInitResponse, FinalizeUploadResponse, ) from app.schemas.common import ErrorResponse, SuccessResponse from app.core.exceptions import ProjectNotFoundError, FileUploadError from app.core.s3 import S3StorageService, get_s3_client from app.core.config import get_settings from app.core.redis import RedisStream logger = logging.getLogger(__name__) settings = get_settings() router = APIRouter() async def get_project_or_404( project_id: str, session: AsyncSession, tenant_id: str ) -> Project: """Retrieve project by ID or raise 404 error Utility function to retrieve projects with validation: - Converts external ID (prj_123) to internal ID - Verifies tenant ownership for data isolation - Raises ProjectNotFoundError if not found Args: project_id: External project ID format (e.g. prj_123) session: Async database session tenant_id: Tenant ID for data isolation Returns: Project: Found project instance Raises: ProjectNotFoundError: If project not found or not accessible """ # Convert external ID to internal numeric ID try: internal_id = int(project_id.replace('prj_', '')) except ValueError: raise ProjectNotFoundError(project_id) # Query project with multi-tenant security filters query = select(Project).where( and_( Project.id == internal_id, # Filter by specific ID Project.tenant_id == tenant_id # IMPORTANT: tenant isolation ) ) result = await session.execute(query) project = result.scalar_one_or_none() if not project: raise ProjectNotFoundError(project_id) return project @router.post("/upload", response_model=ProjectUploadResponse) async def upload_project( request: Request, background_tasks: BackgroundTasks, file: UploadFile = File(...), project_name: str = Form(None), description: str = Form(None), region: str = Form("us"), session: AsyncSession = Depends(get_async_session) ): """ Upload IFC file for processing This endpoint handles IFC file upload with full validation: **Validations performed:** - File extension (.ifc, .ifczip) - Maximum size (500MB) - Non-empty file - SHA256 hash to detect duplicates **Process:** 1. File and metadata validation 2. Upload to S3 storage 3. Database record creation 4. Start async processing (Celery) 5. Immediate return with project ID **Async processing:** - IFC parsing with ifcopenshell - Element and metadata extraction - Geometry and property calculation - Query index generation Args: file: IFC file to process project_name: Optional project name description: Optional description region: Region for local compliance Returns: ProjectUploadResponse: Project ID and initial status Raises: FileUploadError: If file is invalid QuotaExceededError: If tenant limits exceeded """ # Retrieve authentication info from middleware tenant_id = getattr(request.state, "tenant_id", "unknown") api_key_hash = getattr(request.state, "api_key_hash", None) request_id = getattr(request.state, "request_id", "unknown") logger.info(f"Starting project upload for tenant {tenant_id}: {file.filename}") # === FILE VALIDATION === # Check filename presence if not file.filename: raise FileUploadError("Missing filename") # Sanitize filename: reject null bytes, path separators, excessive length if '\x00' in file.filename or '/' in file.filename or '\\' in file.filename: raise FileUploadError("Invalid filename") if len(file.filename) > 255: raise FileUploadError("Filename too long (max 255 characters)") # Check supported file extension using pathlib for safe extraction from pathlib import PurePosixPath file_ext = PurePosixPath(file.filename).suffix.lower() allowed_extensions = settings.ALLOWED_IFC_EXTENSIONS if file_ext not in allowed_extensions: raise FileUploadError(f"Unsupported file type. Supported: {', '.join(allowed_extensions)}") # Check file size max_size = settings.MAX_UPLOAD_SIZE file.file.seek(0, 2) # Move pointer to end file_size = file.file.tell() file.file.seek(0) # Back to beginning if file_size > max_size: from fastapi import status raise FileUploadError( f"File too large (maximum {max_size // (1024 * 1024)}MB)", status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, ) if file_size == 0: raise FileUploadError("Empty file not allowed") # Validate IFC magic bytes (IFC files start with "ISO-10303-21") magic_header = file.file.read(64) file.file.seek(0) if file_ext == '.ifc' and not magic_header.strip().startswith(b'ISO-10303-21'): raise FileUploadError("File content does not match IFC format") try: # Generate project ID and prepare for upload project_name = project_name or file.filename.rsplit('.', 1)[0] # Calculate file hash before creating project record file.file.seek(0) # Reset to beginning file_content = file.file.read() file.file.seek(0) # Reset for later use file_hash = hashlib.sha256(file_content).hexdigest() actual_size = len(file_content) # Check if a project with this file hash already exists for this tenant existing_query = select(Project).where( and_( Project.tenant_id == tenant_id, Project.file_hash == file_hash ) ) existing_result = await session.execute(existing_query) existing_project = existing_result.scalar_one_or_none() if existing_project: # File already exists - return the existing project info logger.info(f"File with hash {file_hash[:8]} already exists for tenant {tenant_id} as project {existing_project.id}") # Touch last access for the existing project existing_project.touch_last_access() await session.commit() project_public_id = f"prj_{existing_project.id}" return ProjectUploadResponse( project_id=project_public_id, status=existing_project.status, message=f"File already exists as project {project_public_id}", file_info={ "filename": file.filename, "size": actual_size, "content_type": file.content_type or "application/octet-stream", "checksum": file_hash }, estimated_processing_time=0 if existing_project.is_ready() else 300 ) # Generate S3 object key using UUID (doesn't depend on DB ID) import uuid file_uuid = str(uuid.uuid4()) object_key = f"projects/{file_uuid}/files/{file.filename}" # Create project record with pre-calculated S3 key project = Project( tenant_id=tenant_id, api_key_hash=api_key_hash, name=project_name, description=description, original_filename=file.filename, file_size=actual_size, file_hash=file_hash, status=ProjectStatus.QUEUED, s3_bucket="bim-files", # From config s3_key=object_key, # Set the S3 key before database insertion s3_region=region ) session.add(project) await session.flush() # Get the ID await session.refresh(project) project_public_id = f"prj_{project.id}" # Upload file content to S3. `settings` is already imported at # the top of the module; redefining it locally turned every # earlier reference (allowed_extensions, max_size) into an # UnboundLocalError because Python promoted `settings` to a # function-local name for the whole frame. s3_client = get_s3_client() s3_client.put_object( Bucket=settings.S3_BUCKET, Key=object_key, Body=file_content, ContentType=file.content_type or "application/octet-stream", Metadata={ 'project-id': project_public_id, 'filename': file.filename, 'sha256': file_hash } ) # Enqueue processing task (using Celery directly) from app.tasks.ifc_processing import process_ifc_file task_data = { "project_id": project_public_id, "tenant_id": tenant_id, "s3_bucket": project.s3_bucket, "s3_key": object_key, "filename": file.filename, "file_size": actual_size, "file_hash": file_hash, "request_id": request_id } try: task_result = process_ifc_file.delay(task_data) logger.info(f"Started IFC processing task {task_result.id} for project {project_public_id}") except Exception as e: logger.error(f"Failed to start IFC processing task: {e}") # Continue anyway - project is uploaded await session.commit() logger.info(f"Project {project_public_id} uploaded successfully") return ProjectUploadResponse( project_id=project_public_id, status=ProjectStatus.QUEUED, message="File uploaded successfully, processing started", file_info={ "filename": file.filename, "size": actual_size, "content_type": file.content_type or "application/octet-stream", "checksum": file_hash }, estimated_processing_time=300 # 5 minutes estimate ) except Exception as e: await session.rollback() logger.error(f"Upload failed for tenant {tenant_id}: {e}") if isinstance(e, (FileUploadError, HTTPException)): raise raise FileUploadError("Upload failed. Please try again.") # --------------------------------------------------------------------------- # Presigned-upload flow (direct-to-R2) # # The proxied /upload endpoint above is limited by the request body size of # whatever sits between the client and us (Vercel hobby tier caps at ~4.5 # MB). For real-world IFC files this is too tight. The two endpoints below # implement an S3-style two-step upload: # # 1. POST /projects/upload-init -> returns a presigned PUT URL for R2 # The client uploads the file straight to R2; the bytes never touch # our API or any proxy. # 2. POST /projects/{id}/finalize -> the client tells us it's done, we # head_object to verify and enqueue the Celery processing task. # --------------------------------------------------------------------------- @router.post("/upload-init", response_model=UploadInitResponse) async def init_upload( body: UploadInitRequest, request: Request, session: AsyncSession = Depends(get_async_session), ): """Validate metadata, create a placeholder project row, and return a presigned PUT URL the client uses to upload the IFC directly to R2. """ tenant_id = getattr(request.state, "tenant_id", "unknown") api_key_hash = getattr(request.state, "api_key_hash", None) # Validate filename if not body.filename or '\x00' in body.filename or '/' in body.filename or '\\' in body.filename: raise FileUploadError("Invalid filename") if len(body.filename) > 255: raise FileUploadError("Filename too long (max 255 characters)") from pathlib import PurePosixPath file_ext = PurePosixPath(body.filename).suffix.lower() if file_ext not in settings.ALLOWED_IFC_EXTENSIONS: raise FileUploadError( f"Unsupported file type. Supported: {', '.join(settings.ALLOWED_IFC_EXTENSIONS)}" ) # Validate size against the configured cap if body.file_size > settings.MAX_UPLOAD_SIZE: from fastapi import status as http_status raise FileUploadError( f"File too large (maximum {settings.MAX_UPLOAD_SIZE // (1024 * 1024)}MB)", status_code=http_status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, ) # Generate a fresh S3 key import uuid file_uuid = str(uuid.uuid4()) object_key = f"projects/{file_uuid}/files/{body.filename}" # Insert placeholder project row. file_hash is filled in finalize(). project = Project( tenant_id=tenant_id, api_key_hash=api_key_hash, name=body.project_name or body.filename, description=body.description, original_filename=body.filename, file_size=body.file_size, file_hash="", # populated in /finalize after head_object status=ProjectStatus.QUEUED, s3_bucket=settings.S3_BUCKET, s3_key=object_key, s3_region=body.region or settings.S3_REGION, ) session.add(project) await session.commit() await session.refresh(project) project_public_id = f"prj_{project.id}" # Mint the presigned PUT URL. The client MUST send a matching # Content-Type header on the upload — boto3 includes the # ContentType in the signature. content_type = body.content_type or "application/octet-stream" s3_client = get_s3_client() upload_url = s3_client.generate_presigned_url( "put_object", Params={ "Bucket": settings.S3_BUCKET, "Key": object_key, "ContentType": content_type, }, ExpiresIn=3600, ) from datetime import datetime, timedelta, timezone expires_at = (datetime.now(timezone.utc) + timedelta(seconds=3600)).isoformat() logger.info( f"Initialised upload for tenant {tenant_id}: {body.filename} " f"({body.file_size} bytes) -> {project_public_id}" ) return UploadInitResponse( project_id=project_public_id, upload_url=upload_url, upload_method="PUT", content_type=content_type, expires_at=expires_at, ) @router.post("/{project_public_id}/finalize", response_model=FinalizeUploadResponse) async def finalize_upload( project_public_id: str, request: Request, session: AsyncSession = Depends(get_async_session), ): """Confirm a presigned upload landed in R2 and enqueue processing.""" tenant_id = getattr(request.state, "tenant_id", "unknown") request_id = getattr(request.state, "request_id", "unknown") if not project_public_id.startswith("prj_"): raise ProjectNotFoundError(project_public_id) try: project_id = int(project_public_id[4:]) except ValueError: raise ProjectNotFoundError(project_public_id) query = select(Project).where( and_(Project.id == project_id, Project.tenant_id == tenant_id) ) result = await session.execute(query) project = result.scalar_one_or_none() if not project: raise ProjectNotFoundError(project_public_id) # Verify the file actually landed in R2. s3_client = get_s3_client() try: head = s3_client.head_object(Bucket=project.s3_bucket, Key=project.s3_key) except Exception as e: logger.error(f"Finalize failed: head_object for {project.s3_key}: {e}") raise FileUploadError( "We can't see the uploaded file in storage. Did the PUT succeed?" ) actual_size = int(head.get("ContentLength", 0)) etag = head.get("ETag", "").strip('"') project.file_size = actual_size # ETag is per-storage object identifier; not a true SHA256, but unique # per object for our dedup checks. if not project.file_hash: project.file_hash = etag project.touch_last_access() # Enqueue Celery processing from app.tasks.ifc_processing import process_ifc_file task_data = { "project_id": project_public_id, "tenant_id": tenant_id, "s3_bucket": project.s3_bucket, "s3_key": project.s3_key, "filename": project.original_filename, "file_size": actual_size, "file_hash": project.file_hash, "request_id": request_id, } try: task_result = process_ifc_file.delay(task_data) logger.info(f"Queued IFC processing {task_result.id} for {project_public_id}") except Exception as e: logger.error(f"Failed to enqueue processing task for {project_public_id}: {e}") await session.commit() return FinalizeUploadResponse( project_id=project_public_id, status=project.status, file_size=actual_size, estimated_processing_time=300, ) @router.get("/{project_id}/status", response_model=ProjectStatusResponse) async def get_project_status( project_id: str, request: Request, session: AsyncSession = Depends(get_async_session) ): """ Get project processing status """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) # Touch last access for tiered storage project.touch_last_access() await session.flush() # For processing projects, check Redis for real-time progress progress_info = project.get_progress_info() if project.is_processing(): try: from app.core.redis import get_redis import json as _json redis = get_redis() cached_progress = await redis.get(f"project_progress:{project_id}") if cached_progress: real_time = _json.loads(cached_progress) progress_info["progress"]["percent"] = real_time["percent"] progress_info["progress"]["stage"] = real_time["stage"] except Exception: pass # Fall back to DB progress return ProjectStatusResponse( project_id=project_id, name=project.name, status=project.status, progress=progress_info.get("progress") if progress_info else None, error=progress_info.get("error"), ifc_schema=project.ifc_schema, ifc_version=project.ifc_version, element_count=project.element_count, created_at=project.created_at, processed_at=project.processed_at, last_queried_at=project.last_queried_at ) @router.get("/{project_id}/summary", response_model=ProjectSummaryResponse) async def get_project_summary( project_id: str, request: Request, session: AsyncSession = Depends(get_async_session) ): """ Get project summary with statistics """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) # Touch last access for tiered storage project.touch_last_access() await session.flush() if not project.is_ready(): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Project is not ready (status: {project.status})" ) # Build element statistics element_stats = None if project.element_count and project.element_count > 0: element_stats = { "total_elements": project.element_count, "by_type": project.element_types or {}, "by_level": {}, # Would need to query elements table "by_material": {}, # Would need to query elements table "with_geometry": 0, # Would need to calculate "with_properties": 0, # Would need to calculate "with_materials": 0 # Would need to calculate } # Build spatial information spatial_info = None if project.project_info: spatial_info = { "site_name": project.site_info.get("site_name") if project.site_info else None, "building_name": project.site_info.get("building_name") if project.site_info else None, "levels": [], # Would need to extract from elements "bounding_box": project.bounding_box, "gross_floor_area": { "value": project.gross_floor_area, "unit": "m2" } if project.gross_floor_area else None, "building_height": { "value": project.building_height, "unit": "m" } if project.building_height else None } return ProjectSummaryResponse( project_id=project_id, name=project.name, status=project.status, original_filename=project.original_filename, file_size=project.file_size, ifc_schema=project.ifc_schema, element_stats=element_stats, spatial_info=spatial_info, created_at=project.created_at, processed_at=project.processed_at, last_queried_at=project.last_queried_at ) @router.get("", response_model=ProjectListResponse) async def list_projects( request: Request, status_filter: ProjectStatus = None, search: str = Query(None, max_length=200), limit: int = Query(50, ge=1, le=100), cursor: str = None, session: AsyncSession = Depends(get_async_session) ): """ List projects for the authenticated tenant """ tenant_id = getattr(request.state, "tenant_id", "unknown") # Build query query = select(Project).where(Project.tenant_id == tenant_id) # Apply filters if status_filter: query = query.where(Project.status == status_filter) if search: search_term = f"%{search.lower()}%" query = query.where( or_( Project.name.ilike(search_term), Project.description.ilike(search_term), Project.original_filename.ilike(search_term) ) ) # Apply cursor pagination if cursor: try: cursor_id = int(cursor) query = query.where(Project.id > cursor_id) except ValueError: pass # Invalid cursor, ignore # Apply limit and ordering query = query.order_by(Project.created_at.desc()).limit(limit + 1) # Execute query result = await session.execute(query) projects = result.scalars().all() # Check if there are more results has_next = len(projects) > limit if has_next: projects = projects[:-1] # Remove the extra item # Generate next cursor next_cursor = None if has_next and projects: next_cursor = str(projects[-1].id) # Convert to response format project_items = [] for project in projects: project_items.append({ "project_id": f"prj_{project.id}", "name": project.name, "status": project.status, "element_count": project.element_count, "file_size": project.file_size, "created_at": project.created_at, "processed_at": project.processed_at, "tags": [] # Would need tags field in model }) return ProjectListResponse( projects=project_items, pagination={ "limit": limit, "cursor": cursor, "has_next": has_next, "next_cursor": next_cursor, "total_count": None # Could be expensive to calculate } ) @router.get("/{project_id}/quantities", response_model=ProjectQuantitiesResponse) async def get_project_quantities( project_id: str, request: Request, session: AsyncSession = Depends(get_async_session) ): """ Get project quantities (Bill of Quantities) """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) # Touch last access for tiered storage project.touch_last_access() await session.flush() if not project.is_ready(): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Project is not ready (status: {project.status})" ) # This would typically involve complex calculations # For now, return a basic structure return ProjectQuantitiesResponse( project_id=project_id, generated_at=project.processed_at or project.created_at, items=[ { "code": "WALLS_CONCRETE", "description": "Concrete walls", "unit": "m3", "quantity": 150.0 }, { "code": "WINDOWS", "description": "Windows", "unit": "m2", "quantity": 250.0 } ], totals={ "total_concrete": {"value": 150.0, "unit": "m3"}, "total_openings": {"value": 250.0, "unit": "m2"} }, methodology="IFC-based calculation", accuracy_level="medium" ) @router.get("/{project_id}/spaces", response_model=ProjectSpacesResponse) async def get_project_spaces( project_id: str, request: Request, session: AsyncSession = Depends(get_async_session) ): """ Get project spaces information """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) # Touch last access for tiered storage project.touch_last_access() await session.flush() if not project.is_ready(): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=f"Project is not ready (status: {project.status})" ) # This would query the elements table for spaces # For now, return a basic structure return ProjectSpacesResponse( project_id=project_id, spaces=[ { "id": "sp_1", "name": "Office A1", "usage": "office", "area_sqm": 25.5, "level": "Level 1" } ], total_spaces=1, total_area={"value": 25.5, "unit": "m2"}, by_usage={"office": 1}, by_level={"Level 1": 1} ) @router.get("/{project_id}/download") async def get_project_download_url( project_id: str, request: Request, session: AsyncSession = Depends(get_async_session) ): """ Get a presigned URL to download the original IFC file. Used by the 3D viewer to load the model in the browser. """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) if not project.s3_key: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="File not available") url = S3StorageService.generate_presigned_url( project_id=project_id, filename=project.original_filename, expiration=3600, http_method='GET' ) return { "url": url, "filename": project.original_filename, "file_size": project.file_size, "expires_in": 3600 } @router.post("/{project_id}/reprocess", response_model=SuccessResponse) async def reprocess_project( project_id: str, request: Request, background_tasks: BackgroundTasks, session: AsyncSession = Depends(get_async_session) ): """ Reprocess a project by putting it back in the queue """ tenant_id = getattr(request.state, "tenant_id", "unknown") api_key_hash = getattr(request.state, "api_key_hash", None) request_id = getattr(request.state, "request_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) try: # Reset project status to queued project.status = ProjectStatus.QUEUED project.progress_info = None project.error_info = None project.processed_at = None # Clear processing results but keep original file info project.element_count = None project.element_types = None project.has_geometry = None project.has_properties = None project.has_materials = None project.has_spaces = None project.ifc_schema = None project.ifc_version = None project.project_info = None project.site_info = None project.bounding_box = None project.gross_floor_area = None project.building_height = None # Store values we need before any session operations s3_bucket = project.s3_bucket s3_key = project.s3_key original_filename = project.original_filename file_size = project.file_size file_hash = project.file_hash updated_at = project.updated_at # Commit the database changes first await session.commit() # Create task data for Celery task_data = { "project_id": project_id, "tenant_id": tenant_id, "s3_bucket": s3_bucket, "s3_key": s3_key, "filename": original_filename, "file_size": file_size, "file_hash": file_hash, "request_id": f"{request_id}_reprocess" } # Schedule Celery task in background to avoid async conflicts def start_celery_task(): from app.tasks.ifc_processing import process_ifc_file try: task_result = process_ifc_file.delay(task_data) logger.info(f"Started IFC processing task {task_result.id} for project {project_id}") except Exception as e: logger.error(f"Failed to start IFC processing task: {e}") background_tasks.add_task(start_celery_task) logger.info(f"Project {project_id} requeued for processing by tenant {tenant_id}") return SuccessResponse( message=f"Project {project_id} has been requeued for processing", data={ "project_id": project_id, "status": "queued", "estimated_processing_time": 300 } ) except Exception as e: await session.rollback() logger.error(f"Failed to reprocess project {project_id}: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to reprocess project" ) @router.delete("/{project_id}", response_model=SuccessResponse) async def delete_project( project_id: str, request: Request, background_tasks: BackgroundTasks, session: AsyncSession = Depends(get_async_session) ): """ Delete a project and all associated data """ tenant_id = getattr(request.state, "tenant_id", "unknown") project = await get_project_or_404(project_id, session, tenant_id) try: # Delete files from S3 if project.s3_key: background_tasks.add_task( S3StorageService.delete_file, project_id, project.original_filename ) # Delete project from database (cascade will handle related records) await session.delete(project) await session.commit() logger.info(f"Project {project_id} deleted for tenant {tenant_id}") return SuccessResponse( message=f"Project {project_id} deleted successfully", data={"project_id": project_id} ) except Exception as e: await session.rollback() logger.error(f"Failed to delete project {project_id}: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to delete project" )