Home Reference Source

src/file-sharing/FileUpload.js

/**
 * Provides access to methods for uploading a file.
 * A file transfer object is obtained by calling the
 * {@link FileSharingManager#createFileTransfer} method.
 *
 * # Events
 * - `status` status : {@link FileTransferStatus}, error? : Error -
 *    Emitted every time the status of the file transfer changes.
 * - `progress` value : number - Emitted every time the progress changes
 *
 */
export class FileUpload {
	/** @protected */
	static valueOf({id, name, type, size, status, bytesSent = 0, url = null} = {}) {
		return new FileUpload(id, name, type, size, status, bytesSent, url);
	}

	/** @private */
	constructor(id, name, type, size, status, bytesSent, url) {
		/** @private */
		this.id = id;
		/** @private */
		this.name = name;
		/** @private */
		this.type = type;
		/** @private */
		this.size = size;
		/** @private */
		this.status = status;
		/** @private */
		this.bytesSent = bytesSent;
		/** @private */
		this.url = url;
	}

	/** @protected */
	with({bytesSent = this.bytesSent, status = this.status, url = this.url} = {}) {
		return new FileUpload(this.id, this.name, this.type, this.size, status, bytesSent, url);
	}

	/**
	 * @return {string}
	 */
	getId() {
		return this.id;
	}

	/**
	 * @return {string}
	 */
	getName() {
		return this.name;
	}

	/**
	 * @return {string}
	 */
	getType() {
		return this.type;
	}

	/**
	 * @return {number}
	 */
	getSize() {
		return this.size;
	}

	/**
	 * @return {FileUploadStatus}
	 */
	getStatus() {
		return this.status;
	}

	/**
	 * @param {FileUploadStatus} statuses
	 * @return {boolean}
	 */
	hasStatus(...statuses) {
		return statuses.includes(this.status);
	}

	/**
	 * @return {number}
	 */
	getBytesSent() {
		return this.bytesSent;
	}

	/**
	 * @return {string}
	 */
	getUrl() {
		return this.url;
	}

	/**
	 * @return {number} File transfer progress.
	 */
	getProgress() {
		return this.bytesSent / this.size;
	}
}