uploadFileForExtStorage.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * 设置 uniCloud.uploadFile 默认上传到扩展存储
  3. * @param {String} provider 云储存供应商
  4. * @value unicloud 内置存储
  5. * @value extStorage 扩展存储
  6. * @param {String} domain 自定义域名,仅扩展存储有效
  7. * @param {Boolean} fileID2fileURL 是否将fileID转为fileURL
  8. * @param {Function} uploadFileOptions 获取上传参数的函数,仅扩展存储有效
  9. */
  10. function init(options = {}) {
  11. let {
  12. provider: defaultProvider,
  13. } = options;
  14. let originalDefaultProvider = defaultProvider;
  15. let extStorage = new ExtStorage(options);
  16. const uploadFile = uniCloud.uploadFile;
  17. uniCloud.uploadFile = (...e) => {
  18. let options = e[0] || {};
  19. let {
  20. provider = defaultProvider
  21. } = options;
  22. if (provider === "extStorage") {
  23. return extStorage.uploadFile(...e);
  24. } else {
  25. return uploadFile(...e);
  26. }
  27. }
  28. const getTempFileURL = uniCloud.getTempFileURL;
  29. uniCloud.getTempFileURL = (...e) => {
  30. let options = e[0] || {};
  31. let {
  32. provider = defaultProvider
  33. } = options;
  34. if (provider === "extStorage") {
  35. return extStorage.getTempFileURL(...e);
  36. } else {
  37. return getTempFileURL(...e);
  38. }
  39. }
  40. const deleteFile = uniCloud.deleteFile;
  41. uniCloud.deleteFile = (...e) => {
  42. let options = e[0] || {};
  43. let {
  44. provider = defaultProvider
  45. } = options;
  46. if (provider === "extStorage") {
  47. return extStorage.deleteFile(...e);
  48. } else {
  49. return deleteFile(...e);
  50. }
  51. }
  52. uniCloud.setCloudStorage = (data={}) => {
  53. let {
  54. provider,
  55. domain,
  56. fileID2fileURL,
  57. } = data;
  58. if (provider === null) {
  59. defaultProvider = originalDefaultProvider;
  60. } else if (provider) {
  61. defaultProvider = provider;
  62. }
  63. if (domain) extStorage.domain = domain;
  64. if (fileID2fileURL) extStorage.fileID2fileURL = fileID2fileURL;
  65. }
  66. }
  67. export default {
  68. init
  69. }
  70. class ExtStorage {
  71. constructor(data = {}) {
  72. let {
  73. uploadFileOptions,
  74. domain,
  75. fileID2fileURL
  76. } = data;
  77. this.uploadFileOptions = uploadFileOptions;
  78. this.domain = domain;
  79. this.fileID2fileURL = fileID2fileURL;
  80. }
  81. // 上传文件
  82. uploadFile(options) {
  83. let {
  84. filePath,
  85. cloudPath,
  86. } = options;
  87. const promiseRes = new Promise(async (resolve, reject) => {
  88. try {
  89. const uploadFileOptionsRes = await this.uploadFileOptions({
  90. cloudPath,
  91. domain: this.domain
  92. });
  93. const uploadTask = uni.uploadFile({
  94. ...uploadFileOptionsRes.uploadFileOptions, // 上传文件所需参数
  95. filePath, // 本地文件路径
  96. success: () => {
  97. const res = {
  98. cloudPath: uploadFileOptionsRes.cloudPath, // 文件云端路径
  99. fileID: uploadFileOptionsRes.fileID, // 文件ID
  100. fileURL: uploadFileOptionsRes.fileURL, // 文件URL(如果是私有权限,则此URL是无法直接访问的)
  101. };
  102. if (this.fileID2fileURL) {
  103. res.fileID = `https://${this.domain}/${res.cloudPath}`;
  104. }
  105. if (typeof options.success === "function") options.success(res);
  106. resolve(res);
  107. },
  108. fail: (err) => {
  109. if (typeof options.fail === "function") options.fail(err);
  110. reject(err);
  111. },
  112. complete: () => {
  113. if (typeof options.complete === "function") options.complete();
  114. }
  115. });
  116. // 监听上传进度
  117. uploadTask.onProgressUpdate((progressEvent) => {
  118. if (typeof options.onUploadProgress === "function") {
  119. const total = progressEvent.totalBytesExpectedToSend;
  120. const loaded = progressEvent.totalBytesSent;
  121. const progress = Math.round(loaded * 100 / total);
  122. options.onUploadProgress({
  123. total,
  124. loaded,
  125. progress
  126. });
  127. }
  128. });
  129. } catch (err) {
  130. if (typeof options.fail === "function") options.fail(err);
  131. reject(err);
  132. if (typeof options.complete === "function") options.complete();
  133. }
  134. });
  135. promiseRes.catch(() => {
  136. });
  137. return promiseRes;
  138. }
  139. // 获取临时文件下载地址
  140. getTempFileURL(options = {}) {
  141. let {
  142. fileList
  143. } = options;
  144. return new Promise((resolve, reject) => {
  145. let res = {
  146. fileList: fileList.map((item, index) => {
  147. let cloudPath = getCloudPath(item);
  148. return {
  149. fileID: item,
  150. tempFileURL: `https://${this.domain}/${cloudPath}`
  151. }
  152. })
  153. }
  154. if (typeof options.success === "function") options.success(res);
  155. resolve(res);
  156. if (typeof options.complete === "function") options.complete();
  157. });
  158. }
  159. // 删除文件
  160. deleteFile(options = {}) {
  161. // 扩展存储不允许前端删除文件(故此处直接返回)
  162. return new Promise((resolve, reject) => {
  163. let res = {
  164. fileList: []
  165. };
  166. if (typeof options.success === "function") options.success(res);
  167. resolve(res);
  168. if (typeof options.complete === "function") options.complete();
  169. });
  170. }
  171. }
  172. function getCloudPath(cloudPath) {
  173. const qiniuPrefix = 'qiniu://';
  174. if (cloudPath.indexOf(qiniuPrefix) === 0) {
  175. cloudPath = cloudPath.substring(qiniuPrefix.length);
  176. } else if (cloudPath.indexOf('http://') === 0 || cloudPath.indexOf('https://') === 0) {
  177. let startIndex = cloudPath.indexOf('://') + 3;
  178. startIndex = cloudPath.indexOf('/', startIndex);
  179. let endIndex = cloudPath.indexOf('?') === -1 ? cloudPath.length : cloudPath.indexOf('?');
  180. endIndex = cloudPath.indexOf('#') !== -1 && cloudPath.indexOf('#') < endIndex ? cloudPath.indexOf('#') : endIndex;
  181. cloudPath = cloudPath.substring(startIndex + 1, endIndex);
  182. }
  183. return cloudPath
  184. }