Skip to content

Commit

Permalink
Merge pull request #55 from livehigh/feat/1.1.0
Browse files Browse the repository at this point in the history
Feat/1.1.0
  • Loading branch information
carsonxu authored Nov 17, 2021
2 parents 62d63fb + 52da287 commit 31525d6
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 79 deletions.
118 changes: 104 additions & 14 deletions demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ var getFileMd5 = function (body, callback) {

function clone(obj) {
return map(obj, function (v) {
return typeof v === 'object' ? clone(v) : v;
return typeof v === 'object' && v !== null ? clone(v) : v;
});
}

Expand Down Expand Up @@ -629,6 +629,13 @@ var canFileSlice = (function () {
};
})();

var isCIHost = function(url) {
if (url && url.split('?')[0].match(/(.ci.|ci.|.ci)/g)) {
return true;
}
return false;
}

var util = {
noop: noop,
formatParams: formatParams,
Expand Down Expand Up @@ -659,6 +666,7 @@ var util = {
getAuth: getAuth,
compareVersion: compareVersion,
canFileSlice: canFileSlice,
isCIHost: isCIHost,
};

module.exports = util;
Expand Down Expand Up @@ -2339,7 +2347,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.13';
COS.version = '1.1.0';

module.exports = COS;

Expand Down Expand Up @@ -8098,6 +8106,53 @@ function multipartAbort(params, callback) {
});
}

/**
* 追加上传
* @param {Object} params 参数对象,必须
* @param {String} params.Bucket Bucket名称,必须
* @param {String} params.Region 地域名称,必须
* @param {String} params.Key object名称,必须
* @param {String} params.Body 上传文件的内容,只支持字符串
* @param {Number} params.Position 追加操作的起始点,单位为字节,必须
* @param {String} params.CacheControl RFC 2616 中定义的缓存策略,将作为 Object 元数据保存,非必须
* @param {String} params.ContentDisposition RFC 2616 中定义的文件名称,将作为 Object 元数据保存,非必须
* @param {String} params.ContentEncoding RFC 2616 中定义的编码格式,将作为 Object 元数据保存,非必须
* @param {String} params.ContentLength RFC 2616 中定义的 HTTP 请求内容长度(字节),必须
* @param {String} params.ContentType RFC 2616 中定义的内容类型(MIME),将作为 Object 元数据保存,非必须
* @param {String} params.Expect 当使用 Expect: 100-continue 时,在收到服务端确认后,才会发送请求内容,非必须
* @param {String} params.Expires RFC 2616 中定义的过期时间,将作为 Object 元数据保存,非必须
* @param {String} params.ACL 允许用户自定义文件权限,有效值:private | public-read,非必须
* @param {String} params.GrantRead 赋予被授权者读取对象的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
* @param {String} params.GrantReadAcp 赋予被授权者读取对象的访问控制列表(ACL)的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
* @param {String} params.GrantWriteAcp 赋予被授权者写入对象的访问控制列表(ACL)的权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
* @param {String} params.GrantFullControl 赋予被授权者操作对象的所有权限,格式:id="[OwnerUin]",可使用半角逗号(,)分隔多组被授权者,非必须
* @param {String} params.StorageClass 设置对象的存储级别,枚举值:STANDARD、STANDARD_IA、ARCHIVE,默认值:STANDARD,非必须
* @param {String} params.x-cos-meta-* 允许用户自定义的头部信息,将作为对象的元数据保存。大小限制2KB,非必须
* @param {String} params.ContentSha1 RFC 3174 中定义的 160-bit 内容 SHA-1 算法校验,非必须
* @param {String} params.ServerSideEncryption 支持按照指定的加密算法进行服务端数据加密,格式 x-cos-server-side-encryption: "AES256",非必须
* @param {Function} callback 回调函数,必须
* @return {Object} err 请求失败的错误,如果请求成功,则为空。https://cloud.tencent.com/document/product/436/7730
* @return {Object} data 返回的数据
*/
function appendObject(params, callback) {
submitRequest.call(this, {
Action: 'name/cos:AppendObject',
method: 'POST',
Bucket: params.Bucket,
Region: params.Region,
action: 'append',
Key: params.Key,
body: params.Body,
qs: {
position: params.Position
},
headers: params.Headers,
}, function (err, data) {
if (err) return callback(err);
callback(null, data);
});
}


/**
* cos 内置请求
Expand All @@ -8119,6 +8174,8 @@ function multipartAbort(params, callback) {
headers: params.Headers,
qs: params.Query,
body: params.Body,
Url: params.Url,
rawBody: params.RawBody,
}, function (err, data) {
if (err) return callback(err);
if (data && data.body) {
Expand Down Expand Up @@ -8616,7 +8673,7 @@ function _submitRequest(params, callback) {
var region = params.Region;
var object = params.Key;
var method = params.method || 'GET';
var url = params.url;
var url = params.url || params.Url;
var body = params.body;
var json = params.json;
var rawBody = params.rawBody;
Expand Down Expand Up @@ -8654,12 +8711,18 @@ function _submitRequest(params, callback) {
json: json,
};

// 兼容ci接口
var token = 'x-cos-security-token';
if (util.isCIHost(url)) {
token = 'x-ci-security-token';
}

// 获取签名
opt.headers.Authorization = params.AuthData.Authorization;
params.AuthData.Token && (opt.headers['token'] = params.AuthData.Token);
params.AuthData.ClientIP && (opt.headers['clientIP'] = params.AuthData.ClientIP);
params.AuthData.ClientUA && (opt.headers['clientUA'] = params.AuthData.ClientUA);
params.AuthData.XCosSecurityToken && (opt.headers['x-cos-security-token'] = params.AuthData.XCosSecurityToken);
params.AuthData.XCosSecurityToken && (opt.headers[token] = params.AuthData.XCosSecurityToken);

// 清理 undefined 和 null 字段
opt.headers && (opt.headers = util.clearKey(opt.headers));
Expand Down Expand Up @@ -8811,6 +8874,7 @@ var API_MAP = {
putObjectTagging: putObjectTagging,
getObjectTagging: getObjectTagging,
deleteObjectTagging: deleteObjectTagging,
appendObject: appendObject,

// 分块上传相关方法
uploadPartCopy: uploadPartCopy,
Expand Down Expand Up @@ -8840,22 +8904,47 @@ module.exports.init = function (COS, task) {
/* 18 */
/***/ (function(module, exports) {

function camSafeUrlEncode(str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}

function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = Object.keys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] || '';
list.push(key + '=' + encodeURIComponent(val));
}
return list.join('&');
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

var request = function (params, callback) {
var filePath = params.filePath;
var headers = params.headers || {};
var url = params.url;
var url = params.url || params.Url;
var method = params.method;
var onProgress = params.onProgress;
var requestTask;
Expand Down Expand Up @@ -8894,6 +8983,7 @@ var request = function (params, callback) {
'Expires',
'x-cos-storage-class',
'x-cos-security-token',
'x-ci-security-token',
];
for (var i in params.headers) {
if (params.headers.hasOwnProperty(i) && (i.indexOf('x-cos-meta-') > -1 || headerKeys.indexOf(i) > -1)) {
Expand Down
Loading

0 comments on commit 31525d6

Please sign in to comment.