resume.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 { uploadChunk, uploadComplete, initUploadParts } from '../api';
  62. import { QiniuError, QiniuErrorName, QiniuRequestError } from '../errors';
  63. import * as utils from '../utils';
  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, mkFileResponse, localKey, uploadChunks, error_1;
  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 QiniuError(QiniuErrorName.InvalidChunkSize, 'chunkSize must be a positive integer');
  89. }
  90. if (this.config.chunkSize > 1024) {
  91. throw new QiniuError(QiniuErrorName.InvalidChunkSize, 'chunkSize maximum value is 1024');
  92. }
  93. return [4 /*yield*/, this.initBeforeUploadChunks()];
  94. case 1:
  95. _a.sent();
  96. pool = new utils.Pool(function (chunkInfo) { return __awaiter(_this, void 0, void 0, function () {
  97. return __generator(this, function (_a) {
  98. switch (_a.label) {
  99. case 0:
  100. if (this.aborted) {
  101. pool.abort();
  102. throw new Error('pool is aborted');
  103. }
  104. return [4 /*yield*/, this.uploadChunk(chunkInfo)];
  105. case 1:
  106. _a.sent();
  107. return [2 /*return*/];
  108. }
  109. });
  110. }); }, this.config.concurrentRequestLimit);
  111. mkFileResponse = null;
  112. localKey = this.getLocalKey();
  113. uploadChunks = this.chunks.map(function (chunk, index) { return pool.enqueue({ chunk: chunk, index: index }); });
  114. _a.label = 2;
  115. case 2:
  116. _a.trys.push([2, 5, , 6]);
  117. return [4 /*yield*/, Promise.all(uploadChunks)];
  118. case 3:
  119. _a.sent();
  120. return [4 /*yield*/, this.mkFileReq()];
  121. case 4:
  122. mkFileResponse = _a.sent();
  123. return [3 /*break*/, 6];
  124. case 5:
  125. error_1 = _a.sent();
  126. // uploadId 无效,上传参数有误(多由于本地存储信息的 uploadId 失效)
  127. if (error_1 instanceof QiniuRequestError && (error_1.code === 612 || error_1.code === 400)) {
  128. utils.removeLocalFileInfo(localKey, this.logger);
  129. }
  130. throw error_1;
  131. case 6:
  132. // 上传成功,清理本地缓存数据
  133. utils.removeLocalFileInfo(localKey, this.logger);
  134. return [2 /*return*/, mkFileResponse];
  135. }
  136. });
  137. });
  138. };
  139. Resume.prototype.uploadChunk = function (chunkInfo) {
  140. return __awaiter(this, void 0, void 0, function () {
  141. var index, chunk, cachedInfo, shouldCheckMD5, reuseSaved, md5, onProgress, requestOptions, response;
  142. var _this = this;
  143. return __generator(this, function (_a) {
  144. switch (_a.label) {
  145. case 0:
  146. index = chunkInfo.index, chunk = chunkInfo.chunk;
  147. cachedInfo = this.cachedUploadedList[index];
  148. this.logger.info("upload part " + index + ", cache:", cachedInfo);
  149. shouldCheckMD5 = this.config.checkByMD5;
  150. reuseSaved = function () {
  151. _this.usedCacheList[index] = true;
  152. _this.updateChunkProgress(chunk.size, index);
  153. _this.uploadedList[index] = cachedInfo;
  154. _this.updateLocalCache();
  155. };
  156. // FIXME: 至少判断一下 size
  157. if (cachedInfo && !shouldCheckMD5) {
  158. reuseSaved();
  159. return [2 /*return*/];
  160. }
  161. return [4 /*yield*/, utils.computeMd5(chunk)];
  162. case 1:
  163. md5 = _a.sent();
  164. this.logger.info('computed part md5.', md5);
  165. if (cachedInfo && md5 === cachedInfo.md5) {
  166. reuseSaved();
  167. return [2 /*return*/];
  168. }
  169. // 没有使用缓存设置标记为 false
  170. this.usedCacheList[index] = false;
  171. onProgress = function (data) {
  172. _this.updateChunkProgress(data.loaded, index);
  173. };
  174. requestOptions = {
  175. body: chunk,
  176. md5: this.config.checkByServer ? md5 : undefined,
  177. onProgress: onProgress,
  178. onCreate: function (xhr) { return _this.addXhr(xhr); }
  179. };
  180. this.logger.info("part " + index + " start uploading.");
  181. return [4 /*yield*/, uploadChunk(this.token, this.key, chunkInfo.index + 1, this.getUploadInfo(), requestOptions)];
  182. case 2:
  183. response = _a.sent();
  184. this.logger.info("part " + index + " upload completed.");
  185. // 在某些浏览器环境下,xhr 的 progress 事件无法被触发,progress 为 null,这里在每次分片上传完成后都手动更新下 progress
  186. onProgress({
  187. loaded: chunk.size,
  188. total: chunk.size
  189. });
  190. this.uploadedList[index] = {
  191. etag: response.data.etag,
  192. md5: response.data.md5,
  193. size: chunk.size
  194. };
  195. this.updateLocalCache();
  196. return [2 /*return*/];
  197. }
  198. });
  199. });
  200. };
  201. Resume.prototype.mkFileReq = function () {
  202. return __awaiter(this, void 0, void 0, function () {
  203. var data, result;
  204. var _this = this;
  205. return __generator(this, function (_a) {
  206. switch (_a.label) {
  207. case 0:
  208. data = __assign(__assign(__assign({ parts: this.uploadedList.map(function (value, index) { return ({
  209. etag: value.etag,
  210. // 接口要求 index 需要从 1 开始,所以需要整体 + 1
  211. partNumber: index + 1
  212. }); }), 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 });
  213. this.logger.info('parts upload completed, make file.', data);
  214. return [4 /*yield*/, uploadComplete(this.token, this.key, this.getUploadInfo(), {
  215. onCreate: function (xhr) { return _this.addXhr(xhr); },
  216. body: JSON.stringify(data)
  217. })];
  218. case 1:
  219. result = _a.sent();
  220. this.logger.info('finish Resume Progress.');
  221. this.updateMkFileProgress(1);
  222. return [2 /*return*/, result];
  223. }
  224. });
  225. });
  226. };
  227. Resume.prototype.initBeforeUploadChunks = function () {
  228. return __awaiter(this, void 0, void 0, function () {
  229. var cachedInfo, res, infoMessage;
  230. return __generator(this, function (_a) {
  231. switch (_a.label) {
  232. case 0:
  233. this.uploadedList = [];
  234. this.usedCacheList = [];
  235. cachedInfo = utils.getLocalFileInfo(this.getLocalKey(), this.logger);
  236. if (!!cachedInfo) return [3 /*break*/, 2];
  237. this.logger.info('init upload parts from api.');
  238. return [4 /*yield*/, initUploadParts(this.token, this.bucketName, this.key, this.uploadHost.getUrl())];
  239. case 1:
  240. res = _a.sent();
  241. this.logger.info("initd upload parts of id: " + res.data.uploadId + ".");
  242. this.uploadId = res.data.uploadId;
  243. this.cachedUploadedList = [];
  244. return [3 /*break*/, 3];
  245. case 2:
  246. infoMessage = [
  247. 'resume upload parts from local cache,',
  248. "total " + cachedInfo.data.length + " part,",
  249. "id is " + cachedInfo.id + "."
  250. ];
  251. this.logger.info(infoMessage.join(' '));
  252. this.cachedUploadedList = cachedInfo.data;
  253. this.uploadId = cachedInfo.id;
  254. _a.label = 3;
  255. case 3:
  256. this.chunks = utils.getChunks(this.file, this.config.chunkSize);
  257. this.loaded = {
  258. mkFileProgress: 0,
  259. chunks: this.chunks.map(function (_) { return 0; })
  260. };
  261. this.notifyResumeProgress();
  262. return [2 /*return*/];
  263. }
  264. });
  265. });
  266. };
  267. Resume.prototype.getUploadInfo = function () {
  268. return {
  269. id: this.uploadId,
  270. url: this.uploadHost.getUrl()
  271. };
  272. };
  273. Resume.prototype.getLocalKey = function () {
  274. return utils.createLocalKey(this.file.name, this.key, this.file.size);
  275. };
  276. Resume.prototype.updateLocalCache = function () {
  277. utils.setLocalFileInfo(this.getLocalKey(), {
  278. id: this.uploadId,
  279. data: this.uploadedList
  280. }, this.logger);
  281. };
  282. Resume.prototype.updateChunkProgress = function (loaded, index) {
  283. this.loaded.chunks[index] = loaded;
  284. this.notifyResumeProgress();
  285. };
  286. Resume.prototype.updateMkFileProgress = function (progress) {
  287. this.loaded.mkFileProgress = progress;
  288. this.notifyResumeProgress();
  289. };
  290. Resume.prototype.notifyResumeProgress = function () {
  291. var _this = this;
  292. this.progress = {
  293. total: this.getProgressInfoItem(utils.sum(this.loaded.chunks) + this.loaded.mkFileProgress,
  294. // FIXME: 不准确的 fileSize
  295. this.file.size + 1 // 防止在 complete 未调用的时候进度显示 100%
  296. ),
  297. chunks: this.chunks.map(function (chunk, index) {
  298. var fromCache = _this.usedCacheList[index];
  299. return _this.getProgressInfoItem(_this.loaded.chunks[index], chunk.size, fromCache);
  300. }),
  301. uploadInfo: {
  302. id: this.uploadId,
  303. url: this.uploadHost.getUrl()
  304. }
  305. };
  306. this.onData(this.progress);
  307. };
  308. return Resume;
  309. }(Base));
  310. export default Resume;
  311. //# sourceMappingURL=resume.js.map