Home Reference Source

src/chat/ChatMessageFile.js

/**
 * Instances of this class represents a file in a chat message. This class must not be
 * directly instantiated. Instances of this class can be obtained using ChatMessage#getData.
 */
export class ChatMessageFile {
	/** @protected */
	constructor(name, url = null) {
		/** @private */
		this.name = name;
		/** @private */
		this.url = url;
		/** @private */
		this.cache = {
			type: null,
		};
		Object.freeze(this);
	}

	/**
	 * Returns the name of the file.
	 * @return {string} Name of the file
	 */
	getName() {
		return this.name;
	}

	/**
	 * Returns the URL of the file.
	 * @return {string} URL of the file
	 */
	getUrl() {
		return this.url;
	}

	/**
	 * Returns the mime-type of the file.
	 * @return {Promise<string>} A promise fulfilled with the mime-type of the file
	 */
	async getType() {
		if (!this.cache.type) {
			const res = await window.fetch(this.url, {
				method: 'HEAD',
			});
			this.cache.type = res.headers.get('content-type');
			Object.freeze(this.cache);
		}
		return this.cache.type;
	}
}