Skip to content

Commit d45d313

Browse files
committed
优化进度反馈
1 parent fb82f20 commit d45d313

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

sdk/advance.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,16 @@ function sliceUploadFile(params, callback) {
139139
}
140140
params.ChunkSize = params.SliceSize = ChunkSize = Math.max(ChunkSize, AutoChunkSize);
141141
})();
142-
onProgress = util.throttleOnProgress.call(self, FileSize, params.onProgress);
143142

144143
// 开始上传
145144
if (FileSize === 0) {
146145
params.Body = '';
146+
params.ContentLength = 0;
147147
params._OnlyUploadNotAddTask = true;
148148
self.putObject(params, function (err, data) {
149149
if (err) {
150-
onProgress(null, true);
151150
return callback(err);
152151
}
153-
onProgress({loaded: FileSize, total: FileSize}, true);
154152
callback(null, data);
155153
});
156154
} else {

sdk/base.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,10 +1073,12 @@ function getObject(params, callback) {
10731073
return callback(err);
10741074
}
10751075
var result = {};
1076-
if (BodyType === 'buffer') {
1077-
result.Body = Buffer.from(data.body);
1078-
} else if (BodyType === 'string') {
1079-
result.Body = data.body;
1076+
if (data.body) {
1077+
if (BodyType === 'buffer') {
1078+
result.Body = Buffer.from(data.body);
1079+
} else if (BodyType === 'string') {
1080+
result.Body = data.body;
1081+
}
10801082
}
10811083
if (data.headers && data.headers.etag) {
10821084
result.ETag = data.headers && data.headers.etag;

sdk/task.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var initTask = function (cos) {
1717
originApiMap[api] = cos[api];
1818
cos[api] = function (params, callback) {
1919
if (params._OnlyUploadNotAddTask) {
20-
originApiMap[api](params, callback);
20+
originApiMap[api].call(cos, params, callback);
2121
} else {
2222
cos._addTask(api, params, callback);
2323
}

sdk/util.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ var throttleOnProgress = function (total, onProgress) {
409409
var time0 = Date.now();
410410
var time1;
411411
var timer;
412+
var preInfo = {};
412413
function update() {
413414
timer = 0;
414415
if (onProgress && (typeof onProgress === 'function')) {
@@ -423,7 +424,14 @@ var throttleOnProgress = function (total, onProgress) {
423424
time0 = time1;
424425
size0 = size1;
425426
try {
426-
onProgress({loaded: size1, total: total, speed: speed, percent: percent});
427+
var curInfo = {loaded: size1, total: total, speed: speed, percent: percent};
428+
if (curInfo.loaded !== preInfo.loaded ||
429+
curInfo.total !== preInfo.total ||
430+
curInfo.speed !== preInfo.speed ||
431+
curInfo.percent !== preInfo.percent) {
432+
onProgress(curInfo);
433+
}
434+
preInfo = curInfo;
427435
} catch (e) {
428436
}
429437
}

0 commit comments

Comments
 (0)