update:异步上传
This commit is contained in:
@@ -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
@@ -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="文件快递柜")
|
||||||
# 网站描述
|
# 网站描述
|
||||||
|
|||||||
+28
-3
@@ -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,12 +480,30 @@
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
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('get', `/file/merge/${res.data}/?file_name=${name}&total_chunks=${total_chunks}`).then(text => {
|
||||||
this.http('post', '/share/file/', {
|
this.http('post', '/share/file/', {
|
||||||
text: text.data,
|
text: text.data,
|
||||||
@@ -505,6 +528,8 @@
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user