取消ZIP材料包照片数量限制,保留文件大小校验
This commit is contained in:
+3
-7
@@ -56,8 +56,8 @@ def now_ms(): return int(time.time() * 1000)
|
||||
|
||||
|
||||
async def validate_photos(db, ids, owner_id, existing=()):
|
||||
if not 1 <= len(ids) <= 5 or len(set(ids)) != len(ids):
|
||||
raise HTTPException(422, '一楼活动须上传1至5张照片,照片不能重复')
|
||||
if not ids or len(set(ids)) != len(ids):
|
||||
raise HTTPException(422, '一楼活动须至少上传1张照片,照片不能重复')
|
||||
rows = (await db.execute(select(center_photos).where(center_photos.c.id.in_(ids)))).mappings().all()
|
||||
if len(rows) != len(ids) or any(r['owner_id'] != owner_id and r['id'] not in existing for r in rows):
|
||||
raise HTTPException(403, '照片不存在或不属于当前账号')
|
||||
@@ -94,7 +94,7 @@ def convert_package(raw):
|
||||
allowed={'.jpg','.jpeg','.png','.webp','.pdf','.doc','.docx','.xls','.xlsx','.ppt','.pptx','.txt','.csv','.md'}
|
||||
with zipfile.ZipFile(io.BytesIO(raw)) as archive:
|
||||
entries=archive.infolist()
|
||||
if len(entries)>100 or sum(e.file_size for e in entries)>100*1024*1024:raise ValueError('ZIP最多100项,展开后不超过100MB')
|
||||
if sum(e.file_size for e in entries)>100*1024*1024:raise ValueError('ZIP展开后不超过100MB')
|
||||
for entry in entries:
|
||||
name=entry.filename.replace('\\','/');path=PurePosixPath(name)
|
||||
if path.is_absolute() or '..' in path.parts or ':' in name or '\x00' in name or ((entry.external_attr>>16)&0o170000)==0o120000:raise ValueError('ZIP包含不安全路径或链接')
|
||||
@@ -108,7 +108,6 @@ def convert_package(raw):
|
||||
if path.suffix.lower() in {'.jpg','.jpeg','.png','.webp'}:
|
||||
if entry.file_size>8*1024*1024:raise ValueError('包内单张照片不超过8MB')
|
||||
photos.append(convert_photo(archive.read(entry)))
|
||||
if len(photos)>5:raise ValueError('ZIP内最多5张活动照片')
|
||||
if not photos:raise ValueError('ZIP内至少需要1张JPG、PNG或WebP活动照片')
|
||||
return photos
|
||||
|
||||
@@ -123,11 +122,8 @@ def install(app, get_db, current_user, photo_root, visible_clause, approver_emai
|
||||
async for chunk in request.stream():
|
||||
raw.extend(chunk)
|
||||
if len(raw)>30*1024*1024:raise HTTPException(413,'ZIP材料包最大30MB')
|
||||
count=(await db.execute(select(func.count()).select_from(center_photos).where(center_photos.c.owner_id==viewer['id'],center_photos.c.created_at>now_ms()-86400000))).scalar()
|
||||
if count>=100:raise HTTPException(429,'今日照片上传已达上限')
|
||||
try:photos=await asyncio.to_thread(convert_package,bytes(raw))
|
||||
except Exception as exc:raise HTTPException(422,str(exc) if isinstance(exc,ValueError) else 'ZIP材料包无效或损坏') from exc
|
||||
if count+len(photos)>100:raise HTTPException(429,'材料包中的照片超过今日上传上限')
|
||||
key=uuid.uuid4().hex;folder=photo_root();folder.mkdir(parents=True,exist_ok=True);paths=[];ids=[]
|
||||
try:
|
||||
package=folder/(key+'.zip');paths.append(package);await asyncio.to_thread(package.write_bytes,bytes(raw))
|
||||
|
||||
+2
-2
@@ -481,7 +481,7 @@ class ChangeEmailIn(NewEmailIn):
|
||||
class ActivityIn(BaseModel):
|
||||
material_id: Optional[str] = Field(default=None, max_length=32)
|
||||
record_type: Literal['normal', 'center'] = 'normal'
|
||||
photo_ids: list[str] = Field(default_factory=list, max_length=5)
|
||||
photo_ids: list[str] = Field(default_factory=list)
|
||||
title: str = Field(min_length=1, max_length=120)
|
||||
start_at: int = Field(gt=0)
|
||||
location: str = Field(default="", max_length=120)
|
||||
@@ -501,7 +501,7 @@ class ActivityIn(BaseModel):
|
||||
class ActivityPatch(BaseModel):
|
||||
material_id: Optional[str] = Field(default=None, max_length=32)
|
||||
record_type: Optional[Literal['normal', 'center']] = None
|
||||
photo_ids: Optional[list[str]] = Field(default=None, max_length=5)
|
||||
photo_ids: Optional[list[str]] = None
|
||||
title: Optional[str] = Field(default=None, max_length=120)
|
||||
start_at: Optional[int] = Field(default=None, gt=0)
|
||||
location: Optional[str] = Field(default=None, max_length=120)
|
||||
|
||||
Reference in New Issue
Block a user