Skip to content

Commit

Permalink
Dev/2.13.2 (#200)
Browse files Browse the repository at this point in the history
* upd: uploadFiles 支持传入 Body

* fix: 类型优化
  • Loading branch information
livehigh authored Mar 5, 2024
1 parent 82b7778 commit 43d9704
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 52 deletions.
8 changes: 7 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ declare namespace COS {
/** 上传/下载的进度回调方法 */
type onProgress = (params: ProgressInfo) => any;

type CopySourceParserFunction = (source: string) => null | { Bucket: string; Region: string; Key: string };

// 实例参数
interface COSOptions {
/** 固定密钥的 SecretId,可从{@link https://console.cloud.tencent.com/cam/capi|API密钥管理}获取 */
Expand Down Expand Up @@ -199,6 +201,8 @@ declare namespace COS {
/** 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true */
ForceSignHost?: boolean;
AutoSwitchHost?: boolean;
/** 自定义拷贝源解析器 */
CopySourceParser?: null | CopySourceParserFunction;
/** 获取签名的回调方法,如果没有 SecretId、SecretKey 时,必选 */
getAuthorization?: (
options: GetAuthorizationOptions,
Expand Down Expand Up @@ -1808,7 +1812,9 @@ Bulk:批量模式,恢复时间为24 - 48小时。 */
// uploadFiles
type UploadFileItemParams = (PutObjectParams | SliceUploadFileParams) & {
/** 要上传的本地文件路径 */
FilePath: string;
FilePath?: string;
/** 要上传对象内容 */
Body?: UploadBody;
/** 上传的进度回调方法 */
onProgress?: onProgress;
/** 上传完成回调方法 */
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-nodejs-sdk-v5",
"version": "2.13.1",
"version": "2.13.2",
"description": "cos nodejs sdk v5",
"main": "index.js",
"types": "index.d.ts",
Expand Down
108 changes: 59 additions & 49 deletions sdk/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,60 +971,70 @@ function uploadFiles(params, callback) {
var taskList = [];
var count = params.files.length;
util.each(params.files, function (fileParams, index) {
fs.stat(fileParams.FilePath, function (err, stat) {
var isDir = stat ? stat.isDirectory() : false;
var FileSize = (fileParams.ContentLength = stat ? stat.size : 0);
var fileInfo = { Index: index, TaskId: '' };

// 更新文件总大小
TotalSize += FileSize;

// 整理 option,用于返回给回调
util.each(fileParams, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
var isDir = false;
var FileSize = 0;
if (fileParams.Body) {
util.getFileSize('putObject', fileParams, function (err, size) {
FileSize = fileParams.ContentLengt = size;
});
} else if (fileParams.FilePath) {
var stat;
try {
stat = fs.statSync(fileParams.FilePath);
} catch (e) {}
isDir = stat ? stat.isDirectory() : false;
FileSize = fileParams.ContentLength = stat ? stat.size : 0;
}
var fileInfo = { Index: index, TaskId: '' };

// 处理单个文件 TaskReady
var _onTaskReady = fileParams.onTaskReady;
var onTaskReady = function (tid) {
fileInfo.TaskId = tid;
_onTaskReady && _onTaskReady(tid);
};
fileParams.onTaskReady = onTaskReady;

// 处理单个文件进度
var PreAddSize = 0;
var _onProgress = fileParams.onProgress;
var onProgress = function (info) {
TotalFinish = TotalFinish - PreAddSize + info.loaded;
PreAddSize = info.loaded;
_onProgress && _onProgress(info);
onTotalProgress({ loaded: TotalFinish, total: TotalSize });
};
fileParams.onProgress = onProgress;

// 处理单个文件完成
var _onFileFinish = fileParams.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data);
onTotalFileFinish && onTotalFileFinish(err, data, fileInfo);
};
// 更新文件总大小
TotalSize += FileSize;

// 添加上传任务
var api = FileSize <= SliceSize || isDir ? 'putObject' : 'sliceUploadFile';
if (api === 'putObject') {
fileParams.Body = isDir ? '' : fs.createReadStream(fileParams.FilePath);
fileParams.Body.isSdkCreated = true;
// 整理 option,用于返回给回调
util.each(fileParams, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
taskList.push({
api: api,
params: fileParams,
callback: onFileFinish,
});
--count === 0 && self._addTasks(taskList);
});

// 处理单个文件 TaskReady
var _onTaskReady = fileParams.onTaskReady;
var onTaskReady = function (tid) {
fileInfo.TaskId = tid;
_onTaskReady && _onTaskReady(tid);
};
fileParams.onTaskReady = onTaskReady;

// 处理单个文件进度
var PreAddSize = 0;
var _onProgress = fileParams.onProgress;
var onProgress = function (info) {
TotalFinish = TotalFinish - PreAddSize + info.loaded;
PreAddSize = info.loaded;
_onProgress && _onProgress(info);
onTotalProgress({ loaded: TotalFinish, total: TotalSize });
};
fileParams.onProgress = onProgress;

// 处理单个文件完成
var _onFileFinish = fileParams.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data);
onTotalFileFinish && onTotalFileFinish(err, data, fileInfo);
};

// 添加上传任务,传入 Body 则只支持简单上传
var api = FileSize <= SliceSize || isDir || fileParams.Body ? 'putObject' : 'sliceUploadFile';
if (api === 'putObject' && fileParams.FilePath && !fileParams.Body) {
fileParams.Body = isDir ? '' : fs.createReadStream(fileParams.FilePath);
fileParams.Body.isSdkCreated = true;
}
taskList.push({
api: api,
params: fileParams,
callback: onFileFinish,
});
--count === 0 && self._addTasks(taskList);
});
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3092,7 +3092,7 @@ function multipartComplete(params, callback) {
protocol: self.options.Protocol,
domain: self.options.Domain,
bucket: params.Bucket,
region: params.Region,
region: !self.options.UseAccelerate ? params.Region : 'accelerate',
object: params.Key,
isLocation: true,
});
Expand Down
1 change: 1 addition & 0 deletions sdk/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var defaultOptions = {
ConfCwd: '',
ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
AutoSwitchHost: true,
CopySourceParser: null, // 自定义拷贝源解析器
// 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey
Credentials: {
secretId: '',
Expand Down

0 comments on commit 43d9704

Please sign in to comment.