Skip to content

Commit 5543811

Browse files
committed
add:前端判断文件大小
add:自定义删除过期文件间隔,修复因间隔过短导致还没下载文件就没了
1 parent 1f14997 commit 5543811

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async def startup():
3939
index_html = open('templates/index.html', 'r', encoding='utf-8').read() \
4040
.replace('{{title}}', settings.TITLE) \
4141
.replace('{{description}}', settings.DESCRIPTION) \
42-
.replace('{{keywords}}', settings.KEYWORDS)
42+
.replace('{{keywords}}', settings.KEYWORDS) \
43+
.replace("'{{fileSizeLimit}}'", str(settings.FILE_SIZE_LIMIT))
4344
# 管理页面
4445
admin_html = open('templates/admin.html', 'r', encoding='utf-8').read() \
4546
.replace('{{title}}', settings.TITLE) \
@@ -116,7 +117,7 @@ async def index(code: str, ip: str = Depends(error_ip_limit), s: AsyncSession =
116117
if info.type != 'text':
117118
info.text = f'/select?code={code}'
118119
return {
119-
'detail': '取件成功,请点击"取"查看',
120+
'detail': f'取件成功,文件将在{settings.DELETE_EXPIRE_FILES_INTERVAL}分钟后删除',
120121
'data': {'type': info.type, 'text': info.text, 'name': info.name, 'code': info.code}
121122
}
122123

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ DATABASE_URL=sqlite+aiosqlite:///database.db
104104
DATA_ROOT=./static
105105
# 静态文件夹URL
106106
STATIC_URL=/static
107-
# 是否开启上传
108-
ENABLE_UPLOAD=true
109107
# 错误次数
110108
ERROR_COUNT=5
111109
# 错误限制分钟数
@@ -114,6 +112,8 @@ ERROR_MINUTE=10
114112
UPLOAD_COUNT=60
115113
# 上传限制分钟数
116114
UPLOAD_MINUTE=1
115+
# 删除过期文件的间隔(秒)
116+
DELETE_EXPIRE_FILES_INTERVAL=60
117117
# 管理地址
118118
ADMIN_ADDRESS=admin
119119
# 管理密码

readme_en.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ DATABASE_URL=sqlite+aiosqlite:///database.db
113113
DATA_ROOT=./static
114114
# 静态文件夹URL
115115
STATIC_URL=/static
116-
# 是否开启上传
117-
ENABLE_UPLOAD=true
118116
# 错误次数
119117
ERROR_COUNT=5
120118
# 错误限制分钟数
@@ -123,6 +121,8 @@ ERROR_MINUTE=10
123121
UPLOAD_COUNT=60
124122
# 上传限制分钟数
125123
UPLOAD_MINUTE=1
124+
# 删除过期文件的间隔(秒)
125+
DELETE_EXPIRE_FILES_INTERVAL=60
126126
# 管理地址
127127
ADMIN_ADDRESS=admin
128128
# 管理密码

settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
UPLOAD_COUNT = config('UPLOAD_COUNT', cast=int, default=60)
2828
# 上传限制分钟数
2929
UPLOAD_MINUTE = config('UPLOAD_MINUTE', cast=int, default=1)
30+
# 删除过期文件的间隔(秒)
31+
DELETE_EXPIRE_FILES_INTERVAL = config('DELETE_EXPIRE_FILES_INTERVAL', cast=int, default=10)
3032
# 管理地址
3133
ADMIN_ADDRESS = config('ADMIN_ADDRESS', cast=str, default="admin")
3234
# 管理密码

templates/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
multiple
103103
style="margin: 1rem 0;"
104104
:data="uploadData"
105+
:before-upload="beforeUpload"
105106
:headers="{'pwd':pwd}"
106107
:on-success="successUpload"
107108
:on-error="errorUpload"
@@ -195,6 +196,7 @@
195196
files: [],
196197
pageNum: 0,
197198
inputDisable: false,
199+
fileSizeLimit: '{{fileSizeLimit}}',
198200
pwd: localStorage.getItem('pwd') || '',
199201
uploadData: {
200202
style: '2',
@@ -322,6 +324,15 @@
322324
type: 'error'
323325
});
324326
},
327+
beforeUpload: function (file) {
328+
if (file.size > this.fileSizeLimit) {
329+
this.$message({
330+
message: `文件大小不能超过${this.fileSizeLimit/1024/1024}MB`,
331+
type: 'error'
332+
});
333+
return false
334+
}
335+
}
325336
}
326337
})
327338
</script>

utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def delete_expire_files():
3333
query = delete(Codes).where(Codes.id.in_(exps_ids))
3434
await s.execute(query)
3535
await s.commit()
36-
await asyncio.sleep(random.randint(2, 2))
36+
await asyncio.sleep(settings.DELETE_EXPIRE_FILES_INTERVAL)
3737

3838

3939
async def get_code(s: AsyncSession):

0 commit comments

Comments
 (0)