direct.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  15. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  16. return new (P || (P = Promise))(function (resolve, reject) {
  17. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  18. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  19. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  20. step((generator = generator.apply(thisArg, _arguments || [])).next());
  21. });
  22. };
  23. var __generator = (this && this.__generator) || function (thisArg, body) {
  24. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  25. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  26. function verb(n) { return function (v) { return step([n, v]); }; }
  27. function step(op) {
  28. if (f) throw new TypeError("Generator is already executing.");
  29. while (_) try {
  30. 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;
  31. if (y = 0, t) op = [op[0] & 2, t.value];
  32. switch (op[0]) {
  33. case 0: case 1: t = op; break;
  34. case 4: _.label++; return { value: op[1], done: false };
  35. case 5: _.label++; y = op[1]; op = [0]; continue;
  36. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  37. default:
  38. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  39. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  40. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  41. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  42. if (t[2]) _.ops.pop();
  43. _.trys.pop(); continue;
  44. }
  45. op = body.call(thisArg, _);
  46. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  47. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  48. }
  49. };
  50. import { CRC32 } from '../utils/crc32';
  51. import { direct } from '../api';
  52. import Base from './base';
  53. var Direct = /** @class */ (function (_super) {
  54. __extends(Direct, _super);
  55. function Direct() {
  56. return _super !== null && _super.apply(this, arguments) || this;
  57. }
  58. Direct.prototype.run = function () {
  59. return __awaiter(this, void 0, void 0, function () {
  60. var formData, crcSign, customVars_1, metadata_1, result;
  61. var _this = this;
  62. return __generator(this, function (_a) {
  63. switch (_a.label) {
  64. case 0:
  65. this.logger.info('start run Direct.');
  66. formData = new FormData();
  67. formData.append('file', this.file);
  68. formData.append('token', this.token);
  69. if (this.key != null) {
  70. formData.append('key', this.key);
  71. }
  72. formData.append('fname', this.putExtra.fname);
  73. if (!this.config.checkByServer) return [3 /*break*/, 2];
  74. return [4 /*yield*/, CRC32.file(this.file)];
  75. case 1:
  76. crcSign = _a.sent();
  77. formData.append('crc32', crcSign.toString());
  78. _a.label = 2;
  79. case 2:
  80. if (this.putExtra.customVars) {
  81. this.logger.info('init customVars.');
  82. customVars_1 = this.putExtra.customVars;
  83. Object.keys(customVars_1).forEach(function (key) { return formData.append(key, customVars_1[key].toString()); });
  84. this.logger.info('customVars inited.');
  85. }
  86. if (this.putExtra.metadata) {
  87. this.logger.info('init metadata.');
  88. metadata_1 = this.putExtra.metadata;
  89. Object.keys(metadata_1).forEach(function (key) { return formData.append(key, metadata_1[key].toString()); });
  90. }
  91. this.logger.info('formData inited.');
  92. return [4 /*yield*/, direct(this.uploadHost.getUrl(), formData, {
  93. onProgress: function (data) {
  94. _this.updateDirectProgress(data.loaded, data.total);
  95. },
  96. onCreate: function (xhr) { return _this.addXhr(xhr); }
  97. })];
  98. case 3:
  99. result = _a.sent();
  100. this.logger.info('Direct progress finish.');
  101. this.finishDirectProgress();
  102. return [2 /*return*/, result];
  103. }
  104. });
  105. });
  106. };
  107. Direct.prototype.updateDirectProgress = function (loaded, total) {
  108. // 当请求未完成时可能进度会达到100,所以total + 1来防止这种情况出现
  109. this.progress = { total: this.getProgressInfoItem(loaded, total + 1) };
  110. this.onData(this.progress);
  111. };
  112. Direct.prototype.finishDirectProgress = function () {
  113. // 在某些浏览器环境下,xhr 的 progress 事件无法被触发,progress 为 null,这里 fake 下
  114. if (!this.progress) {
  115. this.logger.warn('progress is null.');
  116. this.progress = { total: this.getProgressInfoItem(this.file.size, this.file.size) };
  117. this.onData(this.progress);
  118. return;
  119. }
  120. var total = this.progress.total;
  121. this.progress = { total: this.getProgressInfoItem(total.loaded + 1, total.size) };
  122. this.onData(this.progress);
  123. };
  124. return Direct;
  125. }(Base));
  126. export default Direct;
  127. //# sourceMappingURL=direct.js.map