bootstrap.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. //如果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/alioss/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. //如果上传接口不是阿里OSS,则不处理
  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. chunksize: this.options.chunkSize,
  58. chunkfilesize: chunk.dataBlock.data.size,
  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/alioss/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. aliosstoken: Config.upload.multipart.aliosstoken,
  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) {
  112. if (Config.upload.uploadmode === 'client') {
  113. ret = {code: 1, data: {url: '/' + file.key}};
  114. var url = ret.data.url || '';
  115. Fast.api.ajax({
  116. url: "/addons/alioss/index/notify",
  117. 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}
  118. }, function () {
  119. return false;
  120. }, function () {
  121. return false;
  122. });
  123. }
  124. } else {
  125. console.error(file.xhr);
  126. }
  127. } catch (e) {
  128. console.error(e);
  129. }
  130. _success.call(this, file, ret);
  131. });
  132. this.on("addedfile", function (file) {
  133. var that = this;
  134. setTimeout(function () {
  135. if (file.status === 'error') {
  136. return;
  137. }
  138. getFileMd5(file, function (md5) {
  139. var chunk = that.options.chunking && file.size > that.options.chunkSize ? 1 : 0;
  140. var params = $(that.element).data("params") || {};
  141. var category = typeof params.category !== 'undefined' ? params.category : ($(that.element).data("category") || '');
  142. category = typeof category === 'function' ? category.call(that, file) : category;
  143. Fast.api.ajax({
  144. url: "/addons/alioss/index/params",
  145. 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},
  146. }, function (data) {
  147. file.md5 = md5;
  148. file.id = data.id;
  149. file.key = data.key;
  150. file.date = data.date;
  151. file.uploadId = data.uploadId;
  152. file.policy = data.policy;
  153. file.signature = data.signature;
  154. file.partsAuthorization = data.partsAuthorization;
  155. file.params = data;
  156. file.category = category;
  157. if (file.status != 'error') {
  158. //开始上传
  159. that.enqueueFile(file);
  160. } else {
  161. that.removeFile(file);
  162. }
  163. return false;
  164. }, function () {
  165. that.removeFile(file);
  166. });
  167. });
  168. }, 0);
  169. });
  170. if (Config.upload.uploadmode === 'client') {
  171. var _method = this.options.method;
  172. var _url = this.options.url;
  173. this.options.method = function (files) {
  174. if (files[0].upload.chunked) {
  175. var chunk = null;
  176. files[0].upload.chunks.forEach(function (item) {
  177. if (item.status === 'uploading') {
  178. chunk = item;
  179. }
  180. });
  181. if (!chunk) {
  182. return "POST";
  183. } else {
  184. return "PUT";
  185. }
  186. }
  187. return _method;
  188. };
  189. this.options.url = function (files) {
  190. if (files[0].upload.chunked) {
  191. var chunk = null;
  192. files[0].upload.chunks.forEach(function (item) {
  193. if (item.status === 'uploading') {
  194. chunk = item;
  195. }
  196. });
  197. var index = chunk.dataBlock.chunkIndex;
  198. // debugger;
  199. this.options.headers = {"Authorization": "OSS " + files[0]['id'] + ":" + files[0]['partsAuthorization'][index], "x-oss-date": files[0]['date']};
  200. if (!chunk) {
  201. return Config.upload.uploadurl + "/" + files[0].key + "?uploadId=" + files[0].uploadId;
  202. } else {
  203. return Config.upload.uploadurl + "/" + files[0].key + "?partNumber=" + (index + 1) + "&uploadId=" + files[0].uploadId;
  204. }
  205. }
  206. return _url;
  207. };
  208. this.on("sending", function (file, xhr, formData) {
  209. var that = this;
  210. if (file.upload.chunked) {
  211. var _send = xhr.send;
  212. xhr.send = function () {
  213. var chunk = null;
  214. file.upload.chunks.forEach(function (item) {
  215. if (item.status == 'uploading') {
  216. chunk = item;
  217. }
  218. });
  219. if (chunk) {
  220. _send.call(xhr, chunk.dataBlock.data);
  221. }
  222. };
  223. }
  224. });
  225. }
  226. };
  227. });
  228. }