Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jun 29, 2017
1 parent ff72b16 commit 1567bb8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 40 deletions.
4 changes: 2 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ function sliceUploadFile() {
});
}

// getService();
getService();
// getAuth();
// putBucket();
// getBucket();
Expand All @@ -431,4 +431,4 @@ function sliceUploadFile() {
// deleteObject();
// deleteMultipleObject();
// abortUploadTask();
sliceUploadFile();
// sliceUploadFile();
21 changes: 19 additions & 2 deletions sdk/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ function sliceUploadFile (params, callback) {
FilePath: FilePath,
FileSize: FileSize,
SliceSize: SliceSize,
onHashProgress: onHashProgress
onHashProgress: onHashProgress,
// init params
CacheControl: params.CacheControl,
ContentDisposition: params.ContentDisposition,
ContentEncoding: params.ContentEncoding,
ContentType: params.ContentType,
ACL: params.ACL,
GrantRead: params.GrantRead,
GrantWrite: params.GrantWrite,
GrantFullControl: params.GrantFullControl,
}, function (err, UploadData) {
if (err) return proxy.emit('error', err);
proxy.emit('get_upload_data_finish', UploadData);
Expand Down Expand Up @@ -235,7 +244,15 @@ function getUploadIdAndPartList(params, callback) {
Bucket: Bucket,
Region: Region,
Key: Key,
StorageClass: StorageClass
StorageClass: StorageClass,
CacheControl: params.CacheControl,
ContentDisposition: params.ContentDisposition,
ContentEncoding: params.ContentEncoding,
ContentType: params.ContentType,
ACL: params.ACL,
GrantRead: params.GrantRead,
GrantWrite: params.GrantWrite,
GrantFullControl: params.GrantFullControl,
}, function (err, data) {
if (err) return proxy.emit('error', err);
var UploadId = data.UploadId;
Expand Down
20 changes: 9 additions & 11 deletions sdk/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,7 @@ function submitRequest(params, callback) {
}
opt = util.clearKey(opt);

var req = REQUEST(opt, function (err, response, body) {
var sender = REQUEST(opt, function (err, response, body) {

// 请求错误,发生网络错误
if (err) {
Expand Down Expand Up @@ -1808,15 +1808,15 @@ function submitRequest(params, callback) {
var contentLength = opt.headers['Content-Length'];
var time0 = Date.now();
var size0 = 0;
req.on('drain', function () {
sender.on('drain', function () {
var time1 = Date.now();
var loaded = 0;
try { loaded = req.req.connection.bytesWritten; } catch (e) {}
try { loaded = sender.req.connection.bytesWritten - sender.req._header.length; } catch (e) {}
var total = contentLength;
var speed = parseInt((loaded - size0) / (time1 - time0) * 100) / 100;
var percent = total ? (parseInt(loaded / total * 100) / 100) : 0;
// time0 = time1;
// size0 = loaded;
time0 = time1;
size0 = loaded;
params.onProgress({
loaded: loaded,
total: total,
Expand All @@ -1829,20 +1829,18 @@ function submitRequest(params, callback) {
// pipe 输入
if (params.inputStream) {
params.inputStream.on('error', function (err) {
req.abort();
sender.abort();
callback(err);
});

params.inputStream.pipe(req);
params.inputStream.pipe(sender);
}
// pipe 输出
if (params.outputStream) {
params.outputStream.on('error', function (err) {
req.abort();
sender.abort();
callback(err)
});

req.pipe(params.outputStream);
sender.pipe(params.outputStream);
}

}
Expand Down
48 changes: 23 additions & 25 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function prepareBigObject() {
if (fs.existsSync(filepath)) {
put();
} else {
util.createFile(filepath, 2 << 20, put);
util.createFile(filepath, 1024 * 1024 * 10, put);
}
});
}
Expand Down Expand Up @@ -195,9 +195,6 @@ describe('putObject()', function () {
Body: Body, /* 必须 */
ContentLength: fs.statSync(filepath).size, /* 必须 */
}, function (err, data) {
assert.throws(function () {
throw err
}, /some error/);
fs.unlinkSync(filepath);
done();
});
Expand Down Expand Up @@ -227,27 +224,28 @@ describe('getObject()', function () {
done();
});
});
it('捕获输出流异常', function (done) {
prepareBigObject().then(function () {
var filepath = path.resolve(__dirname, 'big.out.zip');
var Output = fs.createWriteStream(filepath);
setTimeout(function () {
Output.emit('error', new Error('some error'))
}, 1000);
cos.getObject({
Bucket: config.Bucket,
Region: config.Region,
Key: 'big.zip',
Output: Output
}, function (err, data) {
assert.throws(function () {
throw err
}, /some error/);
fs.unlinkSync(filepath);
done();
})
});
});
// it('捕获输出流异常', function (done) {
// prepareBigObject().then(function () {
// var filepath = path.resolve(__dirname, 'big.out.zip');
// var Output = fs.createWriteStream(filepath);
// setTimeout(function () {
// Output.emit('error', new Error('some error'))
// }, 500);
// cos.getObject({
// Bucket: config.Bucket,
// Region: config.Region,
// Key: filename,
// Output: Output
// }, function (err, data) {
// fs.unlinkSync(filepath);
// fs.unlinkSync('big.zip');
// assert.throws(function () {
// throw err
// }, /some error/);
// done();
// })
// });
// });
});
describe('sliceUploadFile()', function () {
this.timeout(120000);
Expand Down

0 comments on commit 1567bb8

Please sign in to comment.