bootstrap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. if (typeof Config.upload.storage !== 'undefined' && Config.upload.storage === 'alioss') {
  2. require(['upload'], function (Upload) {
  3. //获取文件MD5值
  4. var getFileMd5 = function (file, cb) {
  5. require(['../addons/alioss/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. var _onInit = Upload.events.onInit;
  33. //初始化中完成判断
  34. Upload.events.onInit = function () {
  35. _onInit.apply(this, Array.prototype.slice.apply(arguments));
  36. //如果上传接口不是阿里OSS,则不处理
  37. if (this.options.url !== Config.upload.uploadurl) {
  38. return;
  39. }
  40. $.extend(this.options, {
  41. //关闭自动处理队列功能
  42. autoQueue: false,
  43. params: function (files, xhr, chunk) {
  44. var params = Config.upload.multipart;
  45. if (chunk) {
  46. return $.extend({}, params, {
  47. filesize: chunk.file.size,
  48. filename: chunk.file.name,
  49. chunkid: chunk.file.upload.uuid,
  50. chunkindex: chunk.index,
  51. chunkcount: chunk.file.upload.totalChunkCount,
  52. chunksize: this.options.chunkSize,
  53. chunkfilesize: chunk.dataBlock.data.size,
  54. width: chunk.file.width || 0,
  55. height: chunk.file.height || 0,
  56. type: chunk.file.type,
  57. uploadId: chunk.file.uploadId,
  58. key: chunk.file.key,
  59. });
  60. } else {
  61. params = $.extend({}, params, files[0].params);
  62. params.category = files[0].category || '';
  63. }
  64. return params;
  65. },
  66. chunkSuccess: function (chunk, file, response) {
  67. var etag = chunk.xhr.getResponseHeader("ETag").replace(/(^")|("$)/g, '');
  68. file.etags = file.etags ? file.etags : [];
  69. file.etags[chunk.index] = etag;
  70. },
  71. chunksUploaded: function (file, done) {
  72. var that = this;
  73. Fast.api.ajax({
  74. url: "/addons/alioss/index/upload",
  75. data: {
  76. action: 'merge',
  77. filesize: file.size,
  78. filename: file.name,
  79. chunkid: file.upload.uuid,
  80. chunkcount: file.upload.totalChunkCount,
  81. md5: file.md5,
  82. key: file.key,
  83. uploadId: file.uploadId,
  84. etags: file.etags,
  85. category: file.category || '',
  86. aliosstoken: Config.upload.multipart.aliosstoken,
  87. },
  88. }, function (data, ret) {
  89. done(JSON.stringify(ret));
  90. return false;
  91. }, function (data, ret) {
  92. file.accepted = false;
  93. that._errorProcessing([file], ret.msg);
  94. return false;
  95. });
  96. },
  97. });
  98. var _success = this.options.success;
  99. //先移除已有的事件
  100. this.off("success", _success).on("success", function (file, response) {
  101. var ret = {code: 0, msg: response};
  102. try {
  103. if (response) {
  104. ret = typeof response === 'string' ? JSON.parse(response) : response;
  105. }
  106. if (file.xhr.status === 200) {
  107. if (Config.upload.uploadmode === 'client') {
  108. ret = {code: 1, data: {url: '/' + file.key}};
  109. }
  110. if (ret.code == 1) {
  111. var url = ret.data.url || '';
  112. Fast.api.ajax({
  113. url: "/addons/alioss/index/notify",
  114. 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 || '', aliosstoken: Config.upload.multipart.aliosstoken}
  115. }, function () {
  116. return false;
  117. }, function () {
  118. return false;
  119. });
  120. } else {
  121. console.error(ret);
  122. }
  123. } else {
  124. console.error(file.xhr);
  125. }
  126. } catch (e) {
  127. console.error(e);
  128. }
  129. _success.call(this, file, ret);
  130. });
  131. this.on("addedfile", function (file) {
  132. var that = this;
  133. setTimeout(function () {
  134. if (file.status === 'error') {
  135. return;
  136. }
  137. getFileMd5(file, function (md5) {
  138. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  139. var params = $(that.element).data("params") || {};
  140. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  141. category = typeof category === 'function' ? category.call(that, file) : category;
  142. Fast.api.ajax({
  143. url: "/addons/alioss/index/params",
  144. data: {method: 'POST', category: category, md5: md5, name: file.name, type: file.type, size: file.size, chunk: chunk, chunksize: that.options.chunkSize, aliosstoken: Config.upload.multipart.aliosstoken},
  145. }, function (data) {
  146. file.md5 = md5;
  147. file.id = data.id;
  148. file.key = data.key;
  149. file.date = data.date;
  150. file.uploadId = data.uploadId;
  151. file.policy = data.policy;
  152. file.signature = data.signature;
  153. file.partsAuthorization = data.partsAuthorization;
  154. file.params = data;
  155. file.category = category;
  156. if (file.status != 'error') {
  157. //开始上传
  158. that.enqueueFile(file);
  159. } else {
  160. that.removeFile(file);
  161. }
  162. return false;
  163. }, function () {
  164. that.removeFile(file);
  165. });
  166. });
  167. }, 0);
  168. });
  169. if (Config.upload.uploadmode === 'client') {
  170. var _method = this.options.method;
  171. var _url = this.options.url;
  172. this.options.method = function (files) {
  173. if (files[0].upload.chunked) {
  174. var chunk = null;
  175. files[0].upload.chunks.forEach(function (item) {
  176. if (item.status === 'uploading') {
  177. chunk = item;
  178. }
  179. });
  180. if (!chunk) {
  181. return "POST";
  182. } else {
  183. return "PUT";
  184. }
  185. }
  186. return _method;
  187. };
  188. this.options.url = function (files) {
  189. if (files[0].upload.chunked) {
  190. var chunk = null;
  191. files[0].upload.chunks.forEach(function (item) {
  192. if (item.status === 'uploading') {
  193. chunk = item;
  194. }
  195. });
  196. var index = chunk.dataBlock.chunkIndex;
  197. // debugger;
  198. this.options.headers = {"Authorization": "OSS " + files[0]['id'] + ":" + files[0]['partsAuthorization'][index], "x-oss-date": files[0]['date']};
  199. if (!chunk) {
  200. return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
  201. } else {
  202. return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
  203. }
  204. }
  205. return _url;
  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. }
  223. });
  224. }
  225. };
  226. });
  227. }