update:限制管理员上传,最长天数配置
This commit is contained in:
+6
-2
@@ -6,9 +6,13 @@ from fastapi import Header, HTTPException, Request
|
|||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
|
||||||
async def admin_required(pwd: Union[str, None] = Header(default=None)):
|
async def admin_required(pwd: Union[str, None] = Header(default=None), request: Request = None):
|
||||||
|
if 'share' in request.url.path:
|
||||||
|
if pwd != settings.ADMIN_PASSWORD and not settings.ENABLE_UPLOAD:
|
||||||
|
raise HTTPException(status_code=403, detail='本站上传功能已关闭,仅管理员可用')
|
||||||
|
else:
|
||||||
if not pwd or pwd != settings.ADMIN_PASSWORD:
|
if not pwd or pwd != settings.ADMIN_PASSWORD:
|
||||||
raise HTTPException(status_code=401, detail="密码错误")
|
raise HTTPException(status_code=401, detail="密码错误,请重新登录")
|
||||||
|
|
||||||
|
|
||||||
class IPRateLimit:
|
class IPRateLimit:
|
||||||
|
|||||||
@@ -121,16 +121,14 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession =
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.post('/share')
|
@app.post('/share', dependencies=[Depends(admin_required)], description='分享文件')
|
||||||
async def share(background_tasks: BackgroundTasks, pwd: str = Header(default=None), text: str = Form(default=None),
|
async def share(background_tasks: BackgroundTasks, text: str = Form(default=None),
|
||||||
style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(default=None),
|
style: str = Form(default='2'), value: int = Form(default=1), file: UploadFile = File(default=None),
|
||||||
ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)):
|
ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)):
|
||||||
if not settings.ENABLE_UPLOAD and pwd != settings.ADMIN_PASSWORD:
|
|
||||||
raise HTTPException(status_code=403, detail="上传功能已关闭")
|
|
||||||
code = await get_code(s)
|
code = await get_code(s)
|
||||||
if style == '2':
|
if style == '2':
|
||||||
if value > 7:
|
if value > settings.MAX_DAYS:
|
||||||
raise HTTPException(status_code=400, detail="最大有效天数为7天")
|
raise HTTPException(status_code=400, detail=f"最大有效天数为{settings.MAX_DAYS}天")
|
||||||
exp_time = datetime.datetime.now() + datetime.timedelta(days=value)
|
exp_time = datetime.datetime.now() + datetime.timedelta(days=value)
|
||||||
exp_count = -1
|
exp_count = -1
|
||||||
elif style == '1':
|
elif style == '1':
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ DATA_ROOT = './data/' + config('DATA_ROOT', cast=str, default=f"static")
|
|||||||
STATIC_URL = config('STATIC_URL', cast=str, default="/static")
|
STATIC_URL = config('STATIC_URL', cast=str, default="/static")
|
||||||
# 开启上传
|
# 开启上传
|
||||||
ENABLE_UPLOAD = config('ENABLE_UPLOAD', cast=bool, default=False)
|
ENABLE_UPLOAD = config('ENABLE_UPLOAD', cast=bool, default=False)
|
||||||
|
# 最长天数
|
||||||
|
MAX_DAYS = config('MAX_DAYS', cast=int, default=7)
|
||||||
# 错误次数
|
# 错误次数
|
||||||
ERROR_COUNT = config('ERROR_COUNT', cast=int, default=5)
|
ERROR_COUNT = config('ERROR_COUNT', cast=int, default=5)
|
||||||
# 错误限制分钟数
|
# 错误限制分钟数
|
||||||
|
|||||||
Reference in New Issue
Block a user