Refine seller store implementation

This commit is contained in:
R524809
2026-08-31 14:37:28 +08:00
parent 957289f717
commit 2a667b4f3b
27 changed files with 1650 additions and 624 deletions
+29 -3
View File
@@ -11,8 +11,17 @@ import uuid
from dataclasses import dataclass
from api.proxy import guess_referer
from config import get_settings
from models import ProductAsset
from services.storage import download_bytes, get_storage, image_extension, local_path, read_local_bytes, storage_key_from_url
from services.storage import (
download_bytes,
get_storage,
image_extension,
local_path,
normalize_image_bytes,
read_local_bytes,
storage_key_from_url,
)
from services.watermark import apply_watermark
@@ -46,8 +55,20 @@ class ImageAssetService:
return read_local_bytes(path), mimetypes.guess_type(path.name)[0] or "image/jpeg"
return await download_bytes(url, referer=guess_referer(url))
async def save_original(self, asset: ProductAsset, data: bytes, content_type: str = "") -> str:
async def save_original(
self,
asset: ProductAsset,
data: bytes,
content_type: str = "",
max_bytes: int | None = None,
) -> str:
"""写入该素材唯一的无水印原图,并将 current 同步到原图。"""
if asset.type == "img":
normalized = normalize_image_bytes(data, max_bytes=max_bytes)
data = normalized.data
content_type = normalized.content_type
asset.width = normalized.width
asset.height = normalized.height
key = self._key(asset, "original", data, content_type)
url = await self.storage.save_bytes(data, key, content_type)
asset.original_url = url
@@ -60,7 +81,12 @@ class ImageAssetService:
async def save_original_from_url(self, asset: ProductAsset, url: str) -> str:
data, content_type = await self._read(url)
return await self.save_original(asset, data, content_type)
return await self.save_original(
asset,
data,
content_type,
max_bytes=get_settings().image_download_max_bytes,
)
async def ensure_original(self, asset: ProductAsset) -> tuple[bytes, str]:
"""为旧记录补齐原图字段;首次触碰旧素材时迁入新的商品/素材路径。"""