UNPKG

@signiant/media-shuttle-sdk

Version:

The SDK for supporting file transfer to and from Media Shuttle

240 lines (239 loc) 12.1 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { CodedError, signiant_private, TransferEventType, } from '@signiant/media-shuttle-sdk-base'; import PlatformTransferEventHandler from './PlatformTransferEventHandler'; import { INVALID_GRANT, REFRESH_TOKEN_EXPIRED } from '../constants/Authentication'; /* * Developer Note: * We are aware that this both PlatformUpload and PlatformDownload share some * common code but due to the current class structure/hierarchy we can't * put that shared code in a common class */ var PlatformDownload = /** @class */ (function (_super) { __extends(PlatformDownload, _super); /** * Coverage seems to think there is a branch here? */ function PlatformDownload(options) { var _this = _super.call(this, options.transferClient) || this; _this._platformService = options.platformService; _this.portalId = options.portalId; _this.serviceId = options.serviceId; _this.accountId = options.accountId; _this.files = options.files; _this._eventListeners = new Map(); _this._plaformTransferEventHandler = new PlatformTransferEventHandler({ platformService: _this._platformService, portalId: _this.portalId, accountId: _this.accountId, serviceId: _this.serviceId, userAgent: navigator.userAgent, }); return _this; } PlatformDownload.prototype.populateTransferConfig = function () { var _a, _b; return __awaiter(this, void 0, void 0, function () { var options, createTransferResponse, files, error_1; return __generator(this, function (_c) { switch (_c.label) { case 0: options = { appVersion: (_a = this._transferClient.clientInfo) === null || _a === void 0 ? void 0 : _a.appVersion, clientId: (_b = this._transferClient.clientInfo) === null || _b === void 0 ? void 0 : _b.clientId, files: this.files, portalId: this.portalId, accountId: this.accountId, serviceId: this.serviceId, }; _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, this._platformService.createTransfer(options)]; case 2: createTransferResponse = _c.sent(); this._transferId = createTransferResponse.transferId; this._transferConfig = createTransferResponse.transferConfig; files = createTransferResponse.files.map(function (_a) { var fileId = _a.fileId, fileName = _a.fileName, filePath = _a.filePath; return { fileId: fileId, fileName: fileName, filePath: filePath, }; }); this._plaformTransferEventHandler.files = files; this._plaformTransferEventHandler.transferId = this._transferId; signiant_private.LogManager.debug('Transfer Config:', this._transferConfig); return [3 /*break*/, 4]; case 3: error_1 = _c.sent(); if (error_1.error && error_1.errorMessage) { throw new CodedError({ code: error_1.error, message: error_1.errorMessage }); } throw error_1; case 4: return [2 /*return*/]; } }); }); }; PlatformDownload.prototype.start = function () { return __awaiter(this, void 0, void 0, function () { var err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); if (!!this._transferState) return [3 /*break*/, 2]; return [4 /*yield*/, this.populateTransferConfig()]; case 1: _a.sent(); this._plaformTransferEventHandler.enableEventDispatch(); _a.label = 2; case 2: return [4 /*yield*/, _super.prototype.start.call(this)]; case 3: _a.sent(); return [3 /*break*/, 5]; case 4: err_1 = _a.sent(); this._plaformTransferEventHandler.disableEventDispatch(); throw err_1; case 5: return [2 /*return*/]; } }); }); }; PlatformDownload.prototype.selectDestinationFolder = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, _super.prototype.selectDestinationFolder.call(this)]; case 1: _a.sent(); this._plaformTransferEventHandler.destinationPath = this.destinationFolder; return [2 /*return*/]; } }); }); }; PlatformDownload.prototype.retry = function () { return __awaiter(this, void 0, void 0, function () { var err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); this._plaformTransferEventHandler.enableEventDispatch(); return [4 /*yield*/, _super.prototype.retry.call(this)]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: err_2 = _a.sent(); this._plaformTransferEventHandler.disableEventDispatch(); throw err_2; case 3: return [2 /*return*/]; } }); }); }; PlatformDownload.prototype.subscribe = function (eventType, listener) { this._eventListeners[eventType] = listener; _super.prototype.subscribe.call(this, eventType, listener); }; PlatformDownload.prototype.notifyTransferError = function (errorCode, errorMessage) { var transferErrorListener = this._eventListeners[TransferEventType.TRANSFER_ERROR]; if (transferErrorListener) { transferErrorListener(TransferEventType.TRANSFER_ERROR, { transfer: this, eventData: { content: { errorCode: errorCode, shortMessage: errorMessage }, }, }); } }; PlatformDownload.prototype.unsubscribe = function (eventType) { delete this._eventListeners[eventType]; return _super.prototype.unsubscribe.call(this, eventType); }; //eslint-disable-next-line @typescript-eslint/no-explicit-any PlatformDownload.prototype.onTransferEvent = function (event) { return __awaiter(this, void 0, void 0, function () { var err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 6]); signiant_private.LogManager.info("in On transfer event, ".concat(event.type, ", ").concat(JSON.stringify(event.eventData))); return [4 /*yield*/, this._plaformTransferEventHandler.handleEvent(event)]; case 1: _a.sent(); return [3 /*break*/, 6]; case 2: err_3 = _a.sent(); signiant_private.LogManager.error('Error while handling the transfer event:', err_3.message); if (!(err_3.errorCode === INVALID_GRANT)) return [3 /*break*/, 4]; this.notifyTransferError(err_3.errorCode, REFRESH_TOKEN_EXPIRED); return [4 /*yield*/, this.cancel()]; case 3: _a.sent(); return [3 /*break*/, 5]; case 4: throw err_3; case 5: return [3 /*break*/, 6]; case 6: return [2 /*return*/]; } }); }); }; return PlatformDownload; }(signiant_private.AbstractDownload)); export default PlatformDownload;