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} 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): 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)} 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套接字 # Sqlite套接字
DATABASE_URL = config('DATABASE_URL', cast=str, default=f"sqlite+aiosqlite:///{DATABASE_FILE}") 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 # 静态文件夹URL
STATIC_URL = config('STATIC_URL', cast=str, default="/static") 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) ADMIN_PASSWORD = config('ADMIN_PASSWORD', cast=str, default=uuid.uuid4().hex)
# 文件大小限制,默认10MB # 文件大小限制,默认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="文件快递柜") TITLE = config('TITLE', cast=str, default="文件快递柜")
# 网站描述 # 网站描述
+51 -26
View File
@@ -11,7 +11,7 @@
<meta name="description" content="{{description}}"/> <meta name="description" content="{{description}}"/>
<meta name="keywords" content="{{keywords}}"/> <meta name="keywords" content="{{keywords}}"/>
<meta name="generator" content="FileCodeBox"/> <meta name="generator" content="FileCodeBox"/>
<meta name="template" content="V1.6 Beta"/> <meta name="template" content="V1.7 Beta"/>
<style> <style>
body { body {
background: #f5f5f5; background: #f5f5f5;
@@ -288,6 +288,7 @@
}, },
quFiles: [], quFiles: [],
jiFiles: [], jiFiles: [],
uploadGroup: [],
pageNum: 0, pageNum: 0,
inputDisable: false, inputDisable: false,
fileSizeLimit: '{{fileSizeLimit}}', fileSizeLimit: '{{fileSizeLimit}}',
@@ -386,7 +387,11 @@
formData.append('file_key', file_key); formData.append('file_key', file_key);
formData.append('chunk_index', chunk_index); formData.append('chunk_index', chunk_index);
formData.append('total_chunks', total_chunks); 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) { copyText: function (value, style = 1) {
if (style === 0) { if (style === 0) {
@@ -475,36 +480,56 @@
const shardSize = 1024 * 1024 * 5; const shardSize = 1024 * 1024 * 5;
const {name, size, type} = file; const {name, size, type} = file;
const total_chunks = Math.ceil(size / shardSize); 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) { while (chunk_index < total_chunks) {
const start = chunk_index * shardSize const start = chunk_index * shardSize
const end = Math.min(start + shardSize, size) const end = Math.min(start + shardSize, size)
chunk_index += 1; 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);
} }
this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => { const interval = setInterval(() => {
this.http('post', '/share/file/', { let flag = true;
text: text.data, for (let i = 0; i < total_chunks; i++) {
size: size, if (this.upload_groups[i] === 0) {
exp_style: this.uploadData.exp_style, flag = false;
exp_value: this.uploadData.exp_value, } else if (this.upload_groups[i] === 2) {
type: type, flag = false;
name: name, start = chunk_index * shardSize;
key: res.data, const end = Math.min(start + shardSize, size)
}, { this.uploadChunk(file.slice(start, end), res.data, chunk_index, total_chunks);
headers: {
'pwd': this.pwd
} }
}).then(res => { }
this.jiFiles.unshift(res.data); if (flag) {
this.jiDrawer = true; clearInterval(interval);
this.uploadData.text = ''; this.http('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
this.uploadData.file = null; this.http('post', '/share/file/', {
this.$message({ text: text.data,
message: '上传成功', size: size,
type: 'success' exp_style: this.uploadData.exp_style,
}); exp_value: this.uploadData.exp_value,
}) type: type,
}) name: name,
key: res.data,
}, {
headers: {
'pwd': this.pwd
}
}).then(res => {
this.jiFiles.unshift(res.data);
this.jiDrawer = true;
this.uploadData.text = '';
this.uploadData.file = null;
this.$message({
message: '上传成功',
type: 'success'
});
})
})
}
}, 100);
}); });
} }
}, },