UNPKG

@kaltura-ng/kaltura-client

Version:
219 lines 10.1 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { CancelableAction } from "../utils/cancelable-action"; import { KalturaClientBase } from "./kaltura-client-base"; var KalturaHttpClientBase = (function (_super) { __extends(KalturaHttpClientBase, _super); function KalturaHttpClientBase(config) { var _this = _super.call(this, config) || this; if (!config || !config.endpointUrl) { throw new Error("invalid config, missing endpoint url value"); } _this.endpointUrl = config.endpointUrl; _this.chunkFileSize = config.chunkFileSize; return _this; } KalturaHttpClientBase.prototype._getHeaders = function () { return { "Accept": "application/json", "Content-Type": "application/json" }; }; KalturaHttpClientBase.prototype._chunkUploadSupported = function (request) { // SUPPORTED BY BROWSER? // Check if these features are support by the browser: // - File object type // - Blob object type // - FileList object type // - slicing files var supportedByBrowser = ((typeof (File) !== 'undefined') && (typeof (Blob) !== 'undefined') && (typeof (FileList) !== 'undefined') && (!!Blob.prototype.webkitSlice || !!Blob.prototype.mozSlice || !!Blob.prototype.slice || false)); var supportedByRequest = request.supportChunkUpload(); return supportedByBrowser && supportedByRequest; }; KalturaHttpClientBase.prototype._transmitFileUploadRequest = function (request) { var _this = this; return new CancelableAction(function (resolve, reject) { var uploadedFileSize = !isNaN(request.uploadedFileSize) && isFinite(request.uploadedFileSize) && request.uploadedFileSize > 0 ? request.uploadedFileSize : 0; var data = { enabled: _this._chunkUploadSupported(request), resume: !!uploadedFileSize, finalChunk: false, resumeAt: uploadedFileSize }; var handleChunkUploadError = function (reason) { chunkUploadRequest = null; reject(reason); }; var handleChunkUploadSuccess = function (response) { if (!data.enabled || data.finalChunk) { chunkUploadRequest = null; resolve(response); } else { chunkUploadRequest = _this._chunkUpload(request, data).then(handleChunkUploadSuccess, handleChunkUploadError); } }; var chunkUploadRequest = _this._chunkUpload(request, data).then(handleChunkUploadSuccess, handleChunkUploadError); return function () { if (chunkUploadRequest) { chunkUploadRequest.cancel(); } }; }); }; KalturaHttpClientBase.prototype._getFormData = function (filePropertyName, fileName, fileChunk) { var result = new FormData(); result.append("fileName", fileName); result.append(filePropertyName, fileChunk); return result; }; KalturaHttpClientBase.prototype._chunkUpload = function (request, uploadChunkData) { var _this = this; return new CancelableAction(function (resolve, reject) { var isComplete = false; var parameters = Object.assign({ format: 1 }, request.toRequestObject()); _this._assignDefaultParameters(parameters); var _a = request.getFileInfo(), propertyName = _a.propertyName, file = _a.file; var data = _this._getFormData(propertyName, file.name, file); var fileStart = 0; var uploadSize = null; if (uploadChunkData.enabled) { uploadSize = 5e6; // default if (_this.chunkFileSize) { if (_this.chunkFileSize < 1e6) { console.warn("user requested for invalid upload chunk size '" + _this.chunkFileSize + "'. minimal value 1Mb. using minimal value 1Mb instead"); uploadSize = 1e6; } else { uploadSize = _this.chunkFileSize; console.log("using user requetsed chunk size '" + _this.chunkFileSize + "'"); } } else { console.log("using default chunk size 5Mb"); } uploadChunkData.finalChunk = (file.size - uploadChunkData.resumeAt) <= uploadSize; fileStart = uploadChunkData.resumeAt; var fileEnd = uploadChunkData.finalChunk ? file.size : fileStart + uploadSize; data = _this._getFormData(propertyName, file.name, file.slice(fileStart, fileEnd, file.type)); parameters.resume = uploadChunkData.resume; parameters.resumeAt = uploadChunkData.resumeAt; parameters.finalChunk = uploadChunkData.finalChunk; } else { console.log("chunk upload not supported by browser or by request. Uploading the file as-is"); } // build endpoint var endpointData = { service: parameters.service, action: parameters.action }; delete parameters.service; delete parameters.action; var querystring = _this._buildQuerystring(parameters); var endpoint = _this._createEndpoint(endpointData) + "?" + querystring; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var resp = void 0; try { if (xhr.status === 200) { resp = JSON.parse(xhr.response); } else { resp = new Error(xhr.responseText); } } catch (e) { resp = new Error(xhr.responseText); } if (resp instanceof Error) { reject(resp); } else { if (uploadChunkData.enabled) { if (typeof resp.uploadedFileSize === "undefined" || resp.uploadedFileSize === null) { resp = new Error("uploaded chunk of file failed, expected response with property 'uploadedFileSize'"); } else if (!uploadChunkData.finalChunk) { uploadChunkData.resumeAt = Number(resp.uploadedFileSize); uploadChunkData.resume = true; } } resolve(resp); } } }; var progressCallback = request._getProgressCallback(); if (progressCallback) { xhr.upload.addEventListener("progress", function (e) { if (e.lengthComputable) { var chunkSize = uploadSize; if (uploadChunkData.enabled) { chunkSize = uploadChunkData.finalChunk ? file.size - fileStart : uploadSize; } progressCallback.apply(request, [Math.floor(e.loaded / e.total * chunkSize) + fileStart, file.size]); } else { // Unable to compute progress information since the total size is unknown } }, false); } xhr.open("POST", endpoint); xhr.send(data); return function () { if (!isComplete) { xhr.abort(); isComplete = true; } }; }); }; KalturaHttpClientBase.prototype._createEndpoint = function (parameters) { var endpoint = this.endpointUrl + "/api_v3/service/" + parameters.service; if (parameters.action) { endpoint = endpoint + "/action/" + parameters.action; } return endpoint; }; KalturaHttpClientBase.prototype._transmitRequest = function (request) { var parameters = Object.assign({ format: 1 }, request.toRequestObject()); this._assignDefaultParameters(parameters); // build endpoint var endpoint = this._createEndpoint(parameters); delete parameters.action; delete parameters.service; var headers = this._getHeaders(); return this._createCancelableAction({ endpoint: endpoint, headers: headers, body: parameters }); }; KalturaHttpClientBase.prototype._buildQuerystring = function (data, prefix) { var str = [], p; for (p in data) { if (data.hasOwnProperty(p)) { var k = prefix ? prefix + "[" + p + "]" : p, v = data[p]; str.push((v !== null && typeof v === "object") ? this._buildQuerystring(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v)); } } return str.join("&"); }; return KalturaHttpClientBase; }(KalturaClientBase)); export { KalturaHttpClientBase }; //# sourceMappingURL=kaltura-http-client-base.js.map