Files
o2o-castad-backend/app/p2v/utils/blob.py
T
skkim b16d4a8f1b feat: P2V 파이프라인 연동 및 좋아요·댓글·공유 지원
- 무빙 포스터(p2v_video)·롱컷(p2v_playreel) 파이프라인과 워커, 갤러리·내 콘텐츠 노출
- 좋아요·댓글: comment/video_reaction에 p2v_playreel_id 추가, 종류별 CHECK 확장
- P2V 소프트 삭제(is_deleted), queued/running 삭제 차단
- 공개 상세·공유 OG 페이지 추가
- 삭제된 부모 댓글이 목록에서 사라지던 has_live_reply 서브쿼리 버그 수정
- 마이그레이션: migration_2026-10-15_p2v_reactions.sql
2026-10-15 10:42:30 +09:00

108 lines
4.0 KiB
Python

"""Azure Blob Storage 업로드.
SAS 토큰을 붙인 URL로 PUT 한 번. 경로 규칙은 부르는 쪽이 정한다.
클라이언트를 모듈에서 공유해 커넥션 풀을 재사용한다.
"""
import mimetypes
from pathlib import Path
from urllib.parse import quote
import httpx
from config import azure_blob_settings
BLOB_TYPE_HEADER = {"x-ms-blob-type": "BlockBlob"}
DEFAULT_CONTENT_TYPE = "application/octet-stream"
UPLOAD_TIMEOUT = httpx.Timeout(180.0, connect=10.0)
POOL_LIMITS = httpx.Limits(max_keepalive_connections=10, max_connections=20)
_client: httpx.AsyncClient | None = None
def get_client() -> httpx.AsyncClient:
global _client
if _client is None or _client.is_closed:
_client = httpx.AsyncClient(timeout=UPLOAD_TIMEOUT, limits=POOL_LIMITS)
return _client
async def close_client() -> None:
global _client
if _client is not None and not _client.is_closed:
await _client.aclose()
_client = None
def guess_content_type(path: str) -> str:
return mimetypes.guess_type(path)[0] or DEFAULT_CONTENT_TYPE
def public_url(path: str) -> str:
return f"{azure_blob_settings.AZURE_BLOB_BASE_URL.rstrip('/')}/{path.lstrip('/')}"
def sas_token() -> str:
# SAS 토큰이 따옴표나 ? 로 감싸여 오는 경우가 있다
return azure_blob_settings.AZURE_BLOB_SAS_TOKEN.strip("?'\"")
def upload_url(path: str) -> str:
return f"{public_url(path)}?{sas_token()}"
def with_sas(url: str) -> str:
"""컨테이너가 비공개면 읽기에도 토큰이 필요함
우리가 올린 URL일 때만 붙임
"""
token = sas_token()
base = azure_blob_settings.AZURE_BLOB_BASE_URL.rstrip("/")
if token and base and url.startswith(base) and "?" not in url:
return f"{url}?{token}"
return url
def as_attachment(filename: str) -> str:
"""이 값이 붙은 blob은 브라우저가 열면 내려받기가 된다.
<a download>은 다른 출처에 안 먹어 완성본은 이 길로 간다.
한글 파일명은 RFC 5987 형식이라야 온전히 건너간다.
"""
return f"attachment; filename*=UTF-8''{quote(filename)}"
async def upload_bytes(data: bytes, path: str, content_type: str | None = None,
disposition: str | None = None) -> str:
"""path 예: playreel/nol_26007416/clip.mp4. 공개 URL을 돌려준다."""
if not azure_blob_settings.AZURE_BLOB_BASE_URL or not azure_blob_settings.AZURE_BLOB_SAS_TOKEN:
raise RuntimeError("AZURE_BLOB_BASE_URL / AZURE_BLOB_SAS_TOKEN 없음")
headers = {"Content-Type": content_type or guess_content_type(path), **BLOB_TYPE_HEADER}
if disposition:
# 헤더 이름이 Content-Disposition이면 이 요청 본문의 성격으로 읽힌다.
# blob 속성으로 저장되는 것은 x-ms- 쪽이다
headers["x-ms-blob-content-disposition"] = disposition
response = await get_client().put(upload_url(path), content=data, headers=headers)
if response.status_code not in (200, 201):
print(f"[blob] 업로드 실패 {response.status_code} {path}: {response.text[:300]}",
flush=True)
raise RuntimeError(f"blob 업로드 실패 {response.status_code}")
return public_url(path)
async def upload_file(file_path: Path, path: str, content_type: str | None = None) -> str:
return await upload_bytes(file_path.read_bytes(), path,
content_type or guess_content_type(file_path.name))
async def download_bytes(url: str) -> bytes:
"""upload_bytes가 돌려준 URL을 그대로 받아 내용을 가져온다.
예외 메시지에 주소를 넣지 않는다 — 실패는 잡에 기록되어 화면까지 간다.
어느 파일이었는지는 로그를 본다.
"""
response = await get_client().get(with_sas(url))
if response.status_code != 200:
print(f"[blob] 다운로드 실패 {response.status_code}: {url}", flush=True)
raise RuntimeError(f"blob 다운로드 실패 {response.status_code}")
return response.content