bootstrap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'cos') {
  2. require(['upload'], function (Upload) {
  3. //获取文件MD5值
  4. var getFileMd5 = function (file, cb) {
  5. require(['../addons/cos/js/spark'], function (SparkMD5) {
  6. var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
  7. chunkSize = 10 * 1024 * 1024,
  8. chunks = Math.ceil(file.size / chunkSize),
  9. currentChunk = 0,
  10. spark = new SparkMD5.ArrayBuffer(),
  11. fileReader = new FileReader();
  12. fileReader.onload = function (e) {
  13. spark.append(e.target.result);
  14. currentChunk++;
  15. if (currentChunk < chunks) {
  16. loadNext();
  17. } else {
  18. cb && cb(spark.end());
  19. }
  20. };
  21. fileReader.onerror = function () {
  22. console.warn('文件读取错误');
  23. };
  24. function loadNext() {
  25. var start = currentChunk * chunkSize,
  26. end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
  27. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  28. }
  29. loadNext();
  30. });
  31. };
  32. //初始化中完成判断
  33. Upload.events.onInit = function () {
  34. //如果上传接口不是COS,则不处理
  35. if (this.options.url !== Config.upload.uploadurl) {
  36. return;
  37. }
  38. $.extend(this.options, {
  39. //关闭自动处理队列功能
  40. autoQueue: false,
  41. params: function (files, xhr, chunk) {
  42. var params = Config.upload.multipart;
  43. if (chunk) {
  44. return $.extend({}, params, {
  45. filesize: chunk.file.size,
  46. filename: chunk.file.name,
  47. chunkid: chunk.file.upload.uuid,
  48. chunkindex: chunk.index,
  49. chunkcount: chunk.file.upload.totalChunkCount,
  50. chunkfilesize: chunk.dataBlock.data.size,
  51. chunksize: this.options.chunkSize,
  52. width: chunk.file.width || 0,
  53. height: chunk.file.height || 0,
  54. type: chunk.file.type,
  55. uploadId: chunk.file.uploadId,
  56. key: chunk.file.key,
  57. });
  58. } else {
  59. params = $.extend({}, params, files[0].params);
  60. params.category = files[0].category || '';
  61. }
  62. return params;
  63. },
  64. chunkSuccess: function (chunk, file, response) {
  65. var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
  66. this.etags = this.etags ? this.etags : [];
  67. this.etags[chunk.index] = etag;
  68. },
  69. chunksUploaded: function (file, done) {
  70. var that = this;
  71. Fast.api.ajax({
  72. url: "/addons/cos/index/upload",
  73. data: {
  74. action: 'merge',
  75. filesize: file.size,
  76. filename: file.name,
  77. chunkid: file.upload.uuid,
  78. chunkcount: file.upload.totalChunkCount,
  79. md5: file.md5,
  80. key: file.key,
  81. uploadId: file.uploadId,
  82. etags: this.etags,
  83. category: file.category || '',
  84. costoken: Config.upload.multipart.costoken,
  85. },
  86. }, function (data, ret) {
  87. done(JSON.stringify(ret));
  88. return false;
  89. }, function (data, ret) {
  90. file.accepted = false;
  91. that._errorProcessing([file], ret.msg);
  92. return false;
  93. });
  94. },
  95. });
  96. var _success = this.options.success;
  97. //先移除已有的事件
  98. this.off("success", _success).on("success", function (file, response) {
  99. var ret = {code: 0, msg: response};
  100. try {
  101. if (response) {
  102. ret = typeof response === 'string' ? JSON.parse(response) : response;
  103. }
  104. if (file.xhr.status === 200 || file.xhr.status === 204) {
  105. var url = '/' + file.key;
  106. ret = {code: 1, data: {url: url}};
  107. Fast.api.ajax({
  108. url: "/addons/cos/index/notify",
  109. data: {name: file.name, url: url, md5: file.md5, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, category: file.category || '', costoken: Config.upload.multipart.costoken}
  110. }, function () {
  111. return false;
  112. }, function () {
  113. return false;
  114. });
  115. }
  116. } catch (e) {
  117. console.error(e);
  118. }
  119. _success.call(this, file, ret);
  120. });
  121. this.on("addedfile", function (file) {
  122. var that = this;
  123. setTimeout(function () {
  124. if (file.status === 'error') {
  125. return;
  126. }
  127. getFileMd5(file, function (md5) {
  128. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  129. var params = $(that.element).data("params") || {};
  130. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  131. Fast.api.ajax({
  132. url: "/addons/cos/index/params",
  133. data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, costoken: Config.upload.multipart.costoken},
  134. }, function (data) {
  135. file.md5 = md5;
  136. file.id = data.id;
  137. file.key = data.key;
  138. file.date = data.date;
  139. file.uploadId = data.uploadId;
  140. file.policy = data.policy;
  141. file.signature = data.signature;
  142. file.partsAuthorization = data.partsAuthorization;
  143. file.params = data;
  144. file.category = category;
  145. if (file.status != 'error') {
  146. //开始上传
  147. that.enqueueFile(file);
  148. } else {
  149. that.removeFile(file);
  150. }
  151. return false;
  152. });
  153. });
  154. }, 0);
  155. });
  156. if (Config.upload.uploadmode === 'client') {
  157. var _method = this.options.method;
  158. var _url = this.options.url;
  159. this.options.method = function (files) {
  160. if (files[0].upload.chunked) {
  161. var chunk = null;
  162. files[0].upload.chunks.forEach(function (item) {
  163. if (item.status === 'uploading') {
  164. chunk = item;
  165. }
  166. });
  167. if (!chunk) {
  168. return "POST";
  169. } else {
  170. return "PUT";
  171. }
  172. }
  173. return _method;
  174. };
  175. this.options.url = function (files) {
  176. if (files[0].upload.chunked) {
  177. var chunk = null;
  178. files[0].upload.chunks.forEach(function (item) {
  179. if (item.status === 'uploading') {
  180. chunk = item;
  181. }
  182. });
  183. var index = chunk.dataBlock.chunkIndex;
  184. // debugger;
  185. this.options.headers = {"Authorization": files[0]['partsAuthorization'][index], "x-date": files[0]['date']};
  186. if (!chunk) {
  187. return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
  188. } else {
  189. return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
  190. }
  191. }
  192. return _url;
  193. };
  194. this.options.params = function (files, xhr, chunk) {
  195. var params = Config.upload.multipart;
  196. if (chunk) {
  197. return $.extend({}, params, {
  198. filesize: chunk.file.size,
  199. filename: chunk.file.name,
  200. chunkid: chunk.file.upload.uuid,
  201. chunkindex: chunk.index,
  202. chunkcount: chunk.file.upload.totalChunkCount,
  203. chunkfilesize: chunk.dataBlock.data.size,
  204. width: chunk.file.width || 0,
  205. height: chunk.file.height || 0,
  206. type: chunk.file.type,
  207. });
  208. } else {
  209. var retParams = $.extend({}, params, files[0].params || {});
  210. delete retParams.costoken;
  211. return retParams;
  212. }
  213. };
  214. this.on("sending", function (file, xhr, formData) {
  215. var that = this;
  216. if (file.upload.chunked) {
  217. var _send = xhr.send;
  218. xhr.send = function () {
  219. var chunk = null;
  220. file.upload.chunks.forEach(function (item) {
  221. if (item.status == 'uploading') {
  222. chunk = item;
  223. }
  224. });
  225. if (chunk) {
  226. _send.call(xhr, chunk.dataBlock.data);
  227. }
  228. };
  229. } else {
  230. }
  231. });
  232. }
  233. };
  234. });
  235. }