bootstrap.js 12 KB

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