feat: 新增 POST /assets/batch 批量获取素材详情接口 #1418
Reference in New Issue
Block a user
Delete Branch "fix/assets-batch-endpoint"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
问题
前端
usePreviewAssets.ts调用POST /assets/batch时返回 405 Method Not Allowed。根因:后端没有
/batchGET/POST 端点,请求命中了GET /{asset_id}(batch被当成 asset_id),但 HTTP method 不匹配,返回 405。修复
新增
POST /assets/batch端点,支持根据 ID 列表批量获取素材详情。接口规格
POST /api/v1/assets/batch{ "ids": ["id1", "id2", ...] }(最多 200 个 ID)List[AssetResponse],包含file_url、duration、width、height、fps、codec、status等完整元数据改动文件
apps/api/app/schemas/asset.py:新增BatchGetRequestschemaapps/api/app/api/routes/assets.py:新增batch_get_assets端点前端适配
前端
usePreviewAssets.ts已有 fallback 逻辑(批量失败则逐个获取),新增接口后无需修改前端代码。前端 usePreviewAssets.ts 调用 POST /assets/batch 时返回 405 Method Not Allowed, 原因是后端没有 /batch GET/POST 端点,请求命中了 GET /{asset_id}(batch 被当成 asset_id)。 新增: - BatchGetRequest schema:接收 { ids: string[] },最多 200 个 ID - POST /assets/batch 端点:调用 asset_repository.find_by_ids() 批量查询 - 返回 List[AssetResponse],包含 file_url、duration、width、height 等完整元数据 改动文件: - apps/api/app/schemas/asset.py:新增 BatchGetRequest - apps/api/app/api/routes/assets.py:新增 batch_get_assets 端点【阻塞级判定】
📊 审查概览
🔴 阻塞级问题(必须修复)
batch_get_assets接口直接根据 ID 列表查询素材并返回,但未对查询结果进行权限校验(如调用check_project_access)。攻击者可以通过遍历 ID 获取项目中无权访问的其他素材详情,造成敏感信息泄露。update_asset_review_status或batch_delete_assets的实现,在返回数据前,必须遍历items并对每个 item 调用check_project_access(item.project_id, authenticated_user)进行权限校验,或者确保asset_repository.find_by_ids方法内部已包含基于用户的权限过滤逻辑。💡 改进建议(不阻塞合并)
list[AssetResponse],而response_model使用了typing.List。建议保持一致,例如统一使用List[AssetResponse](如果需要兼容旧版 Python)或统一使用list[AssetResponse]。✅ 良好实践
BatchGetRequest中对ids字段添加了min_length=1和max_length=MAX_BATCH_SIZE,有效防止了空请求或过大的请求负载,符合防御性编程原则。🤖 由 AI 代码审查机器人自动生成 | 2026-08-18 05:06:34 | 模型:
🚀 预览环境已部署
CI全绿,自动审批通过。
CI全绿,自动审批通过。
🗑️ 预览环境已清理
PR #1418 已关闭或合并,对应的预览环境已被清理。