[feat] negodata/backend: 상품 이미지 업로드(Azure Blob) 엔드포인트 추가
POST /v1/item/image — SAS 토큰을 URL에 붙여 httpx PUT 으로 Azure Blob 업로드. 이미지 타입/용량 검증 + IMAGE_* 에러코드. config 에 azure_blob_base_url/sas/root 추가. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
49e682d693
commit
3280cab088
10 files changed
+287
-15
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
|
||||
from config.server_configs import storage_config
|
||||
|
||||
# 허용 content-type -> 저장 확장자. 목록 밖이면 업로드 거부.
|
||||
_EXT_BY_TYPE = {
|
||||
"image/jpeg": "jpg",
|
||||
"image/png": "png",
|
||||
"image/gif": "gif",
|
||||
"image/webp": "webp",
|
||||
}
|
||||
|
||||
# 업로드용 공유 클라이언트(연결 풀링).
|
||||
_shared_client: httpx.AsyncClient | None = None
|
||||
|
||||
|
||||
def _get_client() -> httpx.AsyncClient:
|
||||
global _shared_client
|
||||
if _shared_client is None or _shared_client.is_closed:
|
||||
_shared_client = httpx.AsyncClient(
|
||||
timeout=httpx.Timeout(60.0, connect=10.0),
|
||||
limits=httpx.Limits(max_keepalive_connections=10, max_connections=20),
|
||||
)
|
||||
return _shared_client
|
||||
|
||||
|
||||
def is_allowed_image(content_type: str) -> bool:
|
||||
return content_type in _EXT_BY_TYPE
|
||||
|
||||
|
||||
async def upload_image(company_id: str, content: bytes, content_type: str) -> str:
|
||||
"""Azure Blob 에 이미지를 올리고 접근 URL 을 돌려준다.
|
||||
blob 경로: <blob_root>/<company_id>/items/<uuid>.<ext>
|
||||
— company_id 로 멀티테넌트 격리.
|
||||
실패 시 예외를 던진다(호출측에서 IMAGE_UPLOAD_FAILED 처리). 저장값은 SAS 를 뗀 public URL."""
|
||||
ext = _EXT_BY_TYPE.get(content_type, "bin")
|
||||
blob_path = f"{storage_config.blob_root}/{company_id}/items/{uuid.uuid4().hex}.{ext}"
|
||||
|
||||
base = storage_config.azure_blob_base_url.rstrip("/")
|
||||
sas = storage_config.azure_blob_sas_token.strip("?'\"")
|
||||
public_url = f"{base}/{blob_path}"
|
||||
upload_url = f"{public_url}?{sas}"
|
||||
headers = {"Content-Type": content_type, "x-ms-blob-type": "BlockBlob"}
|
||||
|
||||
resp = await _get_client().put(upload_url, content=content, headers=headers)
|
||||
if resp.status_code not in (200, 201):
|
||||
raise RuntimeError(f"Azure Blob upload failed: status={resp.status_code} body={resp.text[:200]}")
|
||||
return public_url
|
||||
@@ -1,12 +1,14 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import Depends
|
||||
from fastapi import Depends, UploadFile
|
||||
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import items
|
||||
from common.enums import DBWRType, ErrorType
|
||||
from common.logger import LOG
|
||||
from common.models.gmodel import PageParams
|
||||
from services.azure_blob_client import is_allowed_image, upload_image as blob_upload_image
|
||||
from config.server_configs import storage_config
|
||||
from crud.item_crud import IItemCRUD, ItemCRUD
|
||||
from router.v1.item.protocol import (
|
||||
ItemCategory,
|
||||
@@ -15,6 +17,7 @@ from router.v1.item.protocol import (
|
||||
Res_DeleteItem,
|
||||
Res_Item,
|
||||
Res_ItemCategories,
|
||||
Res_ItemImage,
|
||||
Res_ItemList,
|
||||
)
|
||||
|
||||
@@ -162,3 +165,28 @@ class ItemService:
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
return res
|
||||
|
||||
async def upload_image(self, company_id: str, file: UploadFile) -> Res_ItemImage:
|
||||
"""상품 이미지를 Azure Blob 에 올리고 image_url 을 돌려준다.
|
||||
DB 는 건드리지 않는다 — 프론트가 이 URL 을 create/update 의 image_url 로 실어 보낸다."""
|
||||
res = Res_ItemImage()
|
||||
content_type = file.content_type or ""
|
||||
if not is_allowed_image(content_type):
|
||||
res.result.SetResult(ErrorType.IMAGE_INVALID_TYPE)
|
||||
return res
|
||||
|
||||
content = await file.read()
|
||||
if len(content) > storage_config.max_image_mb * 1024 * 1024:
|
||||
res.result.SetResult(ErrorType.IMAGE_TOO_LARGE)
|
||||
return res
|
||||
|
||||
try:
|
||||
res.image_url = await blob_upload_image(company_id, content, content_type)
|
||||
except Exception as ex:
|
||||
LOG.e_no_callstack(ex)
|
||||
res.result.SetResult(ErrorType.IMAGE_UPLOAD_FAILED)
|
||||
return res
|
||||
|
||||
res.filename = file.filename
|
||||
res.size = len(content)
|
||||
return res
|
||||
Reference in new issue
Block a user