UNPKG

@twilio/mcs-client

Version:

Twilio Media Content Service client library

141 lines (132 loc) 5.05 kB
/* @license Copyright (c) 2018, Twilio, Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 'use strict'; var global = typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}; Object.defineProperty(exports, '__esModule', { value: true }); var cancellablePromise = require('./cancellable-promise.js'); /** * @classdesc A Media represents a metadata information for the media upload * @property {String} sid - The server-assigned unique identifier for Media * @property {String} serviceSid - Service instance id which Media belongs/uploaded to * @property {Date} dateCreated - When the Media was created * @property {Date} dateUpdated - When the Media was updated * @property {Number} size - Size of media, bytes * @property {String} contentType - content type of media * @property {String} fileName - file name, if present, null otherwise * @property {MediaCategory} category - attachment category */ class Media { constructor(config, network, data) { this.config = config; this.network = network; this._update(data); } get sid() { return this.state.sid; } get serviceSid() { return this.state.serviceSid; } get dateCreated() { return this.state.dateCreated; } get dateUpdated() { return this.state.dateUpdated; } get contentType() { return this.state.contentType; } get size() { return this.state.size; } /** @deprecated Use filename instead */ get fileName() { return this.state.filename; } get filename() { return this.state.filename; } get category() { return this.state.category; } /** * Returns direct content URL to uploaded binary. This URL will expire after some time. * This function gets a new URL every time, preventing it from expiring but putting additional load on backend. * See getCachedContentUrl() for a function that reduces the amount of network requests. * * It is reasonable to build your own refresh logic upon these two functions: as soon as URL returned * by getCachedContentUrl() returns 40x status you should call getContentUrl() to refresh it. */ getContentUrl() { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const request = this.network.get(`${this.config.mediaUrl}/${this.sid}`); onCancel(() => request.cancel()); try { const response = await request; this._update(response.body); resolve(this.state.contentDirectUrl); } catch (e) { reject(e); } }); } _update(data) { var _a, _b, _c, _d; this.state = { sid: data.sid, serviceSid: data.service_sid, channelSid: data.channel_sid, messageSid: data.message_sid, dateCreated: data.date_created ? new Date(data.date_created) : null, dateUploadUpdated: data.date_upload_updated ? new Date(data.date_upload_updated) : null, dateUpdated: data.date_updated ? new Date(data.date_updated) : null, size: data.size, contentType: data.content_type, author: data.author, url: data.url, contentUrl: data.links.content, contentDirectUrl: (_a = data.links.content_direct_temporary) !== null && _a !== void 0 ? _a : null, filename: (_b = data.filename) !== null && _b !== void 0 ? _b : null, category: (_c = data.category) !== null && _c !== void 0 ? _c : "media", isMultipartUpstream: (_d = data.is_multipart_upstream) !== null && _d !== void 0 ? _d : false, }; } /** * @internal * This payload is compatible with Conversations' media object _state(). */ _state() { var _a; return { sid: this.state.sid, category: this.state.category, filename: (_a = this.state.filename) !== null && _a !== void 0 ? _a : null, contentType: this.state.contentType, size: this.state.size, }; } } exports.Media = Media; //# sourceMappingURL=media.js.map