update:异步上传

This commit is contained in:
lan
2023-03-01 16:22:40 +08:00
parent cff5fd3674
commit d27a567411
3 changed files with 54 additions and 29 deletions
+1 -1
View File
@@ -193,7 +193,7 @@ async def upload_file(file_key: str, file: bytes = File(...), chunk_index: int =
return {'code': 200}
@app.get('/file/merge/{file_key}')
@app.get('/file/merge/{file_key}/')
async def merge_chunks(file_key: str, file_name: str, total_chunks: int):
return {'code': 200, 'data': await storage.merge_chunks(file_key, file_name, total_chunks)}
+2 -2
View File
@@ -18,7 +18,7 @@ class Settings:
# Sqlite套接字
DATABASE_URL = config('DATABASE_URL', cast=str, default=f"sqlite+aiosqlite:///{DATABASE_FILE}")
# 数据存储文件夹,文件就不暴露在静态资源里面了
DATA_ROOT = './data/' + config('DATA_ROOT', cast=str, default=f"static")
DATA_ROOT = config('DATA_ROOT', cast=str, default=f"./data/static")
# 静态文件夹URL
STATIC_URL = config('STATIC_URL', cast=str, default="/static")
# 开启上传
@@ -42,7 +42,7 @@ class Settings:
# 管理密码
ADMIN_PASSWORD = config('ADMIN_PASSWORD', cast=str, default=uuid.uuid4().hex)
# 文件大小限制,默认10MB
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10) * 1024 * 1024
FILE_SIZE_LIMIT = config('FILE_SIZE_LIMIT', cast=int, default=10 * 1024 * 1024)
# 网站标题
TITLE = config('TITLE', cast=str, default="文件快递柜")
# 网站描述
+28 -3
View File
@@ -11,7 +11,7 @@
<meta name="description" content="{{description}}"/>
<meta name="keywords" content="{{keywords}}"/>
<meta name="generator" content="FileCodeBox"/>
<meta name="template" content="V1.6 Beta"/>
<meta name="template" content="V1.7 Beta"/>
<style>
body {
background: #f5f5f5;
@@ -288,6 +288,7 @@
},
quFiles: [],
jiFiles: [],
uploadGroup: [],
pageNum: 0,
inputDisable: false,
fileSizeLimit: '{{fileSizeLimit}}',
@@ -386,7 +387,11 @@
formData.append('file_key', file_key);
formData.append('chunk_index', chunk_index);
formData.append('total_chunks', total_chunks);
await this.http('post', `/file/upload/${file_key}/`, formData)
this.http('post', `/file/upload/${file_key}/`, formData).then((res) => {
this.upload_groups[chunk_index - 1] = 1;
}).catch((res) => {
this.upload_groups[chunk_index - 1] = 2;
})
},
copyText: function (value, style = 1) {
if (style === 0) {
@@ -475,12 +480,30 @@
const shardSize = 1024 * 1024 * 5;
const {name, size, type} = file;
const total_chunks = Math.ceil(size / shardSize);
this.upload_groups = [];
for (let i = 0; i < total_chunks; i++) {
this.upload_groups.push(0);
}
while (chunk_index < total_chunks) {
const start = chunk_index * shardSize
const end = Math.min(start + shardSize, size)
chunk_index += 1;
await this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
}
const interval = setInterval(() => {
let flag = true;
for (let i = 0; i < total_chunks; i++) {
if (this.upload_groups[i] === 0) {
flag = false;
} else if (this.upload_groups[i] === 2) {
flag = false;
start = chunk_index * shardSize;
const end = Math.min(start + shardSize, size)
this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
}
}
if (flag) {
clearInterval(interval);
this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
this.http('post', '/share/file/', {
text: text.data,
@@ -505,6 +528,8 @@
});
})
})
}
}, 100);
});
}
},