上传插件,不包含上传的前端实现,只提供后端接口等,其他地方接入插件上传。包括上传进度、断点续传等

index.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = UploadSdk;
  6. var _contants = require('./contants');
  7. var _utils = require('./utils');
  8. var _utils2 = _interopRequireDefault(_utils);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function FileFactory(file) {
  11. this.offset = 0; //用于断点续传,默认为 0
  12. this.BYTES_PER_CHUNK = 1024 * 1024;
  13. this.file = file;
  14. this.fileSize = file.size;
  15. this.fileType = file.name.split('.')[1].toLowerCase();
  16. this.chunkNum = this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK) : 1;
  17. this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize : this.BYTES_PER_CHUNK;
  18. }
  19. FileFactory.prototype.setOffset = function (offset) {
  20. this.offset = offset;
  21. };
  22. function UploadSdk(host, origin, token, file) {
  23. this.handlers = []; //用于处理出触发事件
  24. this.host = host.indexOf('http:') > -1 ? 'http:' + host : host;
  25. this.origin = origin;
  26. this.token = token;
  27. this.file = file;
  28. this.generateMd5 = function (callback) {
  29. (0, _utils2.default)(this.file).then(function (result) {
  30. callback(result);
  31. });
  32. };
  33. this.imageUploadAction = function () {
  34. var _this = this;
  35. return new Promise(function (resolve, reject) {
  36. var fileFactory = new FileFactory(_this.file);
  37. if (!fileFactory.fileType.match('[(jpg)|(png)|(gif)]+$')) {
  38. reject(_contants.Constants.IMAGE_TYPE_ERROR);
  39. }
  40. _this.generateMd5(function (md5) {
  41. _this.md5 = md5;
  42. _this.postImage(_this.md5, fileFactory).then(function (res) {
  43. resolve(res);
  44. }).catch(function (err) {
  45. reject(err);
  46. });
  47. });
  48. });
  49. };
  50. this.videoUploadAction = function () {
  51. var _this2 = this;
  52. return new Promise(function (resolve, reject) {
  53. if (!_this2.file.type.match('[(mp4)|(rmvb)|(flv)|(mpeg)|(avi)]+$')) {
  54. reject(_contants.Constants.VIDOE_TYPE_ERROR);
  55. }
  56. _this2.postVideo().then(function (res) {
  57. resolve(res);
  58. }).catch(function (err) {
  59. reject(err);
  60. });
  61. });
  62. };
  63. this.postVideo = function () {
  64. var _this3 = this;
  65. var self = this;
  66. return new Promise(function (resolve, reject) {
  67. var xhr = new XMLHttpRequest();
  68. xhr.open('POST', _this3.host + _contants.Constants.VIDEO_MULTIPART + _this3.origin, true);
  69. xhr.withCredentials = true;
  70. xhr.setRequestHeader('Authorization', 'Bearer ' + _this3.token);
  71. xhr.setRequestHeader('Content-type', 'application/json');
  72. xhr.upload.addEventListener("progress", function updateProgress(oEvent) {
  73. if (oEvent.lengthComputable) {
  74. var percentComplete = oEvent.loaded / oEvent.total * 100;
  75. self.emitUpdateProgress(percentComplete);
  76. }
  77. }, false);
  78. var fd = new FormData();
  79. fd.append('file', _this3.file);
  80. fd.append('code_rate', 'ld,sd,hd');
  81. xhr.send(fd);
  82. xhr.onreadystatechange = function () {
  83. if (xhr.readyState === 4) {
  84. var obj = JSON.parse(xhr.responseText);
  85. if (xhr.status === 304 || xhr.status >= 200 && xhr.status < 300) {
  86. resolve({
  87. code: _contants.Constants.UPLOAD_SUCCESS_CODE,
  88. url: obj.url,
  89. msg: 'success'
  90. });
  91. } else {
  92. reject({
  93. code: _contants.Constants.UPLOAD_FAILED_CODE,
  94. url: '',
  95. msg: obj.message
  96. });
  97. }
  98. }
  99. };
  100. });
  101. };
  102. this.postImage = function (md5, fileFactory) {
  103. var _this4 = this;
  104. var self = this;
  105. return new Promise(function (resolve, reject) {
  106. var xhr = new XMLHttpRequest();
  107. xhr.upload.addEventListener("progress", function updateProgress(oEvent) {
  108. if (oEvent.lengthComputable) {
  109. var percentComplete = oEvent.loaded / oEvent.total * 100;
  110. self.emitUpdateProgress(percentComplete);
  111. }
  112. }, false);
  113. xhr.open('POSt', _this4.host + _contants.Constants.IMAGE_MULTIPART + _this4.origin, true);
  114. xhr.withCredentials = true;
  115. xhr.setRequestHeader('Authorization', 'Bearer ' + _this4.token);
  116. xhr.setRequestHeader('X-Upload-File-Size', fileFactory.fileSize);
  117. xhr.setRequestHeader('X-Upload-Chunk-Index', 1 + '');
  118. xhr.setRequestHeader('X-Upload-Chunk-Num', fileFactory.chunkNum);
  119. xhr.setRequestHeader('X-Upload-Chunk-Size', fileFactory.chunkSize);
  120. xhr.setRequestHeader('X-Upload-Offset', fileFactory.offset + '');
  121. xhr.setRequestHeader('X-Upload-File-Md5', md5);
  122. xhr.setRequestHeader('X-Upload-File-Type', fileFactory.fileType);
  123. var fd = new FormData();
  124. fd.append('file', _this4.file);
  125. xhr.send(fd);
  126. xhr.onreadystatechange = function () {
  127. if (xhr.readyState === 4) {
  128. var obj = JSON.parse(xhr.responseText);
  129. if (xhr.status === 304 || xhr.status >= 200 && xhr.status < 300) {
  130. if (obj.status === 1) {
  131. resolve({
  132. code: _contants.Constants.UPLOAD_SUCCESS_CODE,
  133. url: obj.url,
  134. msg: 'success'
  135. });
  136. } else if (obj.status === 0) {
  137. fileFactory.setOffset(obj.msg);
  138. this.getAllResponseHeaders.postImage(md5, fileFactory);
  139. }
  140. } else {
  141. reject({
  142. code: _contants.Constants.UPLOAD_FAILED_CODE,
  143. url: '',
  144. msg: obj.message
  145. });
  146. }
  147. }
  148. };
  149. });
  150. };
  151. }
  152. UploadSdk.prototype = {
  153. onUpdateProgress: function onUpdateProgress(subscriber) {
  154. var isExist = this.handlers.some(function (item) {
  155. return item == subscriber;
  156. });
  157. if (!isExist) {
  158. this.handlers.push(subscriber);
  159. }
  160. return this;
  161. },
  162. emitUpdateProgress: function emitUpdateProgress(data) {
  163. this.handlers.forEach(function (fn) {
  164. fn(data);
  165. });
  166. return this;
  167. }
  168. };