direct.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports["default"] = void 0;
  8. var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
  9. var _iterator = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol/iterator"));
  10. var _symbol = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol"));
  11. var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
  12. var _create = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/create"));
  13. var _setPrototypeOf = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/set-prototype-of"));
  14. var _crc = require("../utils/crc32");
  15. var _api = require("../api");
  16. var _base = _interopRequireDefault(require("./base"));
  17. var __extends = void 0 && (void 0).__extends || function () {
  18. var _extendStatics = function extendStatics(d, b) {
  19. _extendStatics = _setPrototypeOf["default"] || {
  20. __proto__: []
  21. } instanceof Array && function (d, b) {
  22. d.__proto__ = b;
  23. } || function (d, b) {
  24. for (var p in b) {
  25. if (b.hasOwnProperty(p)) d[p] = b[p];
  26. }
  27. };
  28. return _extendStatics(d, b);
  29. };
  30. return function (d, b) {
  31. _extendStatics(d, b);
  32. function __() {
  33. this.constructor = d;
  34. }
  35. d.prototype = b === null ? (0, _create["default"])(b) : (__.prototype = b.prototype, new __());
  36. };
  37. }();
  38. var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
  39. function adopt(value) {
  40. return value instanceof P ? value : new P(function (resolve) {
  41. resolve(value);
  42. });
  43. }
  44. return new (P || (P = _promise["default"]))(function (resolve, reject) {
  45. function fulfilled(value) {
  46. try {
  47. step(generator.next(value));
  48. } catch (e) {
  49. reject(e);
  50. }
  51. }
  52. function rejected(value) {
  53. try {
  54. step(generator["throw"](value));
  55. } catch (e) {
  56. reject(e);
  57. }
  58. }
  59. function step(result) {
  60. result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
  61. }
  62. step((generator = generator.apply(thisArg, _arguments || [])).next());
  63. });
  64. };
  65. var __generator = void 0 && (void 0).__generator || function (thisArg, body) {
  66. var _ = {
  67. label: 0,
  68. sent: function sent() {
  69. if (t[0] & 1) throw t[1];
  70. return t[1];
  71. },
  72. trys: [],
  73. ops: []
  74. },
  75. f,
  76. y,
  77. t,
  78. g;
  79. return g = {
  80. next: verb(0),
  81. "throw": verb(1),
  82. "return": verb(2)
  83. }, typeof _symbol["default"] === "function" && (g[_iterator["default"]] = function () {
  84. return this;
  85. }), g;
  86. function verb(n) {
  87. return function (v) {
  88. return step([n, v]);
  89. };
  90. }
  91. function step(op) {
  92. if (f) throw new TypeError("Generator is already executing.");
  93. while (_) {
  94. try {
  95. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  96. if (y = 0, t) op = [op[0] & 2, t.value];
  97. switch (op[0]) {
  98. case 0:
  99. case 1:
  100. t = op;
  101. break;
  102. case 4:
  103. _.label++;
  104. return {
  105. value: op[1],
  106. done: false
  107. };
  108. case 5:
  109. _.label++;
  110. y = op[1];
  111. op = [0];
  112. continue;
  113. case 7:
  114. op = _.ops.pop();
  115. _.trys.pop();
  116. continue;
  117. default:
  118. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
  119. _ = 0;
  120. continue;
  121. }
  122. if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
  123. _.label = op[1];
  124. break;
  125. }
  126. if (op[0] === 6 && _.label < t[1]) {
  127. _.label = t[1];
  128. t = op;
  129. break;
  130. }
  131. if (t && _.label < t[2]) {
  132. _.label = t[2];
  133. _.ops.push(op);
  134. break;
  135. }
  136. if (t[2]) _.ops.pop();
  137. _.trys.pop();
  138. continue;
  139. }
  140. op = body.call(thisArg, _);
  141. } catch (e) {
  142. op = [6, e];
  143. y = 0;
  144. } finally {
  145. f = t = 0;
  146. }
  147. }
  148. if (op[0] & 5) throw op[1];
  149. return {
  150. value: op[0] ? op[1] : void 0,
  151. done: true
  152. };
  153. }
  154. };
  155. var Direct =
  156. /** @class */
  157. function (_super) {
  158. __extends(Direct, _super);
  159. function Direct() {
  160. return _super !== null && _super.apply(this, arguments) || this;
  161. }
  162. Direct.prototype.run = function () {
  163. return __awaiter(this, void 0, void 0, function () {
  164. var formData, crcSign, customVars_1, result;
  165. var _this = this;
  166. return __generator(this, function (_a) {
  167. switch (_a.label) {
  168. case 0:
  169. this.logger.info('start run Direct.');
  170. formData = new FormData();
  171. formData.append('file', this.file);
  172. formData.append('token', this.token);
  173. if (this.key != null) {
  174. formData.append('key', this.key);
  175. }
  176. formData.append('fname', this.putExtra.fname);
  177. if (!this.config.checkByServer) return [3
  178. /*break*/
  179. , 2];
  180. return [4
  181. /*yield*/
  182. , _crc.CRC32.file(this.file)];
  183. case 1:
  184. crcSign = _a.sent();
  185. formData.append('crc32', crcSign.toString());
  186. _a.label = 2;
  187. case 2:
  188. if (this.putExtra.customVars) {
  189. this.logger.info('init customVars.');
  190. customVars_1 = this.putExtra.customVars;
  191. (0, _keys["default"])(customVars_1).forEach(function (key) {
  192. return formData.append(key, customVars_1[key].toString());
  193. });
  194. this.logger.info('customVars inited.');
  195. }
  196. this.logger.info('formData inited.');
  197. return [4
  198. /*yield*/
  199. , (0, _api.direct)(this.uploadHost.getUrl(), formData, {
  200. onProgress: function onProgress(data) {
  201. _this.updateDirectProgress(data.loaded, data.total);
  202. },
  203. onCreate: function onCreate(xhr) {
  204. return _this.addXhr(xhr);
  205. }
  206. })];
  207. case 3:
  208. result = _a.sent();
  209. this.logger.info('Direct progress finish.');
  210. this.finishDirectProgress();
  211. return [2
  212. /*return*/
  213. , result];
  214. }
  215. });
  216. });
  217. };
  218. Direct.prototype.updateDirectProgress = function (loaded, total) {
  219. // 当请求未完成时可能进度会达到100,所以total + 1来防止这种情况出现
  220. this.progress = {
  221. total: this.getProgressInfoItem(loaded, total + 1)
  222. };
  223. this.onData(this.progress);
  224. };
  225. Direct.prototype.finishDirectProgress = function () {
  226. // 在某些浏览器环境下,xhr 的 progress 事件无法被触发,progress 为 null,这里 fake 下
  227. if (!this.progress) {
  228. this.logger.warn('progress is null.');
  229. this.progress = {
  230. total: this.getProgressInfoItem(this.file.size, this.file.size)
  231. };
  232. this.onData(this.progress);
  233. return;
  234. }
  235. var total = this.progress.total;
  236. this.progress = {
  237. total: this.getProgressInfoItem(total.loaded + 1, total.size)
  238. };
  239. this.onData(this.progress);
  240. };
  241. return Direct;
  242. }(_base["default"]);
  243. var _default = Direct;
  244. exports["default"] = _default;