Skip to content

Commit 6e52553

Browse files
committed
update:异步上传
1 parent d5bc3e1 commit 6e52553

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ async def upload_file(file_key: str, file: bytes = File(...), chunk_index: int =
193193
return {'code': 200}
194194

195195

196-
@app.get('/file/merge/{file_key}')
196+
@app.get('/file/merge/{file_key}/')
197197
async def merge_chunks(file_key: str, file_name: str, total_chunks: int):
198198
return {'code': 200, 'data': await storage.merge_chunks(file_key, file_name, total_chunks)}
199199

settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Settings:
1818
# Sqlite套接字
1919
DATABASE_URL = config('DATABASE_URL', cast=str, default=f"sqlite+aiosqlite:///{DATABASE_FILE}")
2020
# 数据存储文件夹,文件就不暴露在静态资源里面了
21-
DATA_ROOT = './data/' + config('DATA_ROOT', cast=str, default=f"static")
21+
DATA_ROOT = config('DATA_ROOT', cast=str, default=f"./data/static")
2222
# 静态文件夹URL
2323
STATIC_URL = config('STATIC_URL', cast=str, default="/static")
2424
# 开启上传
@@ -42,7 +42,7 @@ class Settings:
4242
# 管理密码
4343
ADMIN_PASSWORD = config('ADMIN_PASSWORD', cast=str, default=uuid.uuid4().hex)
4444
# 文件大小限制,默认10MB
45-
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10) * 1024 * 1024
45+
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10 * 1024 * 1024)
4646
# 网站标题
4747
TITLE = config('TITLE', cast=str, default="文件快递柜")
4848
# 网站描述

templates/index.html

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<meta name="description" content="{{description}}"/>
1212
<meta name="keywords" content="{{keywords}}"/>
1313
<meta name="generator" content="FileCodeBox"/>
14-
<meta name="template" content="V1.6 Beta"/>
14+
<meta name="template" content="V1.7 Beta"/>
1515
<style>
1616
body {
1717
background: #f5f5f5;
@@ -288,6 +288,7 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
288288
},
289289
quFiles: [],
290290
jiFiles: [],
291+
uploadGroup: [],
291292
pageNum: 0,
292293
inputDisable: false,
293294
fileSizeLimit: '{{fileSizeLimit}}',
@@ -386,7 +387,11 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
386387
formData.append('file_key', file_key);
387388
formData.append('chunk_index', chunk_index);
388389
formData.append('total_chunks', total_chunks);
389-
await this.http('post', `/file/upload/${file_key}/`, formData)
390+
this.http('post', `/file/upload/${file_key}/`, formData).then((res) => {
391+
this.upload_groups[chunk_index - 1] = 1;
392+
}).catch((res) => {
393+
this.upload_groups[chunk_index - 1] = 2;
394+
})
390395
},
391396
copyText: function (value, style = 1) {
392397
if (style === 0) {
@@ -475,36 +480,56 @@ <h1 @click="copyText(file.code,0)" style="margin: 0;display: inline;cursor: poin
475480
const shardSize = 1024 * 1024 * 5;
476481
const {name, size, type} = file;
477482
const total_chunks = Math.ceil(size / shardSize);
483+
this.upload_groups = [];
484+
for (let i = 0; i < total_chunks; i++) {
485+
this.upload_groups.push(0);
486+
}
478487
while (chunk_index < total_chunks) {
479488
const start = chunk_index * shardSize
480489
const end = Math.min(start + shardSize, size)
481490
chunk_index += 1;
482-
await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
491+
this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
483492
}
484-
this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
485-
this.http('post', '/share/file/', {
486-
text: text.data,
487-
size: size,
488-
exp_style: this.uploadData.exp_style,
489-
exp_value: this.uploadData.exp_value,
490-
type: type,
491-
name: name,
492-
key: res.data,
493-
}, {
494-
headers: {
495-
'pwd': this.pwd
493+
const interval = setInterval(() => {
494+
let flag = true;
495+
for (let i = 0; i < total_chunks; i++) {
496+
if (this.upload_groups[i] === 0) {
497+
flag = false;
498+
} else if (this.upload_groups[i] === 2) {
499+
flag = false;
500+
start = chunk_index * shardSize;
501+
const end = Math.min(start + shardSize, size)
502+
this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
496503
}
497-
}).then(res => {
498-
this.jiFiles.unshift(res.data);
499-
this.jiDrawer = true;
500-
this.uploadData.text = '';
501-
this.uploadData.file = null;
502-
this.$message({
503-
message: '上传成功',
504-
type: 'success'
505-
});
506-
})
507-
})
504+
}
505+
if (flag) {
506+
clearInterval(interval);
507+
this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
508+
this.http('post', '/share/file/', {
509+
text: text.data,
510+
size: size,
511+
exp_style: this.uploadData.exp_style,
512+
exp_value: this.uploadData.exp_value,
513+
type: type,
514+
name: name,
515+
key: res.data,
516+
}, {
517+
headers: {
518+
'pwd': this.pwd
519+
}
520+
}).then(res => {
521+
this.jiFiles.unshift(res.data);
522+
this.jiDrawer = true;
523+
this.uploadData.text = '';
524+
this.uploadData.file = null;
525+
this.$message({
526+
message: '上传成功',
527+
type: 'success'
528+
});
529+
})
530+
})
531+
}
532+
}, 100);
508533
});
509534
}
510535
},

0 commit comments

Comments
 (0)