Skip to content

Commit a2ab942

Browse files
authored
Merge pull request #68 from BUAA-SE-coders007/fix/67
[fix]: 限制上传文献为pdf
2 parents 824f17f + 30f0118 commit a2ab942

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

app/api/v1/endpoints/article.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818

1919
@router.post("/uploadToSelfFolder", response_model="dict")
2020
async def upload_to_self_folder(folder_id: int = Query(...), article: UploadFile = File(...), db: AsyncSession = Depends(get_db)):
21-
# 由前端保证上传的为 PDF
21+
# 检查上传的必须为 PDF
22+
head = await article.read(5) # 读取文件的前 5 个字节,用于魔数检测
23+
if not head.startswith(b"%PDF-"):
24+
raise HTTPException(status_code=405, detail="File uploaded must be a PDF.")
25+
await article.seek(0) # 重置文件指针位置
26+
2227
# 用文件名(不带扩展名)作为 Article 名称
2328
name = os.path.splitext(article.filename)[0]
2429

0 commit comments

Comments
 (0)