|
|
@@ -31,7 +31,7 @@ |
|
|
|
options: { |
|
|
|
target: 'http://localhost:9000/upload', |
|
|
|
testChunks: false, |
|
|
|
chunkSize: 1024*1024*128, //128MB |
|
|
|
chunkSize: 1024*1024*64, //64MB |
|
|
|
simultaneousUploads: 1, //并发上传数 |
|
|
|
headers: { |
|
|
|
'access-token': 'abcd1234' |
|
|
@@ -79,11 +79,11 @@ |
|
|
|
md5: file.uniqueIdentifier, |
|
|
|
_csrf: csrf |
|
|
|
}}).then(function (response) { |
|
|
|
console.log(response.data.uploadID, response.data.uuid, response.data.uploaded, response.data.chunks); |
|
|
|
file.uploadID = response.data.uploadID; |
|
|
|
file.uuid = response.data.uuid; |
|
|
|
file.uploaded = response.data.uploaded; |
|
|
|
file.chunks = response.data.chunks; |
|
|
|
console.log(file.chunks); |
|
|
|
resolve(response); |
|
|
|
}).catch(function (error) { |
|
|
|
console.log(error); |
|
|
@@ -101,7 +101,6 @@ |
|
|
|
fileType: file.fileType, |
|
|
|
_csrf: csrf |
|
|
|
}}).then(function (response) { |
|
|
|
console.log(response.data.uploadID, response.data.uuid); |
|
|
|
file.uploadID = response.data.uploadID; |
|
|
|
file.uuid = response.data.uuid; |
|
|
|
resolve(response); |
|
|
@@ -113,7 +112,7 @@ |
|
|
|
}, |
|
|
|
multipartUpload(file) { |
|
|
|
let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, |
|
|
|
chunkSize = 1024*1024*128, |
|
|
|
chunkSize = 1024*1024*64, |
|
|
|
chunks = Math.ceil(file.size / chunkSize), |
|
|
|
currentChunk = 0, |
|
|
|
fileReader = new FileReader(), |
|
|
@@ -135,54 +134,124 @@ |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
function uploadChunk(e) { |
|
|
|
//判断是否已上传 |
|
|
|
if (!checkSuccessChunks()) { |
|
|
|
let start = currentChunk * chunkSize; |
|
|
|
let partSize = ((start + chunkSize) >= file.size) ? file.size -start : chunkSize; |
|
|
|
//获取分片url |
|
|
|
axios.get('/attachments/get_multipart_url', {params :{ |
|
|
|
uuid: file.uuid, |
|
|
|
uploadID: file.uploadID, |
|
|
|
size: partSize, |
|
|
|
chunkNumber: currentChunk+1, |
|
|
|
_csrf: csrf |
|
|
|
}}).then(function (response) { |
|
|
|
|
|
|
|
//todo:分片上传 |
|
|
|
axios.put(response.data.url, e.target.result |
|
|
|
function getUploadChunkUrl(currentChunk, partSize) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
axios.get('/attachments/get_multipart_url', {params :{ |
|
|
|
uuid: file.uuid, |
|
|
|
uploadID: file.uploadID, |
|
|
|
size: partSize, |
|
|
|
chunkNumber: currentChunk+1, |
|
|
|
_csrf: csrf |
|
|
|
}}).then(function (response) { |
|
|
|
urls[currentChunk] = response.data.url |
|
|
|
resolve(response); |
|
|
|
}).catch(function (error) { |
|
|
|
console.log(error); |
|
|
|
reject(error); |
|
|
|
}); |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function uploadMinio(url, e) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
axios.put(url, e.target.result |
|
|
|
).then(function (res) { |
|
|
|
console.log(res.headers.etag); |
|
|
|
etags[currentChunk] = res.headers.etag; |
|
|
|
resolve(res); |
|
|
|
}).catch(function (err) { |
|
|
|
console.log(err); |
|
|
|
reject(err); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function updateChunk(currentChunk) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
axios.post('/attachments/update_chunk', qs.stringify({ |
|
|
|
uuid: file.uuid, |
|
|
|
chunkNumber: currentChunk+1, |
|
|
|
etag: etags[currentChunk], |
|
|
|
_csrf: csrf |
|
|
|
})).then(function (response) { |
|
|
|
resolve(response); |
|
|
|
}).catch(function (error) { |
|
|
|
console.log(error); |
|
|
|
reject(error); |
|
|
|
}); |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
async function uploadChunk(e) { |
|
|
|
if (!checkSuccessChunks()) { |
|
|
|
let start = currentChunk * chunkSize; |
|
|
|
let partSize = ((start + chunkSize) >= file.size) ? file.size -start : chunkSize; |
|
|
|
|
|
|
|
}).catch(function (error) { |
|
|
|
console.log(error); |
|
|
|
}); |
|
|
|
//获取分片上传url |
|
|
|
await getUploadChunkUrl(currentChunk, partSize); |
|
|
|
if (urls[currentChunk] != "") { |
|
|
|
//上传到minio |
|
|
|
await uploadMinio(urls[currentChunk], e); |
|
|
|
if (etags[currentChunk] != "") { |
|
|
|
//更新数据库:分片上传结果 |
|
|
|
console.log(etags[currentChunk]); |
|
|
|
await updateChunk(currentChunk); |
|
|
|
} else { |
|
|
|
return; |
|
|
|
} |
|
|
|
} else { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
function completeUpload(){ |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
axios.post('/attachments/complete_multipart', qs.stringify({ |
|
|
|
uuid: file.uuid, |
|
|
|
uploadID: file.uploadID, |
|
|
|
etag: etags[currentChunk], |
|
|
|
_csrf: csrf |
|
|
|
})).then(function (response) { |
|
|
|
resolve(response); |
|
|
|
}).catch(function (error) { |
|
|
|
console.log(error); |
|
|
|
reject(error); |
|
|
|
}); |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
function upload() { |
|
|
|
loadNext(); |
|
|
|
fileReader.onload = async (e) => { |
|
|
|
await uploadChunk(e); |
|
|
|
currentChunk++; |
|
|
|
|
|
|
|
if (currentChunk < chunks) { |
|
|
|
console.log(`第${currentChunk}个分片上传完成, 开始第${currentChunk +1}/${chunks}个分片上传`); |
|
|
|
await loadNext(); |
|
|
|
} else { |
|
|
|
completeUpload(); |
|
|
|
console.log(`文件上传完成:${file.name} \n分片:${chunks} 大小:${file.size} 用时:${(new Date().getTime() - time)/1000} s`); |
|
|
|
} |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
var successChunks = new Array(); |
|
|
|
successChunks = file.chunks.split(","); |
|
|
|
var successParts = new Array(); |
|
|
|
successParts = file.chunks.split(","); |
|
|
|
for (let i = 0; i < successParts.length; i++) { |
|
|
|
successChunks[i] = successParts[i].split("-")[0].split("\"")[1]; |
|
|
|
} |
|
|
|
|
|
|
|
var urls = new Array(); |
|
|
|
var etags = new Array(); |
|
|
|
|
|
|
|
console.log('上传分片...'); |
|
|
|
loadNext(); |
|
|
|
fileReader.onload = (e) => { |
|
|
|
uploadChunk(e); |
|
|
|
currentChunk++; |
|
|
|
|
|
|
|
if (currentChunk < chunks && !checkSuccessChunks()) { |
|
|
|
console.log(`第${currentChunk}个分片上传完成, 开始第${currentChunk +1} / ${chunks}个分片上传`); |
|
|
|
loadNext(); |
|
|
|
} else { |
|
|
|
console.log(`文件上传完成:${file.name} \n分片:${chunks} 大小:${file.size} 用时:${(new Date().getTime() - time)/1000} s`); |
|
|
|
|
|
|
|
|
|
|
|
//this.computeMD5Success(file); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
upload(); |
|
|
|
|
|
|
|
}, |
|
|
|
chkMd5(file) { |
|
|
@@ -202,7 +271,7 @@ |
|
|
|
//计算MD5 |
|
|
|
computeMD5(file) { |
|
|
|
let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice, |
|
|
|
chunkSize = 1024*1024*128, |
|
|
|
chunkSize = 1024*1024*64, |
|
|
|
chunks = Math.ceil(file.size / chunkSize), |
|
|
|
currentChunk = 0, |
|
|
|
spark = new SparkMD5.ArrayBuffer(), |
|
|
@@ -220,7 +289,7 @@ |
|
|
|
currentChunk++; |
|
|
|
|
|
|
|
if (currentChunk < chunks) { |
|
|
|
console.log(`第${currentChunk}分片解析完成, 开始第${currentChunk +1} / ${chunks}分片解析`); |
|
|
|
console.log(`第${currentChunk}分片解析完成, 开始第${currentChunk +1}/${chunks}分片解析`); |
|
|
|
// let percent = Math.floor(currentChunk / chunks * 100); |
|
|
|
// console.log(percent); |
|
|
|
// file.cmd5progress = percent; |
|
|
@@ -252,7 +321,6 @@ |
|
|
|
await this.getSuccessChunks(file); |
|
|
|
|
|
|
|
if (file.uploadID == "" || file.uuid == "") { //未上传过 |
|
|
|
console.log("test1"); |
|
|
|
await this.newMultiUpload(file); |
|
|
|
if (file.uploadID != "" && file.uuid != "") { |
|
|
|
file.chunks = ""; |
|
|
@@ -262,11 +330,16 @@ |
|
|
|
return; |
|
|
|
} |
|
|
|
} else { |
|
|
|
console.log("test2"); |
|
|
|
if (file.uploaded == "1") { //已上传成功 |
|
|
|
//todo:结束上传 |
|
|
|
} else { |
|
|
|
//todo:查询已上传成功的分片,重新上传未成功上传的分片 |
|
|
|
var successChunks = new Array(); |
|
|
|
var successParts = new Array(); |
|
|
|
successParts = file.chunks.split(","); |
|
|
|
for (let i = 0; i < successParts.length; i++) { |
|
|
|
successChunks[i] = successParts[i].split("-")[0].split("\"")[1]; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|