Skip to content

Commit

Permalink
支持自定义域名
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jul 20, 2017
1 parent 2686021 commit cacaa43
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 39 deletions.
17 changes: 17 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "cos-js-sdk-v5",
"description": "cos js sdk v5",
"main": "dist/cos-js-sdk-v5.min.js",
"authors": [
"carsonxu"
],
"license": "ISC",
"homepage": "https://github.com/tencentyun/cos-js-sdk-v5",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
13 changes: 8 additions & 5 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ var getAuthorization = function (options, callback) {
};

var cos = new COS({
// 必选参数
AppId: config.AppId,
ProgressInterval: 1000,
FileParallelLimit: 3,
ChunkParallelLimit: 3,
ChunkSize: 1024 * 1024,
getAuthorization: getAuthorization
getAuthorization: getAuthorization,
// 可选参数
FileParallelLimit: 3, // 控制文件上传并发数
ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
ProgressInterval: 1000, // 控制 onProgress 回调的间隔
Domain: '{{Bucket}}-{{AppId}}.{{Region}}.myqcloud.com', // 自定义域名
});
var TaskId;

Expand Down
57 changes: 41 additions & 16 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -7882,10 +7882,12 @@ var defaultOptions = {
AppId: '',
SecretId: '',
SecretKey: '',
ProgressInterval: 1000,
FileParallelLimit: 3,
ChunkParallelLimit: 3,
ChunkSize: 1024 * 1024
ChunkSize: 1024 * 1024,
ProgressInterval: 1000,
Domain: '',
ServiceDomain: ''
};

// 对外暴露的类
Expand Down Expand Up @@ -15396,11 +15398,23 @@ function getService(params, callback) {
callback = params;
params = {};
}

var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';
var domain = this.options.ServiceDomain;
var appId = params.AppId || this.options.appId;
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId || '').replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
url: protocol + '//service.cos.myqcloud.com',
url: domain,
method: 'GET'
}, function (err, data) {
if (err) {
Expand Down Expand Up @@ -15525,6 +15539,7 @@ function getBucket(params, callback) {
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {
var self = this;
var headers = {};
headers['x-cos-acl'] = params['ACL'];
headers['x-cos-grant-read'] = params['GrantRead'];
Expand All @@ -15542,6 +15557,7 @@ function putBucket(params, callback) {
return callback(err);
}
var url = getUrl({
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
appId: appId
Expand Down Expand Up @@ -16307,7 +16323,7 @@ function _putObject(params, callback) {
update();
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
}();
Expand Down Expand Up @@ -16736,7 +16752,6 @@ function multipartUpload(params, callback) {
var self = this;
var TaskId = params.TaskId;
var headers = {};
console.log(TaskId, params);

headers['Content-Length'] = params['ContentLength'];
headers['Expect'] = params['Expect'];
Expand All @@ -16758,7 +16773,7 @@ function multipartUpload(params, callback) {
body: params.Body || null,
onProgress: params.onProgress
}, function (err, data) {
// if (TaskId) self.off('inner-kill-task', killTask);
if (TaskId) self.off('inner-kill-task', killTask);
if (err) {
return callback(err);
}
Expand All @@ -16771,16 +16786,14 @@ function multipartUpload(params, callback) {
});

var killTask = function (data) {
console.log(count, TaskId, data.TaskId);
if (data.TaskId === TaskId) {
sender && sender.abort && sender.abort();
self.off('inner-kill-task', killTask);
}
};
killTask.id = count++;
TaskId && this.on('inner-kill-task', killTask);
}
var count = 0;

/**
* 完成分块上传
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -17008,14 +17021,25 @@ function getAuth(params) {

// 生成操作 url
function getUrl(params) {
var domain = params.domain;
var bucket = params.bucket;
var region = params.region;
var object = params.object;
var action = params.action;
var appId = params.appId;
var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';

var url = protocol + '//' + bucket + '-' + appId + '.' + region + '.myqcloud.com';
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId).replace(/\{\{Bucket\}\}/ig, bucket).replace(/\{\{Region\}\}/ig, region).replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//' + bucket + '-' + appId + '.' + region + '.myqcloud.com';
}
var url = domain;

if (object) {
url += '/' + encodeURIComponent(object);
Expand Down Expand Up @@ -17043,14 +17067,15 @@ function submitRequest(params, callback) {

var opt = {
url: url || getUrl({
domain: this.options.Domain,
bucket: bucket,
region: region,
object: object,
action: action,
appId: params.AppId || this.options.AppId
}),
method: method,
headers: headers,
headers: headers || {},
qs: qs,
body: body,
json: json
Expand Down Expand Up @@ -21911,7 +21936,7 @@ function getUploadIdAndPartList(params, callback) {
}
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
var getChunkETag = function (PartNumber, callback) {
Expand Down Expand Up @@ -22213,7 +22238,7 @@ function uploadSliceList(params, cb) {
update();
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
}();
Expand Down Expand Up @@ -29231,7 +29256,7 @@ function plural(ms, n, name) {

module.exports = {
"name": "cos-js-sdk-v5",
"version": "0.0.5",
"version": "0.0.6",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions 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.0.5",
"version": "0.0.6",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function getUploadIdAndPartList(params, callback) {
}
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
var getChunkETag = function (PartNumber, callback) {
Expand Down Expand Up @@ -466,7 +466,7 @@ function uploadSliceList(params, cb) {
update();
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
})();
Expand Down
48 changes: 37 additions & 11 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ function getService(params, callback) {
callback = params;
params = {};
}

var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';
var domain = this.options.ServiceDomain;
var appId = params.AppId || this.options.appId;
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId || '').replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//service.cos.myqcloud.com';
}

submitRequest.call(this, {
url: protocol + '//service.cos.myqcloud.com',
url: domain,
method: 'GET',
}, function (err, data) {
if (err) {
Expand Down Expand Up @@ -146,6 +158,7 @@ function getBucket(params, callback) {
* @return {String} data.Location 操作地址
*/
function putBucket(params, callback) {
var self = this;
var headers = {};
headers['x-cos-acl'] = params['ACL'];
headers['x-cos-grant-read'] = params['GrantRead'];
Expand All @@ -163,6 +176,7 @@ function putBucket(params, callback) {
return callback(err);
}
var url = getUrl({
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
appId: appId
Expand Down Expand Up @@ -930,7 +944,7 @@ function _putObject(params, callback) {
update();
} else {
if (progressTimer) return;
progressTimer = setTimeout(update, self.options.ProgressInterval || 100);
progressTimer = setTimeout(update, self.options.ProgressInterval || 1000);
}
};
})();
Expand Down Expand Up @@ -1362,7 +1376,6 @@ function multipartUpload(params, callback) {
var self = this;
var TaskId = params.TaskId;
var headers = {};
console.log(TaskId, params);

headers['Content-Length'] = params['ContentLength'];
headers['Expect'] = params['Expect'];
Expand All @@ -1384,7 +1397,7 @@ function multipartUpload(params, callback) {
body: params.Body || null,
onProgress: params.onProgress
}, function (err, data) {
// if (TaskId) self.off('inner-kill-task', killTask);
if (TaskId) self.off('inner-kill-task', killTask);
if (err) {
return callback(err);
}
Expand All @@ -1397,17 +1410,15 @@ function multipartUpload(params, callback) {
});

var killTask = function (data) {
console.log(count, TaskId, data.TaskId);
if (data.TaskId === TaskId) {
sender && sender.abort && sender.abort();
self.off('inner-kill-task', killTask);
}
};
killTask.id = count++;
TaskId && this.on('inner-kill-task', killTask);

}
var count = 0;

/**
* 完成分块上传
* @param {Object} params 参数对象,必须
Expand Down Expand Up @@ -1637,14 +1648,28 @@ function getAuth(params) {

// 生成操作 url
function getUrl(params) {
var domain = params.domain;
var bucket = params.bucket;
var region = params.region;
var object = params.object;
var action = params.action;
var appId = params.appId;
var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';

var url = protocol + '//' + bucket + '-' + appId + '.' + region + '.myqcloud.com';
if (domain) {
domain = domain.replace(/\{\{AppId\}\}/ig, appId)
.replace(/\{\{Bucket\}\}/ig, bucket)
.replace(/\{\{Region\}\}/ig, region)
.replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
domain = protocol + '//' + domain;
}
if (domain.slice(-1) === '/') {
domain = domain.slice(0, -1);
}
} else {
domain = protocol + '//' + bucket + '-' + appId + '.' + region + '.myqcloud.com';
}
var url = domain;

if (object) {
url += '/' + encodeURIComponent(object);
Expand Down Expand Up @@ -1672,14 +1697,15 @@ function submitRequest(params, callback) {

var opt = {
url: url || getUrl({
domain: this.options.Domain,
bucket: bucket,
region: region,
object: object,
action: action,
appId: params.AppId || this.options.AppId,
}),
method: method,
headers: headers,
headers: headers || {},
qs: qs,
body: body,
json: json,
Expand Down
4 changes: 3 additions & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ var defaultOptions = {
AppId: '',
SecretId: '',
SecretKey: '',
ProgressInterval: 1000,
FileParallelLimit: 3,
ChunkParallelLimit: 3,
ChunkSize: 1024 * 1024,
ProgressInterval: 1000,
Domain: '',
ServiceDomain: '',
};

// 对外暴露的类
Expand Down

0 comments on commit cacaa43

Please sign in to comment.