From 71e7c6170b14cf3da6980fdeabe479434b94744a Mon Sep 17 00:00:00 2001 From: lan Date: Sat, 11 Feb 2023 19:41:44 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=96=B0=E5=A2=9E=E6=B0=B8=E4=B9=85?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=EF=BC=8C=E4=BD=86=E6=98=AF=E4=BC=9A=E6=B0=B8?= =?UTF-8?q?=E4=B9=85=E5=8D=A0=E7=94=A8=E4=B8=80=E4=B8=AA=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=BE=85=E8=A7=A3=E5=86=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 8 ++++++-- settings.py | 3 ++- templates/admin.html | 3 ++- templates/index.html | 21 ++++++++++++--------- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index abcfae7..6ebbefc 100644 --- a/main.py +++ b/main.py @@ -176,11 +176,14 @@ async def get_file(code: str, token: str, ip: str = Depends(error_ip_limit), s: 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), ip: str = Depends(upload_ip_limit), s: AsyncSession = Depends(get_session)): - code = await get_code(s) if style == '2': if value > settings.MAX_DAYS: raise HTTPException(status_code=400, detail=f"最大有效天数为{settings.MAX_DAYS}天") - exp_time = datetime.datetime.now() + datetime.timedelta(days=value) + # 如果天数大于0,就设置过期时间,否则就设置为永久,无过期时间 + if settings.ENABLE_PERMANENT and value < 0: + exp_time = None + else: + exp_time = datetime.datetime.now() + datetime.timedelta(days=value) exp_count = -1 elif style == '1': if value < 1: @@ -199,6 +202,7 @@ async def share(background_tasks: BackgroundTasks, text: str = Form(default=None background_tasks.add_task(storage.save_file, file, _text) else: size, _text, _type, name = len(text), text, 'text', '文本分享' + code = await get_code(s) s.add(Codes(code=code, text=_text, size=size, type=_type, name=name, count=exp_count, exp_time=exp_time, key=key)) await s.commit() upload_ip_limit.add_ip(ip) diff --git a/settings.py b/settings.py index 20fa5db..b7134e0 100644 --- a/settings.py +++ b/settings.py @@ -1,7 +1,6 @@ import uuid from starlette.config import Config -import configparser # 配置文件.env,存放为data/.env config = Config("data/.env") @@ -32,6 +31,8 @@ class Settings: ERROR_MINUTE = config('ERROR_MINUTE', cast=int, default=10) # 上传次数 UPLOAD_COUNT = config('UPLOAD_COUNT', cast=int, default=60) + # 是否允许永久保存 + ENABLE_PERMANENT = config('ENABLE_PERMANENT', cast=bool, default=True) # 上传限制分钟数 UPLOAD_MINUTE = config('UPLOAD_MINUTE', cast=int, default=1) # 删除过期文件的间隔(分钟) diff --git a/templates/admin.html b/templates/admin.html index c85ef0c..7f8fb9b 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -43,7 +43,8 @@
取件码:${ file.code }
文件名:${ file.name }
次   数:${ file.count }
-
到   期:${ file.exp_time.slice(0,19) }
+
到   期:${ file.exp_time.slice(0,19) }
+
到   期:永不过期
内   容:${ file.text }
diff --git a/templates/index.html b/templates/index.html index ef665a3..e6aee18 100644 --- a/templates/index.html +++ b/templates/index.html @@ -138,15 +138,18 @@ - - - - - - - - + + + + + + + + + + 文件