qiniuUploader.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. "use strict";
  2. const qiniuUploader = function() {
  3. var config = {
  4. qiniuRegion: "",
  5. qiniuImageURLPrefix: "",
  6. qiniuUploadToken: "",
  7. qiniuUploadTokenURL: "",
  8. qiniuUploadTokenFunction: null,
  9. qiniuShouldUseQiniuFileName: false
  10. };
  11. function init(options) {
  12. config = {
  13. qiniuRegion: "",
  14. qiniuImageURLPrefix: "",
  15. qiniuUploadToken: "",
  16. qiniuUploadTokenURL: "",
  17. qiniuUploadTokenFunction: null,
  18. qiniuShouldUseQiniuFileName: false
  19. };
  20. updateConfigWithOptions(options);
  21. }
  22. function updateConfigWithOptions(options) {
  23. if (options.region) {
  24. config.qiniuRegion = options.region;
  25. } else {
  26. console.error("qiniu uploader need your bucket region");
  27. }
  28. if (options.uptoken) {
  29. config.qiniuUploadToken = options.uptoken;
  30. } else if (options.uptokenURL) {
  31. config.qiniuUploadTokenURL = options.uptokenURL;
  32. } else if (options.uptokenFunc) {
  33. config.qiniuUploadTokenFunction = options.uptokenFunc;
  34. }
  35. if (options.domain) {
  36. config.qiniuImageURLPrefix = options.domain;
  37. }
  38. config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName;
  39. }
  40. function upload(filePath, success, fail, options, progress, cancelTask) {
  41. if (filePath == null) {
  42. console.error("qiniu uploader need filePath to upload");
  43. return;
  44. }
  45. if (options) {
  46. updateConfigWithOptions(options);
  47. }
  48. if (config.qiniuUploadToken) {
  49. doUpload(filePath, success, fail, options, progress, cancelTask);
  50. } else if (config.qiniuUploadTokenURL) {
  51. getQiniuToken(function() {
  52. doUpload(filePath, success, fail, options, progress, cancelTask);
  53. });
  54. } else if (config.qiniuUploadTokenFunction) {
  55. config.qiniuUploadToken = config.qiniuUploadTokenFunction();
  56. if (config.qiniuUploadToken == null && config.qiniuUploadToken.length > 0) {
  57. console.error("qiniu UploadTokenFunction result is null, please check the return value");
  58. return;
  59. }
  60. doUpload(filePath, success, fail, options, progress, cancelTask);
  61. } else {
  62. console.error("qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]");
  63. return;
  64. }
  65. }
  66. function doUpload(filePath, success, fail, options, progress, cancelTask) {
  67. if (config.qiniuUploadToken == null && config.qiniuUploadToken.length > 0) {
  68. console.error("qiniu UploadToken is null, please check the init config or networking");
  69. return;
  70. }
  71. var url = uploadURLFromRegionCode(config.qiniuRegion);
  72. var fileName = filePath.split("//")[1];
  73. if (options && options.key) {
  74. fileName = options.key;
  75. }
  76. var formData = {
  77. "token": config.qiniuUploadToken
  78. };
  79. if (!config.qiniuShouldUseQiniuFileName) {
  80. formData["key"] = fileName;
  81. }
  82. var uploadTask = wx.uploadFile({
  83. url,
  84. filePath,
  85. name: "file",
  86. formData,
  87. success: function(res) {
  88. var dataString = res.data;
  89. if (res.data.hasOwnProperty("type") && res.data.type === "Buffer") {
  90. dataString = String.fromCharCode.apply(null, res.data.data);
  91. }
  92. try {
  93. var dataObject = JSON.parse(dataString);
  94. var imageUrl = config.qiniuImageURLPrefix + "/" + dataObject.key;
  95. dataObject.imageURL = imageUrl;
  96. if (success) {
  97. success(dataObject);
  98. }
  99. } catch (e) {
  100. console.log("parse JSON failed, origin String is: " + dataString);
  101. if (fail) {
  102. fail(e);
  103. }
  104. }
  105. },
  106. fail: function(error) {
  107. console.error(error);
  108. if (fail) {
  109. fail(error);
  110. }
  111. }
  112. });
  113. uploadTask.onProgressUpdate((res) => {
  114. progress && progress(res);
  115. });
  116. cancelTask && cancelTask(() => {
  117. uploadTask.abort();
  118. });
  119. }
  120. function getQiniuToken(callback) {
  121. wx.request({
  122. url: config.qiniuUploadTokenURL,
  123. success: function(res) {
  124. var token = res.data.uptoken;
  125. if (token && token.length > 0) {
  126. config.qiniuUploadToken = token;
  127. if (callback) {
  128. callback();
  129. }
  130. } else {
  131. console.error("qiniuUploader cannot get your token, please check the uptokenURL or server");
  132. }
  133. },
  134. fail: function(error) {
  135. console.error("qiniu UploadToken is null, please check the init config or networking: " + error);
  136. }
  137. });
  138. }
  139. function uploadURLFromRegionCode(code) {
  140. var uploadURL = null;
  141. switch (code) {
  142. case "ECN":
  143. uploadURL = "https://up.qbox.me";
  144. break;
  145. case "NCN":
  146. uploadURL = "https://up-z1.qbox.me";
  147. break;
  148. case "SCN":
  149. uploadURL = "https://up-z2.qbox.me";
  150. break;
  151. case "NA":
  152. uploadURL = "https://up-na0.qbox.me";
  153. break;
  154. case "ASG":
  155. uploadURL = "https://up-as0.qbox.me";
  156. break;
  157. default:
  158. console.error("please make the region is with one of [ECN, SCN, NCN, NA, ASG]");
  159. }
  160. return uploadURL;
  161. }
  162. return {
  163. init,
  164. upload
  165. };
  166. }();
  167. exports.qiniuUploader = qiniuUploader;