1. 计算预计上传完成的所需时间

2. 计算平均上传速度
master
huanggy 2 years ago
parent bcf685e538
commit e7f1611477

@ -51,10 +51,22 @@ const getTaskInfo = async (file) => {
*/ */
const handleUpload = (file, taskRecord, options) => { const handleUpload = (file, taskRecord, options) => {
let lastUploadedSize = 0; //
let uploadedSize = 0 // let uploadedSize = 0 //
const totalSize = file.size || 0 // const totalSize = file.size || 0 //
let startMs = new Date().getTime(); //
const { exitPartList, chunkSize, chunkNum, fileIdentifier } = taskRecord const { exitPartList, chunkSize, chunkNum, fileIdentifier } = taskRecord
// byte/s
const getSpeed = () => {
// - = byte
const intervalSize = uploadedSize - lastUploadedSize
const nowMs = new Date().getTime()
// s
const intervalTime = (nowMs - startMs) / 1000
return intervalSize / intervalTime
}
const uploadNext = async (partNumber) => { const uploadNext = async (partNumber) => {
const start = new Number(chunkSize) * (partNumber - 1) const start = new Number(chunkSize) * (partNumber - 1)
const end = start + new Number(chunkSize) const end = start + new Number(chunkSize)
@ -65,9 +77,7 @@ const handleUpload = (file, taskRecord, options) => {
url: data, url: data,
method: 'PUT', method: 'PUT',
data: blob, data: blob,
headers: { headers: {'Content-Type': 'application/octet-stream'}
'Content-Type': 'application/octet-stream'
}
}) })
return Promise.resolve({ partNumber: partNumber, uploadedSize: blob.size }) return Promise.resolve({ partNumber: partNumber, uploadedSize: blob.size })
} }
@ -90,6 +100,12 @@ const handleUpload = (file, taskRecord, options) => {
const percent = Math.round(uploadedSize / totalSize * 100).toFixed(2); const percent = Math.round(uploadedSize / totalSize * 100).toFixed(2);
onProgress({percent: percent}) onProgress({percent: percent})
} }
const speed = getSpeed();
const remainingTime = speed != 0 ? Math.ceil((totalSize - uploadedSize) / speed) + 's' : '未知'
console.log('剩余大小:', (totalSize - uploadedSize) / 1024 / 1024, 'mb');
console.log('当前速度:', (speed / 1024 / 1024).toFixed(2), 'mbps');
console.log('预计完成:', remainingTime);
} }
@ -109,7 +125,8 @@ const handleUpload = (file, taskRecord, options) => {
for (let partNumber = 1; partNumber <= chunkNum; partNumber++) { for (let partNumber = 1; partNumber <= chunkNum; partNumber++) {
const exitPart = (exitPartList || []).find(exitPart => exitPart.partNumber == partNumber) const exitPart = (exitPartList || []).find(exitPart => exitPart.partNumber == partNumber)
if (exitPart) { if (exitPart) {
// // ,
lastUploadedSize += new Number(exitPart.size)
updateProcess(exitPart.size) updateProcess(exitPart.size)
} else { } else {
queue.push(() => uploadNext(partNumber).then(res => { queue.push(() => uploadNext(partNumber).then(res => {

Loading…
Cancel
Save