Skip to content

For huge file #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@
// 文件上传成功调用, 可选参数
oncomplete: function (res) {
console.log(res);
}
},
onprogress: function (progress) {
console.log("progress: ", progress);
}
});
}

Expand Down
112 changes: 34 additions & 78 deletions src/oss-js-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,70 +107,40 @@
var self = this;

var readFile = function (callback) {
var result = {
chunksHash: {},
chunks: []
};
var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;
var chunkSize = self._config.chunkSize;
var chunksNum = Math.ceil(file.size / chunkSize);
var currentChunk = 0;

var frOnload = function (e) {
result.chunks[currentChunk] = e.target.result;
currentChunk++;
if (currentChunk < chunksNum) {
loadNext();
}
else {
result.file_size = file.size;
callback(null, result);
}
var maxUploadTries = options.maxRetry || 3;
var loadedNum = 0;
var uploadId;
var multipartMap = {
Parts: []
};


var frOnerror = function () {
console.error("读取文件失败");
if (typeof options.onerror == 'function') {
options.onerror("读取文件失败");
}
};

function loadNext() {
function loadNext(currentChunk) {
if (currentChunk >= chunksNum) {
return;
}

var fileReader = new FileReader();
fileReader.onload = frOnload;
fileReader.onload = function(e) {
uploadPart(currentChunk, e.target.result);
};
fileReader.onerror = frOnerror;

var start = currentChunk * chunkSize,
end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
end = Math.min(start + chunkSize, file.size);
var blobPacket = blobSlice.call(file, start, end);
fileReader.readAsArrayBuffer(blobPacket);
}

loadNext();
};

var uploadSingle = function (result, callback) {
var params = {
Bucket: self._config.bucket,
Key: options.key,
Body: result.chunks[0],
ContentType: file.type || ''
};
_extend(params, options.headers);

self.oss.putObject(params, callback);
};

var uploadMultipart = function (result, callback) {
var maxUploadTries = options.maxRetry || 3;
var uploadId;
var loadedNum = 0;
var latestUploadNum = -1;
var concurrency = 0;

var multipartMap = {
Parts: []
};

var init = function () {
var params = {
Bucket: self._config.bucket,
Expand All @@ -189,25 +159,15 @@

// console.log("Got upload ID", res.UploadId);
uploadId = res.UploadId;

uploadPart(0);
for (var i = 0; i < self._config.concurrency; i++) {
loadNext(i);
}
});
};

var uploadPart = function (partNum) {
if(partNum >= result.chunks.length) {
return;
}

concurrency++;
if(latestUploadNum < partNum) {
latestUploadNum = partNum;
}
if(concurrency < self._config.concurrency && (partNum < (result.chunks.length - 1))) {
uploadPart(partNum + 1);
}
var uploadPart = function (partNum, chunk) {
var partParams = {
Body: result.chunks[partNum],
Body: chunk,
Bucket: self._config.bucket,
Key: options.key,
PartNumber: String(partNum + 1),
Expand All @@ -232,7 +192,6 @@
return;
}
// console.log(mData);
concurrency--;

multipartMap.Parts[partNum] = {
ETag: mData.ETag,
Expand All @@ -242,11 +201,11 @@
//console.log('mData', mData);

loadedNum++;
if (loadedNum == result.chunks.length) {
callback(null, null, loadedNum/chunksNum);
if (loadedNum == chunksNum) {
complete();
}
else {
uploadPart(latestUploadNum + 1);
} else {
loadNext(partNum + self._config.concurrency);
}
});
};
Expand All @@ -271,26 +230,23 @@
init();
};

readFile(function (err, result) {
var callback = function (err, res) {
readFile(function (err, res, progress) {
if (err) {
if (typeof options.onerror == 'function') {
options.onerror(err);
}
return;
}

if (typeof options.oncomplete == 'function') {
options.oncomplete(res);
if (res) {
if (typeof options.oncomplete == 'function') {
options.oncomplete(res);
}
return;
}
if (typeof options.onprogress == 'function') {
options.onprogress(progress);
}
};

if (result.chunks.length == 1) {
uploadSingle(result, callback)
}
else {
uploadMultipart(result, callback);
}
});

};
Expand Down