Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jul 4, 2017
1 parent b49d4aa commit 9bb221d
Show file tree
Hide file tree
Showing 10 changed files with 895 additions and 907 deletions.
390 changes: 262 additions & 128 deletions demo/demo.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
h1 { font-weight: normal; color:#333;}
a { color: #006eff; background-color: transparent; padding: 8px 16px; line-height: 1.3; display: inline-block; text-align: center; margin: 0 8px 8px 0; border: 1px solid #006eff; font-size: 14px; text-decoration: none; }
a:hover { color: #fff; background-color: #006eff; }
.result {display:none;line-height:1.3;font-size: 13px;font-family:monospace;border:1px solid #006eff;margin:0;min-height:100px;max-height:300px;overflow:auto;box-sizing:border-box;padding:5px;}
.result {display:none;line-height:1.3;font-size: 13px;font-family:monospace;border:1px solid #006eff;margin:0;height:200px;overflow:auto;box-sizing:border-box;padding:5px;}
</style>
</head>
<body>
Expand Down
9 changes: 4 additions & 5 deletions dist/cos-js-sdk-v5.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/cos-js-sdk-v5.js.map

This file was deleted.

34 changes: 17 additions & 17 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4107,6 +4107,23 @@ var request = function (options, callback) {
options.type = options.method;
delete options.method;

// headers
if (options.headers) {
var headers = options.headers;
delete options.headers;
options.beforeSend = function (xhr) {
for (var key in headers) {
if (headers.hasOwnProperty(key) &&
key.toLowerCase() !== 'content-length' &&
key.toLowerCase() !== 'user-agent' &&
key.toLowerCase() !== 'origin' &&
key.toLowerCase() !== 'host') {
xhr.setRequestHeader(key, headers[key]);
}
}
};
}

// progress
if (options.onProgress) {
options.progress = options.onProgress;
Expand Down Expand Up @@ -4137,23 +4154,6 @@ var request = function (options, callback) {
}
}

// headers
if (options.headers) {
var headers = options.headers;
delete options.headers;
options.beforeSend = function (xhr) {
for (var key in headers) {
if (headers.hasOwnProperty(key) &&
key.toLowerCase() !== 'content-length' &&
key.toLowerCase() !== 'user-agent' &&
key.toLowerCase() !== 'origin' &&
key.toLowerCase() !== 'host') {
xhr.setRequestHeader(key, headers[key]);
}
}
};
}

var getResponse = function (xhr) {
var headers = {};
xhr.getAllResponseHeaders().trim().split('\n').forEach(function (item) {
Expand Down
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.0.1",
"version": "0.1.0",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down
121 changes: 45 additions & 76 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function sliceUploadFile (params, callback) {
var FileSize;
var self = this;

StorageClass = null;

var onProgress = params.onProgress;
var onHashProgress = params.onHashProgress;

Expand Down Expand Up @@ -91,7 +93,16 @@ function sliceUploadFile (params, callback) {
Body: Body,
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 @@ -229,7 +240,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 Expand Up @@ -281,6 +300,7 @@ function getUploadIdAndPartList(params, callback) {
});

return proxy.emit('no_available_upload_id');

// 获取符合条件的 UploadId 列表,因为同一个文件可以有多个上传任务。
wholeMultipartList.call(self, {
Bucket: Bucket,
Expand Down Expand Up @@ -353,50 +373,6 @@ function wholeMultipartListPart(params, callback) {
next();
}

// 计算文件所有分片的 ETag
function getFileETagList(params, cb) {
var self = this;
var FileBody = params.Body;
var SliceSize = params.SliceSize;
var FileSize = params.FileSize;
var onHashProgress = params.onHashProgress;

var SliceCount = Math.ceil(FileSize / SliceSize);
var FinishSliceCount = 0;
var ETagList = [];

for (var i = 0; i < SliceCount; i++) {
ETagList.push({PartNumber: i + 1});
}

Async.mapLimit(ETagList, 1, function (SliceItem, callback) {
var PartNumber = SliceItem['PartNumber'];
var Index = PartNumber - 1;
var start = SliceSize * Index;
var end = Math.min(start + SliceSize, FileSize);
var ChunkSize = end - start;
var ChunkBlob = util.fileSlice.call(FileBody, start, end);
util.getFileMd5(ChunkBlob, function (err, md5) {
if (err) return callback(err);
var ETag = '"' + md5 + '"';
ETagList[Index].ETag = ETag;
++FinishSliceCount;
if (onHashProgress && (typeof onHashProgress === 'function')) {
onHashProgress({
PartNumber: PartNumber,
FileSize: FileSize,
ETag: ETag,
Size: ChunkSize
}, parseInt(FinishSliceCount / SliceCount * 100) / 100);
}
callback(null, ETag);
});
}, function (err) {
if (err) return cb(err);
cb(null, {ETagList: ETagList});
});
}

// 上传文件分块,包括
/*
UploadId (上传任务编号)
Expand Down Expand Up @@ -554,6 +530,8 @@ function uploadSliceItem(params, callback) {
});
}



// 完成分块上传
function uploadSliceComplete(params, callback) {
var Bucket = params.Bucket;
Expand Down Expand Up @@ -621,44 +599,40 @@ function abortUploadTask(params, callback) {
});
});

if (Level === 'task') {
// 单个任务级别的任务抛弃,抛弃指定 UploadId 的上传任务
if (!UploadId) {
return callback('abort_upload_task_no_id');
}
if (!Key) {
return callback('abort_upload_task_no_key');
}
ep.emit('get_abort_array', [{
Key: Key,
UploadId: UploadId
}]);
} else if (Level === 'file') {
// 文件级别的任务抛弃,抛弃该文件的全部上传任务
if (!Key) {
return callback('abort_upload_task_no_key');
}
if (Level === 'bucket') {
// Bucket 级别的任务抛弃,抛弃该 Bucket 下的全部上传任务
wholeMultipartList.call(self, {
Bucket: Bucket,
Region: Region,
Key: Key
Region: Region
}, function (err, data) {
if (err) {
return callback(err);
}
ep.emit('get_abort_array', data.UploadList || []);
});
} else {
// Bucket 级别的任务抛弃,抛弃该 Bucket 下的全部上传任务
} else if (Level === 'file') {
// 文件级别的任务抛弃,抛弃该文件的全部上传任务
if (!Key) return callback('abort_upload_task_no_key');
wholeMultipartList.call(self, {
Bucket: Bucket,
Region: Region
Region: Region,
Key: Key
}, function (err, data) {
if (err) {
return callback(err);
}
ep.emit('get_abort_array', data.UploadList || []);
});
} else if (Level === 'task') {
// 单个任务级别的任务抛弃,抛弃指定 UploadId 的上传任务
if (!UploadId) return callback('abort_upload_task_no_id');
if (!Key) return callback('abort_upload_task_no_key');
ep.emit('get_abort_array', [{
Key: Key,
UploadId: UploadId
}]);
} else {
return callback('abort_unknown_level');
}
}

Expand Down Expand Up @@ -735,11 +709,6 @@ var API_MAP = {
abortUploadTask: abortUploadTask,
};

(function () {
for (var apiName in API_MAP) {
if (API_MAP.hasOwnProperty(apiName)) {
var fn = API_MAP[apiName];
exports[apiName] = util.apiWrapper(apiName, fn);
}
}
})();
util.each(API_MAP, function (fn, apiName) {
exports[apiName] = util.apiWrapper(apiName, fn);
});
Loading

0 comments on commit 9bb221d

Please sign in to comment.