bootstrap.js 11 KB

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