resume.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  26. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  27. return new (P || (P = Promise))(function (resolve, reject) {
  28. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  29. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  30. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  31. step((generator = generator.apply(thisArg, _arguments || [])).next());
  32. });
  33. };
  34. var __generator = (this && this.__generator) || function (thisArg, body) {
  35. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  36. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  37. function verb(n) { return function (v) { return step([n, v]); }; }
  38. function step(op) {
  39. if (f) throw new TypeError("Generator is already executing.");
  40. while (_) try {
  41. 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;
  42. if (y = 0, t) op = [op[0] & 2, t.value];
  43. switch (op[0]) {
  44. case 0: case 1: t = op; break;
  45. case 4: _.label++; return { value: op[1], done: false };
  46. case 5: _.label++; y = op[1]; op = [0]; continue;
  47. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  48. default:
  49. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  50. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  51. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  52. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  53. if (t[2]) _.ops.pop();
  54. _.trys.pop(); continue;
  55. }
  56. op = body.call(thisArg, _);
  57. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  58. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  59. }
  60. };
  61. import * as utils from '../utils';
  62. import { Pool } from '../pool';
  63. import { uploadChunk, uploadComplete, initUploadParts } from '../api';
  64. import Base from './base';
  65. /** 是否为正整数 */
  66. function isPositiveInteger(n) {
  67. var re = /^[1-9]\d*$/;
  68. return re.test(String(n));
  69. }
  70. var Resume = /** @class */ (function (_super) {
  71. __extends(Resume, _super);
  72. function Resume() {
  73. return _super !== null && _super.apply(this, arguments) || this;
  74. }
  75. /**
  76. * @returns {Promise<ResponseSuccess<any>>}
  77. * @description 实现了 Base 的 run 接口,处理具体的分片上传事务,并抛出过程中的异常。
  78. */
  79. Resume.prototype.run = function () {
  80. return __awaiter(this, void 0, void 0, function () {
  81. var pool, localKey, uploadChunks, result;
  82. var _this = this;
  83. return __generator(this, function (_a) {
  84. switch (_a.label) {
  85. case 0:
  86. this.logger.info('start run Resume.');
  87. if (!this.config.chunkSize || !isPositiveInteger(this.config.chunkSize)) {
  88. throw new Error('chunkSize must be a positive integer.');
  89. }
  90. if (this.config.chunkSize > 1024) {
  91. throw new Error('chunkSize maximum value is 1024.');
  92. }
  93. return [4 /*yield*/, this.initBeforeUploadChunks()];
  94. case 1:
  95. _a.sent();
  96. pool = new Pool(function (chunkInfo) { return _this.uploadChunk(chunkInfo); }, this.config.concurrentRequestLimit);
  97. localKey = this.getLocalKey();
  98. uploadChunks = this.chunks.map(function (chunk, index) { return pool.enqueue({ chunk: chunk, index: index }); });
  99. result = Promise.all(uploadChunks).then(function () { return _this.mkFileReq(); });
  100. result.then(function () {
  101. try {
  102. utils.removeLocalFileInfo(localKey);
  103. }
  104. catch (error) {
  105. _this.logger.error(error);
  106. }
  107. }, function (err) {
  108. _this.logger.error('uploadChunks failed.', err);
  109. // uploadId 无效,上传参数有误(多由于本地存储信息的 uploadId 失效
  110. if (err.code === 612 || err.code === 400) {
  111. try {
  112. utils.removeLocalFileInfo(localKey);
  113. }
  114. catch (error) {
  115. _this.logger.error(error);
  116. }
  117. }
  118. });
  119. return [2 /*return*/, result];
  120. }
  121. });
  122. });
  123. };
  124. Resume.prototype.uploadChunk = function (chunkInfo) {
  125. return __awaiter(this, void 0, void 0, function () {
  126. var index, chunk, info, shouldCheckMD5, reuseSaved, md5, onProgress, requestOptions, response;
  127. var _this = this;
  128. return __generator(this, function (_a) {
  129. switch (_a.label) {
  130. case 0:
  131. index = chunkInfo.index, chunk = chunkInfo.chunk;
  132. info = this.uploadedList[index];
  133. this.logger.info("upload part " + index + ".", info);
  134. shouldCheckMD5 = this.config.checkByMD5;
  135. reuseSaved = function () {
  136. _this.updateChunkProgress(chunk.size, index);
  137. };
  138. if (info && !shouldCheckMD5) {
  139. reuseSaved();
  140. return [2 /*return*/];
  141. }
  142. return [4 /*yield*/, utils.computeMd5(chunk)];
  143. case 1:
  144. md5 = _a.sent();
  145. this.logger.info("computed part md5.", md5);
  146. if (info && md5 === info.md5) {
  147. reuseSaved();
  148. return [2 /*return*/];
  149. }
  150. onProgress = function (data) {
  151. _this.updateChunkProgress(data.loaded, index);
  152. };
  153. requestOptions = {
  154. body: chunk,
  155. onProgress: onProgress,
  156. onCreate: function (xhr) { return _this.addXhr(xhr); }
  157. };
  158. this.logger.info("part " + index + " start uploading.");
  159. return [4 /*yield*/, uploadChunk(this.token, this.key, chunkInfo.index + 1, this.getUploadInfo(), requestOptions)];
  160. case 2:
  161. response = _a.sent();
  162. this.logger.info("part " + index + " upload completed.");
  163. // 在某些浏览器环境下,xhr 的 progress 事件无法被触发,progress 为 null,这里在每次分片上传完成后都手动更新下 progress
  164. onProgress({
  165. loaded: chunk.size,
  166. total: chunk.size
  167. });
  168. this.uploadedList[index] = {
  169. etag: response.data.etag,
  170. md5: response.data.md5,
  171. size: chunk.size
  172. };
  173. try {
  174. utils.setLocalFileInfo(this.getLocalKey(), {
  175. id: this.uploadId,
  176. data: this.uploadedList
  177. });
  178. }
  179. catch (error) {
  180. this.logger.info("set part " + index + " cache failed.", error);
  181. }
  182. return [2 /*return*/];
  183. }
  184. });
  185. });
  186. };
  187. Resume.prototype.mkFileReq = function () {
  188. return __awaiter(this, void 0, void 0, function () {
  189. var data, result;
  190. var _this = this;
  191. return __generator(this, function (_a) {
  192. switch (_a.label) {
  193. case 0:
  194. data = __assign(__assign(__assign({ parts: this.uploadedList.map(function (value, index) { return ({
  195. etag: value.etag,
  196. partNumber: index + 1
  197. }); }), fname: this.putExtra.fname }, this.putExtra.mimeType && { mimeType: this.putExtra.mimeType }), this.putExtra.customVars && { customVars: this.putExtra.customVars }), this.putExtra.metadata && { metadata: this.putExtra.metadata });
  198. this.logger.info('parts upload completed, make file.', data);
  199. return [4 /*yield*/, uploadComplete(this.token, this.key, this.getUploadInfo(), {
  200. onCreate: function (xhr) { return _this.addXhr(xhr); },
  201. body: JSON.stringify(data)
  202. })];
  203. case 1:
  204. result = _a.sent();
  205. this.logger.info('finishResumeProgress.');
  206. this.updateMkFileProgress(1);
  207. return [2 /*return*/, result];
  208. }
  209. });
  210. });
  211. };
  212. Resume.prototype.initBeforeUploadChunks = function () {
  213. return __awaiter(this, void 0, void 0, function () {
  214. var localInfo, res, infoMessage;
  215. return __generator(this, function (_a) {
  216. switch (_a.label) {
  217. case 0:
  218. localInfo = null;
  219. try {
  220. localInfo = utils.getLocalFileInfo(this.getLocalKey());
  221. }
  222. catch (error) {
  223. this.logger.warn(error);
  224. }
  225. if (!!localInfo) return [3 /*break*/, 2];
  226. this.logger.info('resume upload parts from api.');
  227. return [4 /*yield*/, initUploadParts(this.token, this.bucket, this.key, this.uploadUrl)];
  228. case 1:
  229. res = _a.sent();
  230. this.logger.info("resume upload parts of id: " + res.data.uploadId + ".");
  231. this.uploadId = res.data.uploadId;
  232. this.uploadedList = [];
  233. return [3 /*break*/, 3];
  234. case 2:
  235. infoMessage = [
  236. 'resume upload parts from local cache',
  237. "total " + localInfo.data.length + " part",
  238. "id is " + localInfo.id + "."
  239. ];
  240. this.logger.info(infoMessage.join(', '));
  241. this.uploadedList = localInfo.data;
  242. this.uploadId = localInfo.id;
  243. _a.label = 3;
  244. case 3:
  245. this.chunks = utils.getChunks(this.file, this.config.chunkSize);
  246. this.loaded = {
  247. mkFileProgress: 0,
  248. chunks: this.chunks.map(function (_) { return 0; })
  249. };
  250. this.notifyResumeProgress();
  251. return [2 /*return*/];
  252. }
  253. });
  254. });
  255. };
  256. Resume.prototype.getUploadInfo = function () {
  257. return {
  258. id: this.uploadId,
  259. url: this.uploadUrl
  260. };
  261. };
  262. Resume.prototype.getLocalKey = function () {
  263. return utils.createLocalKey(this.file.name, this.key, this.file.size);
  264. };
  265. Resume.prototype.updateChunkProgress = function (loaded, index) {
  266. this.loaded.chunks[index] = loaded;
  267. this.notifyResumeProgress();
  268. };
  269. Resume.prototype.updateMkFileProgress = function (progress) {
  270. this.loaded.mkFileProgress = progress;
  271. this.notifyResumeProgress();
  272. };
  273. Resume.prototype.notifyResumeProgress = function () {
  274. var _this = this;
  275. this.progress = {
  276. total: this.getProgressInfoItem(utils.sum(this.loaded.chunks) + this.loaded.mkFileProgress, this.file.size + 1 // 防止在 complete 未调用的时候进度显示 100%
  277. ),
  278. chunks: this.chunks.map(function (chunk, index) { return (_this.getProgressInfoItem(_this.loaded.chunks[index], chunk.size)); }),
  279. uploadInfo: {
  280. id: this.uploadId,
  281. url: this.uploadUrl
  282. }
  283. };
  284. this.onData(this.progress);
  285. };
  286. return Resume;
  287. }(Base));
  288. export default Resume;
  289. //# sourceMappingURL=resume.js.map