Files
o2o-weather/backend/services/external/image_proxy.py
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

40 lines
1.5 KiB
Python

from urllib.parse import urlsplit
import httpx
from config.settings import settings
# 공유 이미지에 행사 사진을 그리려고 대신 받아 준다. 사진 서버가 브라우저가 픽셀을 읽는 것(CORS)을 막기 때문. 허용 호스트는 IMAGE_PROXY_HOSTS.
MAX_BYTES = 5 * 1024 * 1024
TIMEOUT_SEC = 10.0
class ImageFetchFailed(Exception):
pass
def allowed(url: str) -> bool:
"""https, 허용한 호스트, 기본 포트, 계정 정보 없음일 때만."""
try:
u = urlsplit(url)
port = u.port
except ValueError:
return False
return u.scheme == "https" and u.hostname in settings.image_proxy_host_set and port in (None, 443) and not u.username and not u.password
async def fetch_image(client: httpx.AsyncClient, url: str) -> tuple[bytes, str]:
"""이미지 바이트와 content-type. 리다이렉트는 따라가지 않는다."""
async with client.stream("GET", url, follow_redirects=False) as res:
if res.status_code != 200:
raise ImageFetchFailed(f"status {res.status_code}")
ctype = res.headers.get("content-type", "").split(";")[0].strip().lower()
if not ctype.startswith("image/") or ctype == "image/svg+xml":
raise ImageFetchFailed(f"content-type {ctype or '-'}")
buf = bytearray()
async for chunk in res.aiter_bytes():
buf += chunk
if len(buf) > MAX_BYTES:
raise ImageFetchFailed("too large")
return bytes(buf), ctype