Skip to content

Commit

Permalink
微信 PC webview 计算分片 md5 不复制分片 blob
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Dec 26, 2018
1 parent 1c58e74 commit 170da58
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
11 changes: 6 additions & 5 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ var fileSliceNeedCopy = function () {
};
return check(navigator.userAgent);
}();
util.fileSlice = function (file, start, end, callback) {
util.fileSlice = function (file, start, end, isUseToUpload, callback) {
var blob;
if (file.slice) {
blob = file.slice(start, end);
Expand All @@ -579,9 +579,10 @@ util.fileSlice = function (file, start, end, callback) {
} else if (file.webkitSlice) {
blob = file.webkitSlice(start, end);
}
if (fileSliceNeedCopy) {
if (isUseToUpload && fileSliceNeedCopy) {
var reader = new FileReader();
reader.onload = function (e) {
blob = null;
callback(new Blob([reader.result]));
};
reader.readAsArrayBuffer(blob);
Expand Down Expand Up @@ -1978,7 +1979,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.4';
COS.version = '0.5.5';

module.exports = COS;

Expand Down Expand Up @@ -10878,7 +10879,7 @@ function getUploadIdAndPartList(params, callback) {
Size: ChunkSize
});
} else {
util.fileSlice(params.Body, start, end, function (chunkItem) {
util.fileSlice(params.Body, start, end, false, function (chunkItem) {
util.getFileMd5(chunkItem, function (err, md5) {
if (err) return callback(err);
var ETag = '"' + md5 + '"';
Expand Down Expand Up @@ -11282,7 +11283,7 @@ function uploadSliceItem(params, callback) {
var PartItem = UploadData.PartList[PartNumber - 1];
Async.retry(ChunkRetryTimes, function (tryCallback) {
if (!self._isRunningTask(TaskId)) return;
util.fileSlice(FileBody, start, end, function (Body) {
util.fileSlice(FileBody, start, end, true, function (Body) {
self.multipartUpload({
TaskId: TaskId,
Bucket: Bucket,
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "0.5.4",
"version": "0.5.5",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/document/product/436)",
"main": "index.js",
"scripts": {
Expand Down
56 changes: 28 additions & 28 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function getUploadIdAndPartList(params, callback) {
Size: ChunkSize
});
} else {
util.fileSlice(params.Body, start, end, function (chunkItem) {
util.fileSlice(params.Body, start, end, false, function (chunkItem) {
util.getFileMd5(chunkItem, function (err, md5) {
if (err) return callback(err);
var ETag = '"' + md5 + '"';
Expand Down Expand Up @@ -652,35 +652,35 @@ function uploadSliceItem(params, callback) {
ContentLength = end - start;
}

var PartItem = UploadData.PartList[PartNumber - 1];
Async.retry(ChunkRetryTimes, function (tryCallback) {
if (!self._isRunningTask(TaskId)) return;
util.fileSlice(FileBody, start, end, function (Body) {
self.multipartUpload({
TaskId: TaskId,
Bucket: Bucket,
Region: Region,
Key: Key,
ContentLength: ContentLength,
PartNumber: PartNumber,
UploadId: UploadData.UploadId,
ServerSideEncryption: ServerSideEncryption,
Body: Body,
onProgress: params.onProgress,
}, function (err, data) {
if (!self._isRunningTask(TaskId)) return;
if (err) {
return tryCallback(err);
} else {
PartItem.Uploaded = true;
return tryCallback(null, data);
}
});
var PartItem = UploadData.PartList[PartNumber - 1];
Async.retry(ChunkRetryTimes, function (tryCallback) {
if (!self._isRunningTask(TaskId)) return;
util.fileSlice(FileBody, start, end, true, function (Body) {
self.multipartUpload({
TaskId: TaskId,
Bucket: Bucket,
Region: Region,
Key: Key,
ContentLength: ContentLength,
PartNumber: PartNumber,
UploadId: UploadData.UploadId,
ServerSideEncryption: ServerSideEncryption,
Body: Body,
onProgress: params.onProgress,
}, function (err, data) {
if (!self._isRunningTask(TaskId)) return;
if (err) {
return tryCallback(err);
} else {
PartItem.Uploaded = true;
return tryCallback(null, data);
}
});
}, function (err, data) {
if (!self._isRunningTask(TaskId)) return;
return callback(err, data);
});
}, function (err, data) {
if (!self._isRunningTask(TaskId)) return;
return callback(err, data);
});
}


Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.4';
COS.version = '0.5.5';

module.exports = COS;
5 changes: 3 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ var fileSliceNeedCopy = (function () {
};
return check(navigator.userAgent);
})();
util.fileSlice = function (file, start, end, callback) {
util.fileSlice = function (file, start, end, isUseToUpload, callback) {
var blob;
if (file.slice) {
blob = file.slice(start, end);
Expand All @@ -514,9 +514,10 @@ util.fileSlice = function (file, start, end, callback) {
} else if (file.webkitSlice) {
blob = file.webkitSlice(start, end);
}
if (fileSliceNeedCopy) {
if (isUseToUpload && fileSliceNeedCopy) {
var reader = new FileReader();
reader.onload = function (e) {
blob = null;
callback(new Blob([reader.result]));
};
reader.readAsArrayBuffer(blob);
Expand Down

0 comments on commit 170da58

Please sign in to comment.