fix picture upload
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m59s

This commit is contained in:
zyc 2026-03-23 14:24:52 +08:00
parent 9113cdafc3
commit 6d4142fff0
2 changed files with 46 additions and 1 deletions

View File

@ -2726,8 +2726,10 @@ def _assets_api_call(func, *args, **kwargs):
return result, None
except AssetsAPIError as e:
logger.warning('Assets API error: %s', e)
# Surface readable message for known parameter errors
msg = e.api_message if hasattr(e, 'api_message') else str(e)
return None, Response(
{'error': 'assets_api_error', 'message': '素材服务暂时不可用,请稍后重试'},
{'error': 'assets_api_error', 'message': msg},
status=status.HTTP_502_BAD_GATEWAY,
)
except Exception as e:
@ -2775,6 +2777,27 @@ def asset_groups_view(request):
if not file:
return Response({'error': '请上传一张素材图片'}, status=status.HTTP_400_BAD_REQUEST)
# Validate image dimensions (Volcano Assets API requires 300-6000px)
try:
from PIL import Image
img = Image.open(file)
w, h = img.size
if w < 300 or h < 300:
return Response(
{'error': f'图片尺寸太小({w}x{h}),宽高均需 ≥ 300px'},
status=status.HTTP_400_BAD_REQUEST,
)
if w > 6000 or h > 6000:
return Response(
{'error': f'图片尺寸太大({w}x{h}),宽高均需 ≤ 6000px'},
status=status.HTTP_400_BAD_REQUEST,
)
file.seek(0) # Reset after PIL read
except ImportError:
pass # Pillow not installed, skip validation
except Exception:
pass # Not an image or corrupted, let TOS handle it
# Upload to TOS
try:
tos_url = tos_upload(file, folder='assets')
@ -2917,6 +2940,27 @@ def asset_group_add_asset_view(request, group_id):
if not file:
return Response({'error': '请上传文件'}, status=status.HTTP_400_BAD_REQUEST)
# Validate image dimensions (Volcano Assets API requires 300-6000px)
try:
from PIL import Image
img = Image.open(file)
w, h = img.size
if w < 300 or h < 300:
return Response(
{'error': f'图片尺寸太小({w}x{h}),宽高均需 ≥ 300px'},
status=status.HTTP_400_BAD_REQUEST,
)
if w > 6000 or h > 6000:
return Response(
{'error': f'图片尺寸太大({w}x{h}),宽高均需 ≤ 6000px'},
status=status.HTTP_400_BAD_REQUEST,
)
file.seek(0)
except ImportError:
pass
except Exception:
pass
name = request.data.get('name', '').strip() or file.name
# Upload to TOS

View File

@ -8,3 +8,4 @@ tos>=2.7,<3.0
requests>=2.31,<3.0
ip-region>=1.0
volcengine>=1.0.218
Pillow>=10.0