673 lines
27 KiB
Python
673 lines
27 KiB
Python
"""B03 파일 입력 FastAPI 라우터."""
|
|
|
|
import asyncio
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from uuid import UUID, uuid4
|
|
|
|
import aiomysql
|
|
from fastapi import APIRouter, File, Form, UploadFile
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from B03_FileInput.B03_FileInput_Email import (
|
|
send_analysis_completion_email,
|
|
send_analysis_error_email,
|
|
send_file_upload_complete_email,
|
|
)
|
|
from B03_FileInput.B03_FileInput_Engine import (
|
|
merge_upload_chunks,
|
|
remove_chunk_session,
|
|
resolve_chunk_session_dir,
|
|
resolve_upload_destination,
|
|
save_upload_chunk,
|
|
save_upload_stream,
|
|
)
|
|
from B03_FileInput.B03_FileInput_Engine_Analyze import analyze_input_metadata
|
|
from B03_FileInput.B03_FileInput_Repository import (
|
|
create_input_file,
|
|
create_upload_session,
|
|
get_project_storage_relative_path,
|
|
get_upload_session,
|
|
list_completed_chunk_indexes,
|
|
mark_upload_session_completed,
|
|
mark_upload_session_failed,
|
|
upsert_upload_chunk,
|
|
)
|
|
from B03_FileInput.B03_FileInput_Schema import (
|
|
ChunkSessionCreateRequest,
|
|
ChunkSessionCreateResponse,
|
|
ChunkUploadResponse,
|
|
FileUploadDescriptor,
|
|
FileUploadResponse,
|
|
UploadedFileResult,
|
|
UploadFinalizeRequest,
|
|
UploadStatusResponse,
|
|
)
|
|
from common_util.common_util_json import atomic_write_json
|
|
from common_util.common_util_storage import resolve_stored_project_path
|
|
from common_util.common_util_workflow import load_project_workflow
|
|
from common_util.common_util_workflow_state import (
|
|
complete_stage,
|
|
fail_stage,
|
|
get_workflow_state,
|
|
start_stage,
|
|
)
|
|
from config.config_db import get_db_pool
|
|
from config.config_system import (
|
|
SEND_ANALYSIS_COMPLETION_EMAIL,
|
|
SURFACE_MODEL_PRECOMPUTE,
|
|
SURFACE_MODEL_SOURCE_FILTERS,
|
|
UPLOAD_CHUNK_SIZE_BYTES,
|
|
UPLOAD_MAX_FILES,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/api/projects", tags=["B03 File Input"])
|
|
|
|
|
|
def _total_chunks(size_bytes: int, chunk_size_bytes: int) -> int:
|
|
return max(1, (size_bytes + chunk_size_bytes - 1) // chunk_size_bytes)
|
|
|
|
|
|
def _is_point_cloud_result(result: UploadedFileResult) -> bool:
|
|
return result.file_type.lower() in {"las", "laz"}
|
|
|
|
|
|
def _schedule_background_task(coro: Any, *, task_name: str) -> None:
|
|
task = asyncio.create_task(coro, name=task_name)
|
|
|
|
def _log_task_failure(completed: asyncio.Task) -> None:
|
|
try:
|
|
completed.result()
|
|
except Exception:
|
|
logger.exception("백그라운드 작업 실패: %s", task_name)
|
|
|
|
task.add_done_callback(_log_task_failure)
|
|
|
|
|
|
async def _get_project_notification_info(
|
|
connection: aiomysql.Connection,
|
|
project_id: UUID,
|
|
) -> dict[str, Any] | None:
|
|
async with connection.cursor(aiomysql.DictCursor) as cursor:
|
|
await cursor.execute(
|
|
"""
|
|
SELECT
|
|
p.id,
|
|
p.name AS project_name,
|
|
u.email AS user_email,
|
|
u.name AS user_name
|
|
FROM projects p
|
|
JOIN users u ON u.id = p.user_id
|
|
WHERE p.id = %s AND p.deleted_at IS NULL AND u.deleted_at IS NULL
|
|
""",
|
|
(str(project_id),),
|
|
)
|
|
row = await cursor.fetchone()
|
|
return dict(row) if row else None
|
|
|
|
|
|
async def _update_project_status(project_id: UUID, status: str) -> None:
|
|
pool = get_db_pool()
|
|
async with pool.acquire() as connection, connection.cursor() as cursor:
|
|
await cursor.execute(
|
|
"""
|
|
UPDATE projects
|
|
SET status = %s, updated_at = NOW()
|
|
WHERE id = %s AND deleted_at IS NULL
|
|
""",
|
|
(status, str(project_id)),
|
|
)
|
|
await connection.commit()
|
|
|
|
|
|
async def _send_upload_complete_notification(
|
|
*,
|
|
project_id: UUID,
|
|
uploaded_file: UploadedFileResult,
|
|
) -> None:
|
|
pool = get_db_pool()
|
|
async with pool.acquire() as connection:
|
|
project_info = await _get_project_notification_info(connection, project_id)
|
|
if not project_info or not project_info.get("user_email"):
|
|
logger.warning("업로드 완료 이메일 수신자 없음: project_id=%s", project_id)
|
|
return
|
|
|
|
await send_file_upload_complete_email(
|
|
to_email=str(project_info["user_email"]),
|
|
user_name=str(project_info.get("user_name") or "사용자"),
|
|
project_name=str(project_info.get("project_name") or project_id),
|
|
file_name=uploaded_file.original_filename,
|
|
file_size_mb=uploaded_file.size_bytes / (1024 * 1024),
|
|
metadata=uploaded_file.metadata,
|
|
)
|
|
|
|
|
|
async def trigger_wf1_analysis_and_email(
|
|
*,
|
|
project_id: UUID,
|
|
input_file_id: int,
|
|
) -> None:
|
|
"""WF1 분석 실행, DB 저장, 완료/오류 이메일 발송을 백그라운드에서 수행한다.
|
|
|
|
실행 흐름:
|
|
1. 프로젝트 저장 경로와 입력 파일 정보를 조회한다.
|
|
2. 무거운 WF1 분석은 워커 스레드에서 실행한다.
|
|
3. 분석 결과를 단일 DB 트랜잭션으로 저장한다.
|
|
4. 완료 이메일은 SEND_ANALYSIS_COMPLETION_EMAIL 설정이 켜진 경우에만 발송한다.
|
|
5. 분석 또는 DB 저장 실패 시 오류 이메일을 발송한다.
|
|
"""
|
|
pool = get_db_pool()
|
|
project_info: dict[str, Any] | None = None
|
|
try:
|
|
source_filters = list(SURFACE_MODEL_SOURCE_FILTERS)
|
|
methods = list(SURFACE_MODEL_PRECOMPUTE)
|
|
params = {
|
|
"input_file_id": str(input_file_id),
|
|
"source_filters": source_filters,
|
|
"methods": methods,
|
|
"force": False,
|
|
}
|
|
|
|
async with pool.acquire() as connection:
|
|
async with connection.cursor() as cursor:
|
|
await start_stage(cursor, str(project_id), 1, params)
|
|
await connection.commit()
|
|
|
|
stored_path = await get_project_storage_relative_path(connection, project_id)
|
|
project_info = await _get_project_notification_info(connection, project_id)
|
|
if not project_info or not project_info.get("user_email"):
|
|
logger.warning("WF1 분석 이메일 수신자 없음: project_id=%s", project_id)
|
|
|
|
from B04_wf1_Surface.B04_wf1_Surface_Repository import get_input_file
|
|
|
|
input_file = await get_input_file(connection, project_id, input_file_id)
|
|
|
|
project_root = Path(resolve_stored_project_path(stored_path))
|
|
las_path = project_root / Path(str(input_file["raw_file_path"]))
|
|
if not las_path.is_file():
|
|
raise FileNotFoundError("원본 LAS/LAZ 파일을 찾을 수 없습니다.")
|
|
|
|
logger.info(
|
|
"WF1 백그라운드 분석 시작: project_id=%s input_file_id=%s",
|
|
project_id,
|
|
input_file_id,
|
|
)
|
|
|
|
from B04_wf1_Surface.B04_wf1_Surface_Engine import run_surface_analysis
|
|
from B04_wf1_Surface.B04_wf1_Surface_Router import save_surface_analysis_to_db
|
|
|
|
logger.info("WF1 분석 엔진 시작: las_path=%s", las_path)
|
|
analysis_result = await asyncio.to_thread(
|
|
run_surface_analysis,
|
|
project_root,
|
|
las_path,
|
|
source_filters=source_filters,
|
|
methods=methods,
|
|
force=False,
|
|
)
|
|
logger.info("WF1 분석 엔진 완료: 모델 %d개 생성됨", len(analysis_result.get("models", [])))
|
|
|
|
logger.info("WF1 분석 결과 DB 저장 시작: project_id=%s", project_id)
|
|
async with pool.acquire() as connection:
|
|
await connection.begin()
|
|
try:
|
|
await save_surface_analysis_to_db(
|
|
connection,
|
|
project_id=project_id,
|
|
input_file_id=input_file_id,
|
|
analysis_result=analysis_result,
|
|
source_filters=source_filters,
|
|
)
|
|
async with connection.cursor() as cursor:
|
|
await complete_stage(cursor, str(project_id), 1)
|
|
await connection.commit()
|
|
logger.info("WF1 분석 결과 DB 저장 완료: project_id=%s", project_id)
|
|
except Exception as e:
|
|
logger.exception("WF1 분석 결과 DB 저장 실패: %s", e)
|
|
await connection.rollback()
|
|
raise
|
|
|
|
logger.info(
|
|
"SEND_ANALYSIS_COMPLETION_EMAIL=%s, project_info=%s",
|
|
SEND_ANALYSIS_COMPLETION_EMAIL,
|
|
bool(project_info),
|
|
)
|
|
if SEND_ANALYSIS_COMPLETION_EMAIL and project_info and project_info.get("user_email"):
|
|
logger.info("WF1 완료 이메일 발송 시작: to=%s", project_info["user_email"])
|
|
await send_analysis_completion_email(
|
|
project_id=project_id,
|
|
project_name=str(project_info.get("project_name") or project_id),
|
|
to_email=str(project_info["user_email"]),
|
|
analysis_result=analysis_result,
|
|
)
|
|
logger.info("WF1 완료 이메일 발송 완료: project_id=%s", project_id)
|
|
else:
|
|
logger.info(
|
|
"WF1 완료 이메일 발송 스킵: SEND_ANALYSIS_COMPLETION_EMAIL=%s, has_email=%s",
|
|
SEND_ANALYSIS_COMPLETION_EMAIL,
|
|
project_info and project_info.get("user_email") is not None,
|
|
)
|
|
logger.info("WF1 백그라운드 분석 완료: project_id=%s", project_id)
|
|
except Exception as exc:
|
|
logger.exception("WF1 백그라운드 분석 실패: project_id=%s", project_id)
|
|
async with pool.acquire() as connection, connection.cursor() as cursor:
|
|
await fail_stage(cursor, str(project_id), 1, str(exc))
|
|
await connection.commit()
|
|
if project_info and project_info.get("user_email"):
|
|
await send_analysis_error_email(
|
|
project_id=project_id,
|
|
project_name=str(project_info.get("project_name") or project_id),
|
|
to_email=str(project_info["user_email"]),
|
|
error_message=str(exc),
|
|
)
|
|
|
|
|
|
@router.post("/{project_id}/files", response_model=FileUploadResponse)
|
|
async def upload_project_files(
|
|
project_id: UUID,
|
|
files: list[UploadFile] = File(...),
|
|
) -> FileUploadResponse | JSONResponse:
|
|
"""프로젝트 입력 파일을 저장·분석하고 DB 메타데이터를 기록한다."""
|
|
if not files or len(files) > UPLOAD_MAX_FILES:
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={
|
|
"status": "error",
|
|
"message": f"파일은 1~{UPLOAD_MAX_FILES}개까지 가능합니다.",
|
|
},
|
|
)
|
|
|
|
filenames = [upload.filename or "" for upload in files]
|
|
if len({filename.casefold() for filename in filenames}) != len(filenames):
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={"status": "error", "message": "동일한 파일명을 중복 업로드할 수 없습니다."},
|
|
)
|
|
las_count = sum(Path(filename).suffix.lower() in {".las", ".laz"} for filename in filenames)
|
|
if las_count != 1:
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={
|
|
"status": "error",
|
|
"message": "LAS 또는 LAZ 파일을 정확히 1개 포함해야 합니다.",
|
|
},
|
|
)
|
|
|
|
pool = get_db_pool()
|
|
saved_paths: list[Path] = []
|
|
try:
|
|
results: list[UploadedFileResult] = []
|
|
|
|
async with pool.acquire() as connection:
|
|
stored_path = await get_project_storage_relative_path(connection, project_id)
|
|
project_root = Path(resolve_stored_project_path(stored_path))
|
|
|
|
await connection.begin()
|
|
try:
|
|
for upload in files:
|
|
preliminary = FileUploadDescriptor(
|
|
original_filename=upload.filename or "",
|
|
size_bytes=max(upload.size or 0, 1),
|
|
)
|
|
destination = resolve_upload_destination(project_root, preliminary)
|
|
written_bytes = await save_upload_stream(upload, destination)
|
|
saved_paths.append(destination)
|
|
descriptor = FileUploadDescriptor(
|
|
original_filename=preliminary.original_filename,
|
|
size_bytes=written_bytes,
|
|
)
|
|
metadata = await asyncio.to_thread(analyze_input_metadata, destination)
|
|
relative_path = destination.relative_to(project_root).as_posix()
|
|
file_type = destination.suffix.lower().lstrip(".")
|
|
crs_epsg = metadata.get("epsg")
|
|
input_file_id = await create_input_file(
|
|
connection,
|
|
project_id=project_id,
|
|
file_type=file_type,
|
|
original_filename=descriptor.original_filename,
|
|
relative_path=relative_path,
|
|
file_size_bytes=written_bytes,
|
|
upload_by=None,
|
|
crs_epsg=int(crs_epsg) if crs_epsg is not None else None,
|
|
metadata=metadata,
|
|
)
|
|
results.append(
|
|
UploadedFileResult(
|
|
input_file_id=input_file_id,
|
|
original_filename=descriptor.original_filename,
|
|
file_type=file_type,
|
|
relative_path=relative_path,
|
|
size_bytes=written_bytes,
|
|
metadata=metadata,
|
|
)
|
|
)
|
|
async with connection.cursor() as cursor:
|
|
await complete_stage(cursor, str(project_id), 0)
|
|
await connection.commit()
|
|
except Exception:
|
|
await connection.rollback()
|
|
raise
|
|
|
|
stage_root = project_root / "B03_FileInput"
|
|
atomic_write_json(
|
|
stage_root / "metadata.json",
|
|
{"project_id": str(project_id), "files": [result.model_dump() for result in results]},
|
|
)
|
|
workflow_path = project_root / "workflow.json"
|
|
if not workflow_path.exists():
|
|
atomic_write_json(workflow_path, load_project_workflow(project_root))
|
|
point_cloud_result = next(
|
|
(result for result in results if _is_point_cloud_result(result)),
|
|
None,
|
|
)
|
|
if point_cloud_result:
|
|
_schedule_background_task(
|
|
_send_upload_complete_notification(
|
|
project_id=project_id,
|
|
uploaded_file=point_cloud_result,
|
|
),
|
|
task_name=f"b03-upload-email-{project_id}",
|
|
)
|
|
_schedule_background_task(
|
|
trigger_wf1_analysis_and_email(
|
|
project_id=project_id,
|
|
input_file_id=point_cloud_result.input_file_id,
|
|
),
|
|
task_name=f"b04-wf1-auto-{project_id}",
|
|
)
|
|
return FileUploadResponse(project_id=str(project_id), files=results)
|
|
except LookupError as exc:
|
|
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
|
|
except (OSError, ValueError) as exc:
|
|
for saved_path in saved_paths:
|
|
saved_path.unlink(missing_ok=True)
|
|
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
|
|
except Exception:
|
|
for saved_path in saved_paths:
|
|
saved_path.unlink(missing_ok=True)
|
|
logger.exception("B03 파일 업로드 처리 실패: project_id=%s", project_id)
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"status": "error", "message": "파일 업로드 처리 중 오류가 발생했습니다."},
|
|
)
|
|
finally:
|
|
for upload in files:
|
|
await upload.close()
|
|
|
|
|
|
@router.post("/{project_id}/upload-sessions", response_model=ChunkSessionCreateResponse)
|
|
async def create_project_upload_session(
|
|
project_id: UUID,
|
|
payload: ChunkSessionCreateRequest,
|
|
) -> ChunkSessionCreateResponse | JSONResponse:
|
|
"""대용량 파일 청크 업로드 세션을 생성한다."""
|
|
chunk_size_bytes = min(payload.chunk_size_bytes, UPLOAD_CHUNK_SIZE_BYTES)
|
|
total_chunks = _total_chunks(payload.size_bytes, chunk_size_bytes)
|
|
session_id = str(uuid4())
|
|
|
|
pool = get_db_pool()
|
|
try:
|
|
async with pool.acquire() as connection:
|
|
await get_project_storage_relative_path(connection, project_id)
|
|
await create_upload_session(
|
|
connection,
|
|
session_id=session_id,
|
|
project_id=project_id,
|
|
original_filename=payload.original_filename,
|
|
file_size_bytes=payload.size_bytes,
|
|
chunk_size_bytes=chunk_size_bytes,
|
|
total_chunks=total_chunks,
|
|
)
|
|
return ChunkSessionCreateResponse(
|
|
project_id=str(project_id),
|
|
upload_session_id=session_id,
|
|
original_filename=payload.original_filename,
|
|
file_size_bytes=payload.size_bytes,
|
|
chunk_size_bytes=chunk_size_bytes,
|
|
total_chunks=total_chunks,
|
|
)
|
|
except LookupError as exc:
|
|
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
|
|
except (OSError, ValueError) as exc:
|
|
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
|
|
except Exception:
|
|
logger.exception("B03 청크 세션 생성 실패: project_id=%s", project_id)
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"status": "error", "message": "업로드 세션 생성 중 오류가 발생했습니다."},
|
|
)
|
|
|
|
|
|
@router.post("/{project_id}/chunks", response_model=ChunkUploadResponse)
|
|
async def upload_project_chunk(
|
|
project_id: UUID,
|
|
session_id: str = Form(...),
|
|
chunk_index: int = Form(...),
|
|
chunk_data: UploadFile = File(...),
|
|
) -> ChunkUploadResponse | JSONResponse:
|
|
"""단일 파일 청크를 B03 임시 폴더에 저장하고 DB에 기록한다."""
|
|
pool = get_db_pool()
|
|
try:
|
|
async with pool.acquire() as connection:
|
|
session = await get_upload_session(
|
|
connection,
|
|
project_id=project_id,
|
|
session_id=session_id,
|
|
)
|
|
if chunk_index < 0 or chunk_index >= int(session["total_chunks"]):
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={"status": "error", "message": "청크 인덱스가 범위를 벗어났습니다."},
|
|
)
|
|
stored_path = await get_project_storage_relative_path(connection, project_id)
|
|
project_root = Path(resolve_stored_project_path(stored_path))
|
|
session_dir = resolve_chunk_session_dir(project_root, session_id)
|
|
chunk_path, size_bytes, chunk_hash = await save_upload_chunk(
|
|
chunk_data,
|
|
session_dir,
|
|
chunk_index,
|
|
expected_max_bytes=int(session["chunk_size_bytes"]),
|
|
)
|
|
relative_chunk_path = chunk_path.relative_to(project_root).as_posix()
|
|
completed_chunks = await upsert_upload_chunk(
|
|
connection,
|
|
session_id=session_id,
|
|
chunk_index=chunk_index,
|
|
chunk_hash=chunk_hash,
|
|
size_bytes=size_bytes,
|
|
stored_at=relative_chunk_path,
|
|
)
|
|
return ChunkUploadResponse(
|
|
upload_session_id=session_id,
|
|
chunk_index=chunk_index,
|
|
completed_chunks=completed_chunks,
|
|
total_chunks=int(session["total_chunks"]),
|
|
chunk_hash=chunk_hash,
|
|
)
|
|
except LookupError as exc:
|
|
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
|
|
except (OSError, ValueError) as exc:
|
|
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
|
|
except Exception:
|
|
logger.exception(
|
|
"B03 청크 업로드 실패: project_id=%s session_id=%s",
|
|
project_id,
|
|
session_id,
|
|
)
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"status": "error", "message": "청크 업로드 중 오류가 발생했습니다."},
|
|
)
|
|
finally:
|
|
await chunk_data.close()
|
|
|
|
|
|
@router.post("/{project_id}/finalize", response_model=FileUploadResponse)
|
|
async def finalize_project_upload(
|
|
project_id: UUID,
|
|
payload: UploadFinalizeRequest,
|
|
) -> FileUploadResponse | JSONResponse:
|
|
"""청크 업로드를 최종 병합하고 input_files 메타데이터를 기록한다."""
|
|
pool = get_db_pool()
|
|
final_path: Path | None = None
|
|
try:
|
|
async with pool.acquire() as connection:
|
|
session = await get_upload_session(
|
|
connection,
|
|
project_id=project_id,
|
|
session_id=payload.session_id,
|
|
)
|
|
if int(session["total_chunks"]) != payload.total_chunks:
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={"status": "error", "message": "세션 청크 개수가 일치하지 않습니다."},
|
|
)
|
|
completed_indexes = await list_completed_chunk_indexes(
|
|
connection,
|
|
session_id=payload.session_id,
|
|
)
|
|
expected_indexes = list(range(payload.total_chunks))
|
|
if completed_indexes != expected_indexes:
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content={"status": "error", "message": "아직 업로드되지 않은 청크가 있습니다."},
|
|
)
|
|
|
|
stored_path = await get_project_storage_relative_path(connection, project_id)
|
|
project_root = Path(resolve_stored_project_path(stored_path))
|
|
descriptor = FileUploadDescriptor(
|
|
original_filename=str(session["original_filename"]),
|
|
size_bytes=int(session["file_size_bytes"]),
|
|
)
|
|
final_path = merge_upload_chunks(
|
|
project_root,
|
|
descriptor,
|
|
payload.session_id,
|
|
payload.total_chunks,
|
|
)
|
|
metadata = await asyncio.to_thread(analyze_input_metadata, final_path)
|
|
relative_path = final_path.relative_to(project_root).as_posix()
|
|
file_type = final_path.suffix.lower().lstrip(".")
|
|
crs_epsg = metadata.get("epsg")
|
|
|
|
await connection.begin()
|
|
try:
|
|
input_file_id = await create_input_file(
|
|
connection,
|
|
project_id=project_id,
|
|
file_type=file_type,
|
|
original_filename=descriptor.original_filename,
|
|
relative_path=relative_path,
|
|
file_size_bytes=descriptor.size_bytes,
|
|
upload_by=None,
|
|
crs_epsg=int(crs_epsg) if crs_epsg is not None else None,
|
|
metadata=metadata,
|
|
)
|
|
await mark_upload_session_completed(connection, session_id=payload.session_id)
|
|
async with connection.cursor() as cursor:
|
|
await complete_stage(cursor, str(project_id), 0)
|
|
await connection.commit()
|
|
except Exception:
|
|
await connection.rollback()
|
|
await mark_upload_session_failed(connection, session_id=payload.session_id)
|
|
raise
|
|
|
|
remove_chunk_session(project_root, payload.session_id)
|
|
result = UploadedFileResult(
|
|
input_file_id=input_file_id,
|
|
original_filename=descriptor.original_filename,
|
|
file_type=file_type,
|
|
relative_path=relative_path,
|
|
size_bytes=descriptor.size_bytes,
|
|
metadata=metadata,
|
|
)
|
|
stage_root = project_root / "B03_FileInput"
|
|
atomic_write_json(
|
|
stage_root / "metadata.json",
|
|
{"project_id": str(project_id), "files": [result.model_dump()]},
|
|
)
|
|
if _is_point_cloud_result(result):
|
|
_schedule_background_task(
|
|
_send_upload_complete_notification(
|
|
project_id=project_id,
|
|
uploaded_file=result,
|
|
),
|
|
task_name=f"b03-upload-email-{project_id}",
|
|
)
|
|
_schedule_background_task(
|
|
trigger_wf1_analysis_and_email(
|
|
project_id=project_id,
|
|
input_file_id=result.input_file_id,
|
|
),
|
|
task_name=f"b04-wf1-auto-{project_id}",
|
|
)
|
|
return FileUploadResponse(project_id=str(project_id), files=[result])
|
|
except LookupError as exc:
|
|
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
|
|
except (OSError, ValueError) as exc:
|
|
if final_path is not None:
|
|
final_path.unlink(missing_ok=True)
|
|
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
|
|
except Exception:
|
|
if final_path is not None:
|
|
final_path.unlink(missing_ok=True)
|
|
logger.exception("B03 청크 업로드 최종 병합 실패: project_id=%s", project_id)
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"status": "error", "message": "업로드 최종 처리 중 오류가 발생했습니다."},
|
|
)
|
|
|
|
|
|
@router.get("/{project_id}/upload-status/{session_id}", response_model=UploadStatusResponse)
|
|
async def get_project_upload_status(
|
|
project_id: UUID,
|
|
session_id: str,
|
|
) -> UploadStatusResponse | JSONResponse:
|
|
"""업로드 세션의 청크 완료 상태를 조회한다."""
|
|
pool = get_db_pool()
|
|
try:
|
|
async with pool.acquire() as connection:
|
|
session = await get_upload_session(
|
|
connection,
|
|
project_id=project_id,
|
|
session_id=session_id,
|
|
)
|
|
completed_indexes = await list_completed_chunk_indexes(
|
|
connection,
|
|
session_id=session_id,
|
|
)
|
|
return UploadStatusResponse(
|
|
upload_session_id=session_id,
|
|
upload_status=str(session["status"]),
|
|
original_filename=str(session["original_filename"]),
|
|
file_size_bytes=int(session["file_size_bytes"]),
|
|
chunk_size_bytes=int(session["chunk_size_bytes"]),
|
|
total_chunks=int(session["total_chunks"]),
|
|
completed_chunks=len(completed_indexes),
|
|
completed_chunk_indexes=completed_indexes,
|
|
)
|
|
except LookupError as exc:
|
|
return JSONResponse(status_code=404, content={"status": "error", "message": str(exc)})
|
|
except (OSError, ValueError) as exc:
|
|
return JSONResponse(status_code=400, content={"status": "error", "message": str(exc)})
|
|
except Exception:
|
|
logger.exception(
|
|
"B03 업로드 상태 조회 실패: project_id=%s session_id=%s",
|
|
project_id,
|
|
session_id,
|
|
)
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"status": "error", "message": "업로드 상태 조회 중 오류가 발생했습니다."},
|
|
)
|
|
|
|
|
|
@router.get("/{project_id}/workflow-state")
|
|
async def get_project_workflow_state(project_id: str):
|
|
pool = get_db_pool()
|
|
async with pool.acquire() as connection, connection.cursor(aiomysql.DictCursor) as cursor:
|
|
state = await get_workflow_state(cursor, project_id)
|
|
return {"status": "success", "workflow_state": state}
|