Skip to content

Commit 757e8ec

Browse files
committed
add:上传前进行检测
1 parent 2fc95eb commit 757e8ec

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ async def startup():
4141
.replace('{{title}}', settings.TITLE) \
4242
.replace('{{description}}', settings.DESCRIPTION) \
4343
.replace('{{keywords}}', settings.KEYWORDS) \
44-
.replace("'{{fileSizeLimit}}'", str(settings.FILE_SIZE_LIMIT))
45-
# 管理页面
44+
.replace("'{{fileSizeLimit}}'", str(settings.FILE_SIZE_LIMIT)) \
45+
# 管理页面
4646
admin_html = open('templates/admin.html', 'r', encoding='utf-8').read() \
4747
.replace('{{title}}', settings.TITLE) \
4848
.replace('{{description}}', settings.DESCRIPTION) \
@@ -107,7 +107,7 @@ async def index():
107107

108108

109109
@app.get('/banner')
110-
async def banner(s: AsyncSession = Depends(get_session)):
110+
async def banner(request: Request, s: AsyncSession = Depends(get_session)):
111111
# 数据库查询config
112112
config = (await s.execute(select(Values).filter(Values.key == 'config'))).scalar_one_or_none()
113113
# 如果存在config,就返回config的value
@@ -119,6 +119,7 @@ async def banner(s: AsyncSession = Depends(get_session)):
119119
# 如果不存在config,就返回默认的banner
120120
return {
121121
'detail': 'banner',
122+
'enable': request.headers.get('pwd', '') == settings.ADMIN_PASSWORD,
122123
'data': [{
123124
'text': 'FileCodeBox',
124125
'url': 'https://github.com/vastsa/FileCodeBox',

templates/index.html

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
271271
fileSizeLimit: '{{fileSizeLimit}}',
272272
pwd: localStorage.getItem('pwd') || '',
273273
banners: [],
274+
enableUpload: false,
274275
uploadData: {
275276
style: '2',
276277
type: '1',
@@ -313,7 +314,12 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
313314
}
314315
});
315316
// 获取Banner
316-
axios.get('/banner').then(res => {
317+
axios.get('/banner', {
318+
headers: {
319+
pwd: localStorage.getItem('pwd')
320+
}
321+
}).then(res => {
322+
this.enableUpload = res.data.enable
317323
this.banners = res.data.data
318324
})
319325
},
@@ -385,24 +391,42 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
385391
changeInput: function (value) {
386392
this.code = value;
387393
},
394+
checkFile: function (file) {
395+
if (!this.enable) {
396+
this.$message({
397+
message: '上传功能已关闭',
398+
type: 'error'
399+
});
400+
return false
401+
}
402+
if (file.size > this.fileSizeLimit) {
403+
this.$message({
404+
message: `文件大小不能超过${this.fileSizeLimit/1024/1024}MB`,
405+
type: 'error'
406+
});
407+
return false
408+
}
409+
},
388410
toUploadData: function () {
389411
if (this.uploadData.text === '' && this.uploadData.file === null) {
390412
this.$message({
391413
message: '请先粘贴文字或者上传文件',
392414
type: 'error'
393415
});
394416
} else {
395-
this.http('post', '/share', this.uploadData, {
396-
headers: {
397-
'Content-Type': 'multipart/form-data',
398-
'pwd': this.pwd
399-
}
400-
}).then(res => {
401-
this.jiFiles.unshift(res.data);
402-
this.jiDrawer = true;
403-
this.uploadData.text = '';
404-
this.uploadData.file = null;
405-
});
417+
if (this.checkFile(this.uploadData.file)) {
418+
this.http('post', '/share', this.uploadData, {
419+
headers: {
420+
'Content-Type': 'multipart/form-data',
421+
'pwd': this.pwd
422+
}
423+
}).then(res => {
424+
this.jiFiles.unshift(res.data);
425+
this.jiDrawer = true;
426+
this.uploadData.text = '';
427+
this.uploadData.file = null;
428+
});
429+
}
406430
}
407431
},
408432
successUpload(response, file, fileList) {
@@ -420,13 +444,7 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
420444
});
421445
},
422446
beforeUpload: function (file) {
423-
if (file.size > this.fileSizeLimit) {
424-
this.$message({
425-
message: `文件大小不能超过${this.fileSizeLimit/1024/1024}MB`,
426-
type: 'error'
427-
});
428-
return false
429-
}
447+
return this.checkFile(file);
430448
}
431449
}
432450
})

0 commit comments

Comments
 (0)