Files
o2o-weather/backend/router/v1/image/image.py
T
minaChoi 5e2846ff72 feat: 공유를 비율 고르기 + 공유하기 하나로 줄이고 이미지에 행사 사진·우리 주소 넣기
- 공유 이미지 9:16·4:5·1:1, 위 로고·지역 날짜 / 카드 / 아래 주소 알약 (시안 A)
- 2장 카드는 한 이미지에 묶음, 버튼만 빼고 링크(장소)는 남김
- 웹폰트는 카드 글자에 필요한 조각만 data URL 로 넣어 글꼴이 안 깨지게
- 문구 고르기·링크 복사 제거, 공유하기 한 번에 이미지 + 제목·주소 글
- 행사 사진은 백엔드 /v1/image 가 대신 받음 (IMAGE_PROXY_HOSTS 만 허용)
- 공유 주소는 VITE_SITE_URL 하나로
2026-10-07 15:43:18 +09:00

23 lines
1.2 KiB
Python

import httpx
from fastapi import APIRouter, HTTPException, Query
from fastapi.responses import Response
from common.logger import LOG
from services.external.image_proxy import TIMEOUT_SEC, ImageFetchFailed, allowed, fetch_image
router = APIRouter(prefix="/v1/image", tags=["image"])
# 이미지 자체를 돌려주므로 다른 API 와 달리 실패는 HTTP 상태 코드로 알린다
@router.get("", summary="행사 사진 대신 받기 (공유 이미지용, 허용한 호스트만)", responses={200: {"content": {"image/*": {}}}, 400: {}, 502: {}})
async def get_image(url: str = Query(..., max_length=2048, description="원본 이미지 주소")) -> Response:
if not allowed(url):
raise HTTPException(status_code=400, detail="url not allowed")
try:
async with httpx.AsyncClient(timeout=TIMEOUT_SEC, headers={"User-Agent": "nalssigak-bot"}) as client:
data, ctype = await fetch_image(client, url)
except (ImageFetchFailed, httpx.HTTPError) as e:
LOG.warning(f"[image] fetch failed {url}: {e!r}")
raise HTTPException(status_code=502, detail="image fetch failed") from e
return Response(content=data, media_type=ctype, headers={"Cache-Control": "public, max-age=86400", "X-Content-Type-Options": "nosniff"})