Created
October 29, 2015 14:37
-
-
Save jechav/76a04610c8c63ac760d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fr = new FileReader(), buf, file = element.files[0]; | |
var etags = []; | |
var partSize = 6 * 1024 * 1024; // constant | |
var totalSize = file.size; | |
var numParts = Math.ceil(totalSize / partSize); | |
var key = null; | |
var uplaodId = null; | |
// 1. Init Upload | |
coverService.initUpload({fileName: file.name, fileType: 'video'}, function(res){ | |
key = res.data.Key; | |
uplaodId = res.data.UploadId; | |
uploadPart(1); | |
}); | |
// 2. send parts | |
function uploadPart(partNum){ | |
if (partNum > numParts) { | |
completeMultipartUpload(); | |
return; | |
} | |
console.log("Uploading part " + partNum); | |
var start = (partNum - 1) * partSize; | |
var end = start + partSize; | |
if (end > totalSize) | |
end = totalSize; | |
var length = end - start; | |
var curBlobPart = file.slice(start, end); | |
data = { | |
'part': curBlobPart, | |
'Key': key, | |
'UploadId': uplaodId, | |
'PartNumber': partNum | |
} | |
console.log('part ',data); | |
coverService.sendParts(data, function(result){ | |
console.log(result); | |
//get the etags array | |
etags.push({ | |
'ETag': result.data.ETag, | |
'PartNumber': result.data.PartNumber | |
}); | |
uploadPart(partNum +1); | |
}); | |
} | |
function completeMultipartUpload(){ | |
etags.sort(function(a, b){ | |
var nameA= a.PartNumber, nameB=b.PartNumber | |
if (nameA < nameB) //sort string ascending | |
return -1 | |
if (nameA > nameB) | |
return 1 | |
return 0 //default return value (no sorting) | |
}) | |
send = { | |
'Key': key, | |
'UploadId': uplaodId, | |
'Parts': etags | |
} | |
//3. Finish upload | |
console.log('ultimo', send); | |
coverService.completeUpload(send, function(out){ | |
console.log(out); | |
}) | |
} | |
//fr.onload = function(e) { | |
////buf = new Uint8Array(e.target.result); | |
////for (var i = 0, blobs = []; i < buf.length; i += 6e6) | |
////blobs.push(new Blob([buf.subarray(i, i + 6e6)], {type: file.type} )); | |
//// 1. Init Upload | |
//coverService.initUpload({fileName: file.name, fileType: 'video'}, function(res){ | |
//angular.forEach(blobs, function(ele, id){ | |
//data = { | |
//'part': file.slice(), | |
//'Key': res.data.Key, | |
//'UploadId': res.data.UploadId, | |
//'PartNumber': id+1 | |
//} | |
//// 2. send parts | |
//console.log('part ',data); | |
//coverservice.sendparts(data, function(result){ | |
//console.log(result); | |
////get the etags array | |
//etags.push({ | |
//'etag': result.data.etag, | |
//'partnumber': result.data.partnumber | |
//}); | |
//if(result.data.PartNumber == blobs.length){//if last | |
////order parts | |
//etags.sort(function(a, b){ | |
//var nameA= a.PartNumber, nameB=b.PartNumber | |
//if (nameA < nameB) //sort string ascending | |
//return -1 | |
//if (nameA > nameB) | |
//return 1 | |
//return 0 //default return value (no sorting) | |
//}) | |
//send = { | |
//'Key': res.data.Key, | |
//'UploadId': res.data.UploadId, | |
//'Parts': etags | |
//} | |
////3. Finish upload | |
//console.log('ultimo', send); | |
//coverService.completeUpload(send, function(out){ | |
//console.log(out); | |
//}) | |
//} | |
//}); | |
//}); | |
//}); | |
//}; | |
//fr.readAsArrayBuffer(file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment