Skip to content

Commit

Permalink
Merge pull request #48 from livehigh/feat/add-uploadFile
Browse files Browse the repository at this point in the history
feat:新增高级上传uploadFile接口
  • Loading branch information
carsonxu authored Jun 11, 2021
2 parents 9263e5f + f7f8949 commit b36de15
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 4 deletions.
45 changes: 44 additions & 1 deletion demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.8';
COS.version = '1.0.9';

module.exports = COS;

Expand Down Expand Up @@ -9884,6 +9884,48 @@ function abortUploadTaskArray(params, callback) {
});
}

// 高级上传
function uploadFile(params, callback) {
var self = this;

// 判断多大的文件使用分片上传
var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize;

var taskList = [];

var FileSize = params.FileSize;
var fileInfo = {TaskId: ''};

// 整理 option,用于返回给回调
util.each(params, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
});

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

// 处理文件完成
var _onFileFinish = params.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data, fileInfo);
callback && callback(err, data);
};

// 添加上传任务
var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject';
taskList.push({
api: api,
params: params,
callback: onFileFinish,
});
self._addTasks(taskList);
}

// 批量上传文件
function uploadFiles(params, callback) {
Expand Down Expand Up @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) {
var API_MAP = {
sliceUploadFile: sliceUploadFile,
abortUploadTask: abortUploadTask,
uploadFile: uploadFile,
uploadFiles: uploadFiles,
sliceCopyFile: sliceCopyFile,
};
Expand Down
61 changes: 61 additions & 0 deletions demo/demo-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,67 @@ var dao = {
console.log(err || data)
});
},
'uploadFile 高级上传': function() {
var uploadFile = function(file) {
cos.uploadFile({
Bucket: config.Bucket,
Region: config.Region,
Key: file.name,
FilePath: file.path,
SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传
onProgress: function (info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
},
onFileFinish: function (err, data, options) {
console.log(options.Key + '上传' + (err ? '失败' : '完成'));
},
}, function (err, data) {
console.log(err || data);
});
}
wx.chooseMessageFile({
count: 10,
type: 'all',
success: function(res) {
uploadFile(res.tempFiles[0]);
}
});
},
'uploadFiles 批量上传': function() {
var uploadFiles = function(files) {
const fileList = files.map(function(file) {
return Object.assign(file, {
Bucket: config.Bucket,
Region: config.Region,
Key: file.name,
FilePath: file.path,
});
});
cos.uploadFiles({
files: fileList,
SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传
onProgress: function (info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
},
onFileFinish: function (err, data, options) {
console.log(options.Key + '上传' + (err ? '失败' : '完成'));
},
}, function (err, data) {
console.log(err || data);
});
}
wx.chooseMessageFile({
count: 10,
type: 'all',
success: function(res) {
uploadFiles(res.tempFiles);
}
});
},
'分片上传': function() {
var sliceUploadFile = function (file) {
var key = file.name;
Expand Down
45 changes: 44 additions & 1 deletion demo/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2338,7 +2338,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.8';
COS.version = '1.0.9';

module.exports = COS;

Expand Down Expand Up @@ -9884,6 +9884,48 @@ function abortUploadTaskArray(params, callback) {
});
}

// 高级上传
function uploadFile(params, callback) {
var self = this;

// 判断多大的文件使用分片上传
var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize;

var taskList = [];

var FileSize = params.FileSize;
var fileInfo = {TaskId: ''};

// 整理 option,用于返回给回调
util.each(params, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
});

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

// 处理文件完成
var _onFileFinish = params.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data, fileInfo);
callback && callback(err, data);
};

// 添加上传任务
var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject';
taskList.push({
api: api,
params: params,
callback: onFileFinish,
});
self._addTasks(taskList);
}

// 批量上传文件
function uploadFiles(params, callback) {
Expand Down Expand Up @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) {
var API_MAP = {
sliceUploadFile: sliceUploadFile,
abortUploadTask: abortUploadTask,
uploadFile: uploadFile,
uploadFiles: uploadFiles,
sliceCopyFile: sliceCopyFile,
};
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-wx-sdk-v5",
"version": "1.0.8",
"version": "1.0.9",
"description": "小程序 SDK for [腾讯云对象存储服务](https://cloud.tencent.com/product/cos)",
"main": "demo/lib/cos-wx-sdk-v5.js",
"scripts": {
Expand Down
43 changes: 43 additions & 0 deletions src/advance.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,48 @@ function abortUploadTaskArray(params, callback) {
});
}

// 高级上传
function uploadFile(params, callback) {
var self = this;

// 判断多大的文件使用分片上传
var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize;

var taskList = [];

var FileSize = params.FileSize;
var fileInfo = {TaskId: ''};

// 整理 option,用于返回给回调
util.each(params, function (v, k) {
if (typeof v !== 'object' && typeof v !== 'function') {
fileInfo[k] = v;
}
});

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

// 处理文件完成
var _onFileFinish = params.onFileFinish;
var onFileFinish = function (err, data) {
_onFileFinish && _onFileFinish(err, data, fileInfo);
callback && callback(err, data);
};

// 添加上传任务
var api = FileSize > SliceSize ? 'sliceUploadFile' : 'postObject';
taskList.push({
api: api,
params: params,
callback: onFileFinish,
});
self._addTasks(taskList);
}

// 批量上传文件
function uploadFiles(params, callback) {
Expand Down Expand Up @@ -1135,6 +1177,7 @@ function copySliceItem(params, callback) {
var API_MAP = {
sliceUploadFile: sliceUploadFile,
abortUploadTask: abortUploadTask,
uploadFile: uploadFile,
uploadFiles: uploadFiles,
sliceCopyFile: sliceCopyFile,
};
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.8';
COS.version = '1.0.9';

module.exports = COS;

0 comments on commit b36de15

Please sign in to comment.