支持一楼活动照片多选直传,ZIP改为可选文档附件

This commit is contained in:
2026-11-12 14:01:12 +08:00
parent f8b3f3f7d7
commit 3404fff5fc
8 changed files with 200 additions and 56 deletions
+10 -8
View File
@@ -54,15 +54,16 @@ class ReviewIn(BaseModel):
class MaterialIn(BaseModel):
material_id: str = Field(min_length=32, max_length=32)
material_id: str | None = Field(default=None, min_length=32, max_length=32)
photo_ids: list[str] = Field(default_factory=list)
def now_ms(): return int(time.time() * 1000)
async def validate_photos(db, ids, owner_id, existing=()):
if not ids or len(set(ids)) != len(ids):
raise HTTPException(422, '一楼活动须至少上传1张照片,照片不能重复')
async def validate_photos(db, ids, owner_id, existing=(), allow_empty=False):
if (not ids and not allow_empty) or len(set(ids)) != len(ids):
raise HTTPException(422, '请至少上传照片或ZIP附件,照片不能重复')
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, '照片不存在或不属于当前账号')
@@ -71,7 +72,7 @@ async def validate_photos(db, ids, owner_id, existing=()):
async def save_meta(db, activity_id, kind, photo_ids, owner_id, material_id=None):
if kind != 'center':
return
await validate_photos(db, photo_ids, owner_id)
await validate_photos(db, photo_ids, owner_id, allow_empty=bool(material_id))
await db.execute(insert(center_activity_meta).values(activity_id=activity_id, photo_ids=json.dumps(photo_ids),material_id=material_id))
if material_id: await link_material(db, activity_id, material_id)
@@ -123,7 +124,7 @@ def convert_photo(raw):
def convert_package(raw):
photos=[];names=set()
photos=[];names=set();file_count=0
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()
@@ -137,11 +138,12 @@ def convert_package(raw):
if entry.is_dir():continue
if name.startswith('__MACOSX/') or path.name=='.DS_Store':continue
if path.suffix.lower() not in allowed:raise ValueError('ZIP只支持图片、PDF、Office文档和文本材料')
file_count += 1
if entry.file_size>20*1024*1024:raise ValueError('包内单个文档不超过20MB')
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 not photos:raise ValueError('ZIP内至少需要1张JPG、PNG或WebP活动照片')
if not file_count:raise ValueError('ZIP附件不能为空')
return photos
@@ -190,7 +192,7 @@ def install(app, get_db, current_user, photo_root, visible_clause, approver_emai
raw.extend(chunk)
if len(raw) > 8 * 1024 * 1024: raise HTTPException(413, '每张照片最大8MB')
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, '今日照片上传已达100张')
if count >= 1000: raise HTTPException(429, '今日照片上传已达1000张')
try: data = await asyncio.to_thread(convert_photo, bytes(raw))
except Exception as exc: raise HTTPException(422, '照片无效,仅支持JPG、PNG、WebP,最大24百万像素') from exc
key = uuid.uuid4().hex