优化视频复刻和商品内容大小
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
|
||||
@@ -115,8 +116,23 @@ def _store_uploaded_asset(*, team, user, upload, asset_type: str, category: str,
|
||||
suffix = Path(upload.name).suffix.lower() or fallback_suffix
|
||||
asset_id = uuid.uuid4()
|
||||
object_key = f"teams/{team.id}/uploads/{asset_id}{suffix}"
|
||||
# 图片走尺寸闸:小于 300px 的图能存进来,但送火山审核会被 WidthTooSmall 拒 ——
|
||||
# 那时报错离上传很远(要到生成视频那一步),用户根本对不上号。
|
||||
width = height = None
|
||||
if asset_type == Asset.Type.IMAGE:
|
||||
from apps.assets.image_guard import ImageTooSmallError, ensure_reviewable_image
|
||||
|
||||
raw = upload.read()
|
||||
try:
|
||||
width, height = ensure_reviewable_image(raw, label=name or "图片")
|
||||
except ImageTooSmallError as exc:
|
||||
# 转成 DRF 校验错 → 400 + 原文;裸 ValueError 会变成 500
|
||||
raise ValidationError({"detail": str(exc)}) from exc
|
||||
fileobj = BytesIO(raw)
|
||||
else:
|
||||
fileobj = upload.file
|
||||
stored = TosStorage().upload_fileobj(
|
||||
fileobj=upload.file,
|
||||
fileobj=fileobj,
|
||||
object_key=object_key,
|
||||
content_type=upload.content_type or "application/octet-stream",
|
||||
)
|
||||
@@ -126,7 +142,8 @@ def _store_uploaded_asset(*, team, user, upload, asset_type: str, category: str,
|
||||
)
|
||||
AssetFile.objects.create(
|
||||
asset=asset, object_key=stored.object_key, bucket=stored.bucket,
|
||||
content_type=stored.content_type, size_bytes=stored.size_bytes, is_primary=True,
|
||||
content_type=stored.content_type, size_bytes=stored.size_bytes,
|
||||
width=width, height=height, is_primary=True,
|
||||
)
|
||||
return asset
|
||||
|
||||
|
||||
Reference in New Issue
Block a user