feat: p2v 연동

This commit is contained in:
skkim committed 2026-08-26 13:52:18 +09:00
1 parent b3385e7df2
commit 5de5c7507d
26 files changed
+2944 -9

No files matched your search

View File
Whitespace-only changes.
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+429
View File
@@ -0,0 +1,429 @@
# -*- coding: utf-8 -*-
"""P2V F1(무빙 포스터) 프록시 라우터.
생성 → 폴링 → (검수: 나레이션/메타 수정 → 승인) → 폴링 → done 의 2단 게이트.
크레딧은 생성 시점 선차감. 실패 시 즉시 환불하지 않는다 — 재시도 버튼이 있어
"환불 + 공짜 재시도"가 되기 때문이다. 환불은 삭제·고아 스윕에서 일어난다
(f1_service 모듈 docstring 참조).
"""
from typing import Optional
from fastapi import APIRouter, BackgroundTasks, Depends, File, Form, UploadFile
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.credit.exceptions import InsufficientCreditError
from app.database.session import AsyncSessionLocal, get_session
from app.p2v.exceptions import (
P2vDisabledError,
P2vException,
P2vInvalidStateError,
P2vUploadTooLargeError,
P2vUpstreamError,
)
from app.p2v.models import P2vF1Job
from app.p2v.schemas.p2v_schema import (
ApproveRequest,
MetadataUpdateRequest,
NarrationUpdateRequest,
P2vDeleteResponse,
P2vF1StatusResponse,
P2vJobCreateResponse,
P2vJobError,
P2vMetadata,
)
from app.p2v.services import archive_service, client, f1_service
from app.user.dependencies.auth import get_current_user
from app.user.models import User
from app.utils.logger import get_logger
from config import p2v_settings
logger = get_logger("p2v")
router = APIRouter(prefix="/p2v/f1", tags=["P2V"])
def _guard_enabled() -> None:
if not p2v_settings.P2V_ENABLED:
raise P2vDisabledError()
@router.post(
"/jobs",
response_model=P2vJobCreateResponse,
summary="무빙 포스터 생성 요청",
description="**이 시점에 크레딧이 선차감됩니다.** 검수 승인 없이 방치하면 "
"일정 시간 후 자동 환불·실패 처리됩니다.",
responses={
401: {"description": "인증 실패"},
402: {"description": "크레딧 부족"},
413: {"description": "용량 초과"},
503: {"description": "P2V 서버 연결 실패"},
},
)
async def create_job(
background_tasks: BackgroundTasks,
poster: UploadFile = File(...),
name: str = Form(""),
current_user: User = Depends(get_current_user),
) -> P2vJobCreateResponse:
_guard_enabled()
raw = await poster.read()
if len(raw) > p2v_settings.P2V_MAX_UPLOAD_BYTES:
raise P2vUploadTooLargeError(p2v_settings.P2V_MAX_UPLOAD_BYTES)
# 행 삽입 + 크레딧 선차감을 한 트랜잭션으로 묶는다 (ssulbox create_ssul 패턴)
async with AsyncSessionLocal() as session:
try:
row = await f1_service.create_job(
session, user_uuid=current_user.user_uuid, name=name.strip()
)
await session.commit()
job_id = row.id
except InsufficientCreditError:
await session.rollback()
logger.info(f"[f1] INSUFFICIENT CREDIT user={current_user.user_uuid}")
raise
except Exception:
await session.rollback()
raise
# 차감이 확정된 뒤에야 P2V 를 부른다
try:
created = await client.request_json(
"POST",
"/api/f1/jobs",
data={"name": name},
files={
"poster": (
poster.filename or "poster.png",
raw,
poster.content_type or "image/png",
)
},
)
except P2vException as e:
# P2V 를 태우지 못했다(실비용 없음) — 환불하고 실패로 남긴다
async with AsyncSessionLocal() as session:
failed_row = await session.get(P2vF1Job, job_id)
if failed_row is not None:
await f1_service.fail_job(
session, failed_row, f"P2V 요청 실패: {e.message}", refund=True
)
await session.commit()
raise
async with AsyncSessionLocal() as session:
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
row.p2v_job_id = str(created["id"])
await session.commit()
background_tasks.add_task(
archive_service.upload_source_image,
"f1",
job_id,
current_user.user_uuid,
raw,
poster.filename or "poster.png",
)
return P2vJobCreateResponse(
id=job_id,
status="queued",
poll_interval_seconds=p2v_settings.P2V_POLL_HINT_SECONDS,
)
def _error_of(row: P2vF1Job) -> Optional[P2vJobError]:
if row.status != "failed" or not row.error:
return None
stage, _, detail = row.error.partition(": ")
return P2vJobError(stage=stage or "?", detail=detail or row.error)
def _metadata_of(row: P2vF1Job) -> Optional[P2vMetadata]:
if not (row.event_name or row.place or row.date_text):
return None
return P2vMetadata(
event_name=row.event_name or "",
date_text=row.date_text or "",
place=row.place or "",
keywords=row.keywords,
)
def _db_response(row: P2vF1Job) -> P2vF1StatusResponse:
"""P2V 를 부르지 않고 castad DB 미러만으로 응답 (archived/failed 종결 상태)."""
artifacts: dict[str, str] = {}
if row.p2v_video_url:
artifacts["video"] = row.p2v_video_url
if row.poster_url:
artifacts["thumbnail"] = row.poster_url
return P2vF1StatusResponse(
id=row.id,
status=row.status, # type: ignore[arg-type]
name=row.name,
narration=row.narration,
metadata=_metadata_of(row),
duration=float(row.duration) if row.duration is not None else None,
error=_error_of(row),
artifacts=artifacts,
)
@router.get(
"/jobs/{job_id}",
response_model=P2vF1StatusResponse,
summary="생성 진행 상태 (폴링)",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def get_job(
job_id: int,
background_tasks: BackgroundTasks,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> P2vF1StatusResponse:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
# 종결 상태 — P2V 를 부르지 않는다 (컨테이너 재기동으로 잡이 지워졌을 수 있다)
if row.archived_at is not None or row.status == "failed" or not row.p2v_job_id:
return _db_response(row)
try:
p2v_job = await client.request_json("GET", f"/api/f1/jobs/{row.p2v_job_id}")
except P2vUpstreamError as e:
if e.status_code == 404:
# P2V 재기동으로 잡 소실 — 재시도가 불가능하므로 환불하고 실패 처리
await f1_service.fail_job(
session,
row,
"서버 재시작으로 작업이 유실되었습니다. 크레딧을 환불했습니다",
refund=True,
)
await session.commit()
return _db_response(row)
raise
f1_service.sync_from_p2v(row, p2v_job)
status = row.status
if p2v_job.get("status") == "done" and row.archived_at is None:
# Blob 업로드 전까지는 진행 중으로 보인다 — URL 없는 done 창을 만들지 않는다
status = "archiving"
background_tasks.add_task(archive_service.archive_f1, row.id)
await session.commit()
# 검수용 영역분석 이미지는 프록시 경로로 재작성한다 (regions/ 는 인증 프록시)
artifacts: dict[str, str] = {}
check = (p2v_job.get("artifacts") or {}).get("check_jpg")
if check and check.startswith("/files/"):
artifacts["check_jpg"] = "/p2v" + check
return P2vF1StatusResponse(
id=row.id,
status=status, # type: ignore[arg-type]
stage=p2v_job.get("stage"),
stages=p2v_job.get("stages"),
name=row.name,
narration=row.narration,
metadata=_metadata_of(row),
motion_elements=p2v_job.get("motion_elements"),
duration=float(row.duration) if row.duration is not None else None,
queue_size=p2v_job.get("queue_size"),
error=_error_of(row),
artifacts=artifacts,
)
# =============================================================================
# 검수 게이트
# =============================================================================
@router.put(
"/jobs/{job_id}/narration",
summary="검수 중 나레이션 수정",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def update_narration(
job_id: int,
body: NarrationUpdateRequest,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
if not row.p2v_job_id:
raise P2vInvalidStateError("아직 서버에 접수되지 않은 작업입니다")
await client.request_json(
"PUT",
f"/api/f1/jobs/{row.p2v_job_id}/narration",
json_body={"narration": body.narration},
)
row.narration = body.narration
await session.commit()
return {"ok": True}
@router.put(
"/jobs/{job_id}/metadata",
summary="검수 중 행사 메타데이터 수정",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def update_metadata(
job_id: int,
body: MetadataUpdateRequest,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
if not row.p2v_job_id:
raise P2vInvalidStateError("아직 서버에 접수되지 않은 작업입니다")
await client.request_json(
"PUT",
f"/api/f1/jobs/{row.p2v_job_id}/metadata",
json_body=body.model_dump(),
)
row.event_name = body.event_name[:200]
row.date_text = body.date_text[:100] or None
row.place = body.place[:200]
await session.commit()
return {"ok": True}
@router.post(
"/jobs/{job_id}/approve",
summary="검수 승인 — TTS·BGM·애니메이션·렌더 시작",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def approve_job(
job_id: int,
body: Optional[ApproveRequest] = None,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
if not row.p2v_job_id:
raise P2vInvalidStateError("아직 서버에 접수되지 않은 작업입니다")
json_body = (
{"motions": body.motions} if body is not None and body.motions is not None else None
)
await client.request_json(
"POST", f"/api/f1/jobs/{row.p2v_job_id}/approve", json_body=json_body
)
row.status = "running"
await session.commit()
return {"ok": True}
@router.post(
"/jobs/{job_id}/retry",
summary="실패한 스테이지부터 재시도",
description="선차감분이 그대로 유효하므로 재시도에 크레딧이 추가 차감되지 않는다. "
"(재과금 정책은 D-2 로 보류 — 도입 시 attempt 컬럼 추가)",
responses={
401: {"description": "인증 실패"},
404: {"description": "잡 없음"},
409: {"description": "실패 상태가 아님"},
},
)
async def retry_job(
job_id: int,
body: Optional[ApproveRequest] = None,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
if row.status != "failed":
raise P2vInvalidStateError(f"실패 상태가 아닙니다: {row.status}")
if not row.p2v_job_id:
raise P2vInvalidStateError("서버에 접수되지 못한 작업은 재시도할 수 없습니다")
json_body = (
{"motions": body.motions} if body is not None and body.motions is not None else None
)
await client.request_json(
"POST", f"/api/f1/jobs/{row.p2v_job_id}/retry", json_body=json_body
)
row.status = "queued"
row.error = None
await session.commit()
return {"ok": True}
@router.post(
"/jobs/{job_id}/force",
summary="실패를 무시하고 진행 (i2v 게이트 실패 전용)",
description="제목 훼손 게이트 등 클립 생성 **이후의** 실패를 사람이 확인하고 "
"이미 만들어진 클립으로 렌더를 잇는다. 재시도와 달리 Higgsfield 재생성이 없다. "
"클립이 없는 실패는 P2V 가 409 로 거절한다.",
responses={
401: {"description": "인증 실패"},
404: {"description": "잡 없음"},
409: {"description": "실패 상태가 아니거나 무시할 수 없는 실패"},
},
)
async def force_job(
job_id: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
if row.status != "failed":
raise P2vInvalidStateError(f"실패 상태가 아닙니다: {row.status}")
if not row.p2v_job_id:
raise P2vInvalidStateError("서버에 접수되지 못한 작업은 진행할 수 없습니다")
await client.request_json("POST", f"/api/f1/jobs/{row.p2v_job_id}/force")
row.status = "queued"
row.error = None
await session.commit()
return {"ok": True}
@router.delete(
"/jobs/{job_id}",
response_model=P2vDeleteResponse,
summary="잡과 산출물 삭제 — 되돌릴 수 없다",
description="완성(done) 전에 버리는 잡은 이 시점에 크레딧이 환불된다.",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def delete_job(
job_id: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> P2vDeleteResponse:
_guard_enabled()
row = await f1_service.get_owned(session, job_id, current_user.user_uuid)
# P2V 쪽 산출물 먼저 정리. 이미 사라진 잡(404)은 정상 — 지울 게 없다.
if row.p2v_job_id:
try:
await client.request_json("DELETE", f"/api/f1/jobs/{row.p2v_job_id}")
except P2vUpstreamError as e:
if e.status_code != 404:
raise # 진행 중(409) 등은 그대로 중계 — 반쪽 삭제를 만들지 않는다
# 환불 판정은 잠금 재조회로 한다 (2026-08-26 리뷰 TOCTOU 반영) —
# 처음 읽은 스냅샷과 달리, 그 사이 백그라운드 아카이빙이 done 으로 전이를
# 커밋했을 수 있다. FOR UPDATE 로 최신 상태를 고정한 뒤 판정해야
# "완성본도 받고 환불도 받는" 창이 닫힌다.
result = await session.execute(
select(P2vF1Job)
.where(P2vF1Job.id == job_id)
.with_for_update()
)
row = result.scalar_one_or_none()
if row is None:
return P2vDeleteResponse(id=job_id, removed=True)
# done(완성)·archiving(렌더 완료, 업로드 중)은 결과물이 이미 만들어졌다 — 환불 없음
if row.archived_at is None and row.status not in ("done", "archiving"):
await f1_service.refund_job(session, row, reason="무빙 포스터 삭제(미완성)")
await session.delete(row)
await session.commit()
return P2vDeleteResponse(id=job_id, removed=True)
+335
View File
@@ -0,0 +1,335 @@
# -*- coding: utf-8 -*-
"""P2V F2(포스터 스타일링) 프록시 라우터.
브라우저 → castad(인증·크레딧) → P2V 서버(:8010) 중계.
템플릿 목록 등 조회는 순수 중계, 잡 생성만 크레딧 선차감이 붙는다.
"""
from fastapi import APIRouter, BackgroundTasks, Depends, File, Form, UploadFile
from sqlalchemy.ext.asyncio import AsyncSession
from app.credit.exceptions import InsufficientCreditError
from app.database.session import AsyncSessionLocal, get_session
from app.p2v.exceptions import (
P2vDisabledError,
P2vException,
P2vUploadTooLargeError,
P2vUpstreamError,
)
from app.p2v.models import P2vF2Job
from app.p2v.schemas.p2v_schema import (
F2CategoryResponse,
F2FormatResponse,
F2TemplateResponse,
F2UploadHintResponse,
P2vF2StatusResponse,
P2vJobCreateResponse,
P2vJobError,
)
from app.p2v.services import archive_service, client, f2_service
from app.user.dependencies.auth import get_current_user
from app.user.models import User
from app.utils.logger import get_logger
from config import p2v_settings
logger = get_logger("p2v")
router = APIRouter(prefix="/p2v/f2", tags=["P2V"])
def _guard_enabled() -> None:
if not p2v_settings.P2V_ENABLED:
raise P2vDisabledError()
def _proxy_thumb(url: str) -> str:
"""P2V 의 /files/... 경로를 castad 프록시 경로로 재작성한다."""
if url.startswith("/files/"):
return "/p2v" + url
return url
def _rewrite_template(t: dict) -> dict:
return {**t, "thumb_url": _proxy_thumb(t.get("thumb_url", ""))}
# =============================================================================
# 템플릿 · 메타 (순수 중계)
# =============================================================================
@router.get(
"/templates",
response_model=list[F2TemplateResponse],
summary="스타일 템플릿 목록",
responses={401: {"description": "인증 실패"}, 503: {"description": "P2V 서버 연결 실패"}},
)
async def list_templates(
current_user: User = Depends(get_current_user),
) -> list[dict]:
_guard_enabled()
items = await client.request_json("GET", "/api/f2/templates")
return [_rewrite_template(t) for t in items]
@router.post(
"/templates",
response_model=F2TemplateResponse,
summary="사용자 레퍼런스 업로드",
description="저장 + 화풍 분석까지 P2V 가 동기로 끝낸다(10초 안팎).",
responses={401: {"description": "인증 실패"}, 413: {"description": "용량 초과"}},
)
async def create_template(
reference: UploadFile = File(...),
name: str = Form(""),
current_user: User = Depends(get_current_user),
) -> dict:
_guard_enabled()
raw = await reference.read()
if len(raw) > p2v_settings.P2V_MAX_UPLOAD_BYTES:
raise P2vUploadTooLargeError(p2v_settings.P2V_MAX_UPLOAD_BYTES)
created = await client.request_json(
"POST",
"/api/f2/templates",
data={"name": name},
files={
"reference": (
reference.filename or "reference.png",
raw,
reference.content_type or "image/png",
)
},
)
return _rewrite_template(created)
@router.delete(
"/templates/{template_id}",
summary="사용자 레퍼런스 삭제",
responses={401: {"description": "인증 실패"}, 404: {"description": "삭제 불가 템플릿"}},
)
async def delete_template(
template_id: str,
current_user: User = Depends(get_current_user),
) -> dict:
_guard_enabled()
return await client.request_json("DELETE", f"/api/f2/templates/{template_id}")
@router.get("/categories", response_model=list[F2CategoryResponse], summary="템플릿 카테고리")
async def list_categories(current_user: User = Depends(get_current_user)) -> list[dict]:
_guard_enabled()
return await client.request_json("GET", "/api/f2/categories")
@router.get("/formats", response_model=list[F2FormatResponse], summary="출력 포맷 목록")
async def list_formats(current_user: User = Depends(get_current_user)) -> list[dict]:
_guard_enabled()
return await client.request_json("GET", "/api/f2/formats")
@router.get("/upload-hint", response_model=F2UploadHintResponse, summary="레퍼런스 업로드 안내")
async def upload_hint(current_user: User = Depends(get_current_user)) -> dict:
_guard_enabled()
return await client.request_json("GET", "/api/f2/upload-hint")
# =============================================================================
# 잡
# =============================================================================
@router.post(
"/jobs",
response_model=P2vJobCreateResponse,
summary="스타일 변환 요청",
description="**이 시점에 크레딧이 선차감됩니다.** 실패 시 자동 환불됩니다.",
responses={
401: {"description": "인증 실패"},
402: {"description": "크레딧 부족"},
413: {"description": "용량 초과"},
503: {"description": "P2V 서버 연결 실패"},
},
)
async def create_job(
background_tasks: BackgroundTasks,
poster: UploadFile = File(...),
template_id: str = Form(...),
format: str = Form("poster"),
current_user: User = Depends(get_current_user),
) -> P2vJobCreateResponse:
_guard_enabled()
raw = await poster.read()
if len(raw) > p2v_settings.P2V_MAX_UPLOAD_BYTES:
raise P2vUploadTooLargeError(p2v_settings.P2V_MAX_UPLOAD_BYTES)
# 템플릿 스냅샷은 차감 **전에** 확보한다 — 없는 템플릿이면 크레딧을 건드리지 않는다
templates = await client.request_json("GET", "/api/f2/templates")
tpl = next((t for t in templates if t.get("id") == template_id), None)
if tpl is None:
raise P2vUpstreamError("존재하지 않는 템플릿입니다", 404)
# 포스터 이름은 업로드 파일명에서 딴다 (F2 에는 F1 의 행사명 같은 입력이 없다).
# 템플릿 이름을 이름 자리에 쓰면 "별이 빛나는 밤"이 포스터 이름처럼 보인다.
poster_name = (poster.filename or "").rsplit(".", 1)[0].strip()
# 행 삽입 + 크레딧 선차감을 한 트랜잭션으로 묶는다 (ssulbox create_ssul 패턴)
async with AsyncSessionLocal() as session:
try:
row = await f2_service.create_job(
session,
user_uuid=current_user.user_uuid,
name=poster_name,
template_id=template_id,
template_name=tpl.get("name_ko"),
license=tpl.get("license"),
format=format,
)
await session.commit()
job_id = row.id
except InsufficientCreditError:
await session.rollback()
logger.info(f"[f2] INSUFFICIENT CREDIT user={current_user.user_uuid}")
raise
except Exception:
await session.rollback()
raise
# 차감이 확정된 뒤에야 P2V 를 부른다 — 잔액 없는 사용자가 실비용을 태우지 않게
try:
created = await client.request_json(
"POST",
"/api/f2/jobs",
data={"template_id": template_id, "format": format},
files={
"poster": (
poster.filename or "poster.png",
raw,
poster.content_type or "image/png",
)
},
)
except P2vException as e:
# 차감은 이미 확정됐다 — 새 트랜잭션에서 실패 처리(환불)한다
async with AsyncSessionLocal() as session:
failed_row = await session.get(P2vF2Job, job_id)
if failed_row is not None:
await f2_service.fail_job(
session, failed_row, f"P2V 요청 실패: {e.message}"
)
await session.commit()
raise
async with AsyncSessionLocal() as session:
row = await f2_service.get_owned(session, job_id, current_user.user_uuid)
row.p2v_job_id = str(created["id"])
await session.commit()
background_tasks.add_task(
archive_service.upload_source_image,
"f2",
job_id,
current_user.user_uuid,
raw,
poster.filename or "poster.png",
)
return P2vJobCreateResponse(
id=job_id,
status="queued",
poll_interval_seconds=p2v_settings.P2V_POLL_HINT_SECONDS,
)
@router.delete(
"/jobs/{job_id}",
summary="스타일링 잡 삭제 (내 콘텐츠 목록에서 제거)",
description="완성(done) 전에 버리는 잡은 이 시점에 크레딧이 환불된다. "
"P2V 서버에는 F2 잡 삭제 API 가 없어 castad 기록만 지운다 "
"(P2V 쪽 산출물은 서버 자체 보존 주기를 따른다).",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def delete_job(
job_id: int,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> dict:
_guard_enabled()
row = await f2_service.get_owned(session, job_id, current_user.user_uuid)
# 완성본을 이미 받았다면 환불 대상이 아니다 (실패분은 fail_job 이 이미 환불했고,
# refund 는 멱등이라 중복 호출돼도 안전하다)
if row.archived_at is None and row.status not in ("done", "archiving"):
await f2_service.refund_job(session, row, reason="포스터 스타일링 삭제(미완성)")
await session.delete(row)
await session.commit()
return {"id": job_id, "removed": True}
def _error_of(row) -> P2vJobError | None:
if row.status != "failed" or not row.error:
return None
stage, _, detail = row.error.partition(": ")
return P2vJobError(stage=stage or "?", detail=detail or row.error)
@router.get(
"/jobs/{job_id}",
response_model=P2vF2StatusResponse,
summary="스타일 변환 상태 (폴링)",
responses={401: {"description": "인증 실패"}, 404: {"description": "잡 없음"}},
)
async def get_job(
job_id: int,
background_tasks: BackgroundTasks,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
) -> P2vF2StatusResponse:
_guard_enabled()
row = await f2_service.get_owned(session, job_id, current_user.user_uuid)
# 아카이브 완료 또는 종결 상태 — P2V 를 부를 필요가 없다 (잡이 지워졌을 수도 있다)
if row.archived_at is not None or (row.status == "failed") or not row.p2v_job_id:
artifacts = {}
if row.p2v_poster_url:
artifacts["image"] = row.p2v_poster_url
return P2vF2StatusResponse(
id=row.id,
status=row.status, # type: ignore[arg-type]
template_id=row.template_id,
error=_error_of(row),
artifacts=artifacts,
)
try:
p2v_job = await client.request_json("GET", f"/api/f2/jobs/{row.p2v_job_id}")
except P2vUpstreamError as e:
if e.status_code == 404:
# P2V 재기동 등으로 잡 소실 — 결과를 받을 길이 없으니 실패·환불
await f2_service.fail_job(
session, row, "서버 재시작으로 작업이 유실되었습니다. 크레딧을 환불했습니다"
)
await session.commit()
return P2vF2StatusResponse(
id=row.id, status="failed", template_id=row.template_id,
error=_error_of(row), artifacts={},
)
raise
await f2_service.sync_from_p2v(session, row, p2v_job)
status = row.status
if p2v_job.get("status") == "done" and row.archived_at is None:
# Blob 업로드 전까지는 진행 중으로 보인다 — URL 없는 done 창을 만들지 않는다
status = "archiving"
background_tasks.add_task(archive_service.archive_f2, row.id)
await session.commit()
return P2vF2StatusResponse(
id=row.id,
status=status, # type: ignore[arg-type]
stage=p2v_job.get("stage"),
stages=p2v_job.get("stages"),
template_id=row.template_id,
queue_size=p2v_job.get("queue_size"),
error=_error_of(row),
artifacts={}, # 결과 이미지는 아카이브 완료 후에만 내려간다
)
+81
View File
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""P2V 정적 파일 경량 프록시.
결과물(영상·결과 이미지)은 Azure Blob 공개 URL 로 나가므로 여기를 지나지 않는다.
이 프록시는 **작은 이미지 두 종류**만 중계한다:
- regions/ — 검수용 영역분석 이미지. 사용자 포스터가 담기므로 로그인 필수.
(프론트는 authenticatedFetch → blob URL 로 <img> 에 넣는다)
- templates/, user_templates/ — 템플릿 썸네일. `<img src>` 는 인증 헤더를 못
붙이고 내용도 공개 명화(퍼블릭 도메인)라 인증 없이 중계한다.
인증 라우트를 분리한 이유(2026-08-26 리뷰 Critical 반영): 401 응답을 직접 만들면
프로젝트 표준 포맷(detail={"code", "message"})과 어긋나 authenticatedFetch 의
토큰 자동 갱신이 무력화되고 강제 로그아웃이 난다. get_current_user 의존성에
맡기면 만료/누락 케이스 모두 표준 예외 포맷으로 나간다.
render/·uploads/·f2/ 는 어느 라우트에도 없다 — 원본·결과물은 Blob 으로만 나간다.
Range 미지원(대용량 스트리밍 용도가 아니다).
"""
from fastapi import APIRouter, Depends, Response
from app.p2v.constants import PUBLIC_FILE_PREFIXES
from app.p2v.exceptions import P2vDisabledError, P2vFileNotFoundError
from app.p2v.services import client
from app.user.dependencies.auth import get_current_user
from app.user.models import User
from app.utils.logger import get_logger
from config import p2v_settings
logger = get_logger("p2v")
router = APIRouter(prefix="/p2v", tags=["P2V"])
def _validate_path(path: str) -> None:
"""경로 탈출 방어. uvicorn 이 라우팅 전에 percent-decoding 하므로
%2e%2e 우회도 여기서 걸린다 (2026-08-26 리뷰에서 검증됨)."""
if not p2v_settings.P2V_ENABLED:
raise P2vDisabledError()
if ".." in path or path.startswith("/") or "\\" in path:
raise P2vFileNotFoundError()
async def _relay(path: str, cache: str) -> Response:
content, content_type = await client.download(f"/files/{path}")
return Response(
content=content,
media_type=content_type,
headers={"Cache-Control": cache},
)
# 정적 경로가 더 구체적이므로 catch-all 보다 먼저 선언한다 (ssulbox 라우트 순서 관례)
@router.get(
"/files/regions/{path:path}",
summary="검수용 영역분석 이미지 중계 (로그인 필수)",
responses={
401: {"description": "인증 실패 (표준 인증 오류 포맷 — 토큰 갱신 대상)"},
404: {"description": "없는 파일"},
},
)
async def proxy_region_file(
path: str,
current_user: User = Depends(get_current_user),
) -> Response:
_validate_path(path)
return await _relay(f"regions/{path}", cache="private, max-age=300")
@router.get(
"/files/{path:path}",
summary="P2V 공개 파일 중계 (템플릿 썸네일 전용)",
responses={404: {"description": "화이트리스트 밖이거나 없는 파일"}},
)
async def proxy_public_file(path: str) -> Response:
_validate_path(path)
if not path.startswith(PUBLIC_FILE_PREFIXES):
# render/·uploads/ 등 — 존재 여부를 노출하지 않는다
raise P2vFileNotFoundError()
return await _relay(path, cache="public, max-age=3600")