From 8d62f6ec338163592dc4fa424c50d1bea8f478a2 Mon Sep 17 00:00:00 2001 From: Jialin0502 Date: Sun, 28 Jul 2024 21:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=9C=AC=E5=88=86=E4=BA=AB=E5=90=8E?= =?UTF-8?q?=E7=AB=AF=E5=A4=A7=E5=B0=8F=E9=99=90=E5=88=B6=EF=BC=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=99=90=E5=88=B6=E8=BF=94=E5=9B=9E=E4=B8=BAMB?= =?UTF-8?q?=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/base/views.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/base/views.py b/apps/base/views.py index c838e2b..8e553eb 100644 --- a/apps/base/views.py +++ b/apps/base/views.py @@ -23,6 +23,12 @@ share_api = APIRouter( # 分享文本的API @share_api.post('/text/', dependencies=[Depends(admin_required)]) async def share_text(text: str = Form(...), expire_value: int = Form(default=1, gt=0), expire_style: str = Form(default='day'), ip: str = Depends(ip_limit['upload'])): + # 获取大小 + text_size = len(text.encode('utf-8')) + # 限制 222KB + max_txt_size = 222 * 1024 # 转换为字节 + if text_size > max_txt_size: + raise HTTPException(status_code=403, detail=f'内容过多,建议采用文件形式') # 获取过期信息 expired_at, expired_count, used_count, code = await get_expire_info(expire_value, expire_style) # 创建一个新的FileCodes实例 @@ -47,9 +53,10 @@ async def share_text(text: str = Form(...), expire_value: int = Form(default=1, @share_api.post('/file/', dependencies=[Depends(admin_required)]) async def share_file(expire_value: int = Form(default=1, gt=0), expire_style: str = Form(default='day'), file: UploadFile = File(...), ip: str = Depends(ip_limit['upload'])): - # 检查文件大小是否超过限制 - if file.size > int(settings.uploadSize): - raise HTTPException(status_code=403, detail=f'文件大小超过限制,最大为{settings.uploadSize}字节') + if file.size > settings.uploadSize: + # 转换为 MB 并格式化输出 + max_size_mb = settings.uploadSize / (1024 * 1024) + raise HTTPException(status_code=403, detail=f'大小超过限制,最大为{max_size_mb:.2f} MB') # 获取过期信息 if expire_style not in settings.expireStyle: raise HTTPException(status_code=400, detail='过期时间类型错误')