From 6bde57706e97562ff3087f680b822805b5a1872a Mon Sep 17 00:00:00 2001 From: chrisftian Date: Fri, 11 Jun 2021 11:41:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat=EF=BC=9A=E6=96=B0=E5=A2=9E=E9=AB=98?= =?UTF-8?q?=E7=BA=A7=E4=B8=8A=E4=BC=A0uploadFile=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo-album/lib/cos-wx-sdk-v5.js | 45 +++++++++++++++++++++++- demo/demo-sdk.js | 61 +++++++++++++++++++++++++++++++++ demo/lib/cos-wx-sdk-v5.js | 45 +++++++++++++++++++++++- package.json | 2 +- src/advance.js | 43 +++++++++++++++++++++++ src/cos.js | 2 +- 6 files changed, 194 insertions(+), 4 deletions(-) diff --git a/demo-album/lib/cos-wx-sdk-v5.js b/demo-album/lib/cos-wx-sdk-v5.js index bb36b64..e331f03 100644 --- a/demo-album/lib/cos-wx-sdk-v5.js +++ b/demo-album/lib/cos-wx-sdk-v5.js @@ -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; @@ -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 = []; + // util.each(params.files, function (fileParams, index) { + 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) { @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/demo/demo-sdk.js b/demo/demo-sdk.js index 3eeb6a7..134ec00 100644 --- a/demo/demo-sdk.js +++ b/demo/demo-sdk.js @@ -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; diff --git a/demo/lib/cos-wx-sdk-v5.js b/demo/lib/cos-wx-sdk-v5.js index bb36b64..e331f03 100644 --- a/demo/lib/cos-wx-sdk-v5.js +++ b/demo/lib/cos-wx-sdk-v5.js @@ -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; @@ -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 = []; + // util.each(params.files, function (fileParams, index) { + 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) { @@ -10217,6 +10259,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/package.json b/package.json index b059284..04f6119 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/advance.js b/src/advance.js index 7df61cd..8322f6c 100644 --- a/src/advance.js +++ b/src/advance.js @@ -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 = []; + // util.each(params.files, function (fileParams, index) { + 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) { @@ -1135,6 +1177,7 @@ function copySliceItem(params, callback) { var API_MAP = { sliceUploadFile: sliceUploadFile, abortUploadTask: abortUploadTask, + uploadFile: uploadFile, uploadFiles: uploadFiles, sliceCopyFile: sliceCopyFile, }; diff --git a/src/cos.js b/src/cos.js index bf8180f..80f4c01 100644 --- a/src/cos.js +++ b/src/cos.js @@ -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; From f7f89495694eb07f4d19f0759320ac2962e58abe Mon Sep 17 00:00:00 2001 From: chrisftian Date: Fri, 11 Jun 2021 14:15:53 +0800 Subject: [PATCH 2/2] update uploadFile --- demo-album/lib/cos-wx-sdk-v5.js | 2 +- demo/lib/cos-wx-sdk-v5.js | 2 +- src/advance.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demo-album/lib/cos-wx-sdk-v5.js b/demo-album/lib/cos-wx-sdk-v5.js index e331f03..dfbab8b 100644 --- a/demo-album/lib/cos-wx-sdk-v5.js +++ b/demo-album/lib/cos-wx-sdk-v5.js @@ -9892,7 +9892,7 @@ function uploadFile(params, callback) { var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; var taskList = []; - // util.each(params.files, function (fileParams, index) { + var FileSize = params.FileSize; var fileInfo = {TaskId: ''}; diff --git a/demo/lib/cos-wx-sdk-v5.js b/demo/lib/cos-wx-sdk-v5.js index e331f03..dfbab8b 100644 --- a/demo/lib/cos-wx-sdk-v5.js +++ b/demo/lib/cos-wx-sdk-v5.js @@ -9892,7 +9892,7 @@ function uploadFile(params, callback) { var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; var taskList = []; - // util.each(params.files, function (fileParams, index) { + var FileSize = params.FileSize; var fileInfo = {TaskId: ''}; diff --git a/src/advance.js b/src/advance.js index 8322f6c..090a730 100644 --- a/src/advance.js +++ b/src/advance.js @@ -810,7 +810,7 @@ function uploadFile(params, callback) { var SliceSize = params.SliceSize === undefined ? self.options.SliceSize : params.SliceSize; var taskList = []; - // util.each(params.files, function (fileParams, index) { + var FileSize = params.FileSize; var fileInfo = {TaskId: ''};