direct.js 7.3 KB

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