Skip to content

Commit

Permalink
Merge pull request #107 from Do1e/master
Browse files Browse the repository at this point in the history
add: #103, add max_save_seconds config
  • Loading branch information
vastsa authored Nov 28, 2023
2 parents 4516e44 + 3b84035 commit 5d08e69
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
17 changes: 15 additions & 2 deletions apps/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import datetime
import uuid
import os
from fastapi import UploadFile
from fastapi import UploadFile, HTTPException

from apps.base.depends import IPRateLimit
from apps.base.models import FileCodes
from core.settings import settings
from core.utils import get_random_num, get_random_string
from core.utils import get_random_num, get_random_string, max_save_times_desc


async def get_file_path_name(file: UploadFile):
Expand Down Expand Up @@ -43,11 +43,24 @@ async def get_expire_info(expire_value: int, expire_style: str):
:return: expired_at 过期时间, expired_count 可用次数, used_count 已用次数, code 随机码
"""
expired_count, used_count, now, code = -1, 0, datetime.datetime.now(), None
if settings.max_save_seconds > 0:
max_timedelta = datetime.timedelta(seconds=settings.max_save_seconds)
detail = await max_save_times_desc(settings.max_save_seconds)
detail = f'保存时间超过限制,{detail[0]}'
else:
max_timedelta = datetime.timedelta(days=7)
detail = '保存时间超过限制,最长保存时间:7天'
if expire_style == 'day':
if datetime.timedelta(days=expire_value) > max_timedelta:
raise HTTPException(status_code=403, detail=detail)
expired_at = now + datetime.timedelta(days=expire_value)
elif expire_style == 'hour':
if datetime.timedelta(hours=expire_value) > max_timedelta:
raise HTTPException(status_code=403, detail=detail)
expired_at = now + datetime.timedelta(hours=expire_value)
elif expire_style == 'minute':
if datetime.timedelta(minutes=expire_value) > max_timedelta:
raise HTTPException(status_code=403, detail=detail)
expired_at = now + datetime.timedelta(minutes=expire_value)
elif expire_style == 'count':
expired_at = now + datetime.timedelta(days=1)
Expand Down
1 change: 1 addition & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'name': '文件快递柜-FileCodeBox',
'description': '开箱即用的文件快传系统',
'keywords': 'FileCodeBox,文件快递柜,口令传送箱,匿名口令分享文本,文件',
'max_save_seconds': 0,
's3_access_key_id': '',
's3_secret_access_key': '',
's3_bucket_name': '',
Expand Down
32 changes: 32 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,35 @@ async def get_file_url(code: str):
:return:
"""
return f'/share/download?key={await get_select_token(code)}&code={code}'

async def max_save_times_desc(max_save_seconds: int):
"""
获取最大保存时间的描述
:param max_save_seconds:
:return:
"""
def gen_desc_zh(value: int, desc: str):
if value > 0:
return f'{value}{desc}'
else:
return ''
def gen_desc_en(value: int, desc: str):
if value > 0:
ret = f'{value} {desc}'
if value > 1:
ret += 's'
ret += ' '
return ret
else:
return ''
max_timedelta = datetime.timedelta(seconds=max_save_seconds)
desc_zh, desc_en = '最长保存时间:', 'Max save time: '
desc_zh += gen_desc_zh(max_timedelta.days, '天')
desc_en += gen_desc_en(max_timedelta.days, 'day')
desc_zh += gen_desc_zh(max_timedelta.seconds // 3600, '小时')
desc_en += gen_desc_en(max_timedelta.seconds // 3600, 'hour')
desc_zh += gen_desc_zh(max_timedelta.seconds % 3600 // 60, '分钟')
desc_en += gen_desc_en(max_timedelta.seconds % 3600 // 60, 'minute')
desc_zh += gen_desc_zh(max_timedelta.seconds % 60, '秒')
desc_en += gen_desc_en(max_timedelta.seconds % 60, 'second')
return desc_zh, desc_en
23 changes: 21 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
# @Software: PyCharm
import asyncio
import os
import re

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import HTMLResponse
from starlette.responses import HTMLResponse, FileResponse
from starlette.staticfiles import StaticFiles
from tortoise.contrib.fastapi import register_tortoise

from apps.base.views import share_api
from apps.admin.views import admin_api
from core.settings import data_root, settings
from core.tasks import delete_expire_files
from core.utils import max_save_times_desc

app = FastAPI()

Expand All @@ -26,7 +28,24 @@
allow_headers=["*"],
)

app.mount('/assets', StaticFiles(directory='./fcb-fronted/dist/assets'), name="assets")
@app.get('/assets/{file_path:path}')
async def assets(file_path: str):
if settings.max_save_seconds > 0:
if re.match(r'SendView-[\d|a-f|A-F]+\.js', file_path):
with open(f'./fcb-fronted/dist/assets/{file_path}', 'r', encoding='utf-8') as f:
# 删除永久保存选项
content = f.read()
content = content.replace('_(c,{label:e(r)("send.expireData.forever"),value:"forever"},null,8,["label"]),', '')
return HTMLResponse(content=content, media_type='text/javascript')
if re.match(r'index-[\d|a-f|A-F]+\.js', file_path):
with open(f'./fcb-fronted/dist/assets/{file_path}', 'r', encoding='utf-8') as f:
# 更改本文描述
desc_zh, desc_en = await max_save_times_desc(settings.max_save_seconds)
content = f.read()
content = content.replace('天数<7', desc_zh)
content = content.replace('Days <7', desc_en)
return HTMLResponse(content=content, media_type='text/javascript')
return FileResponse(f'./fcb-fronted/dist/assets/{file_path}')

register_tortoise(
app,
Expand Down

0 comments on commit 5d08e69

Please sign in to comment.