UNPKG

camstreamerlib

Version:

Helper library for CamStreamer ACAP applications.

44 lines (43 loc) 1.45 kB
import * as fs from 'fs/promises'; export default class ResourceManager { co; imgFileNames = {}; fontFileNames = {}; images = {}; fonts = {}; constructor(co) { this.co = co; } registerImage(moniker, fileName) { this.imgFileNames[moniker] = process.env.INSTALL_PATH + '/images/' + fileName; } registerFont(moniker, fileName) { this.fontFileNames[moniker] = process.env.INSTALL_PATH + '/fonts/' + fileName; } async image(moniker) { if (this.images[moniker] !== undefined) { return this.images[moniker]; } if (this.imgFileNames[moniker] !== undefined) { const imgData = await fs.readFile(this.imgFileNames[moniker]); this.images[moniker] = await this.co.uploadImageData(imgData); return this.images[moniker]; } throw new Error('Error! Unknown image requested!'); } async font(moniker) { if (this.fonts[moniker] !== undefined) { return this.fonts[moniker]; } if (this.fontFileNames[moniker] !== undefined) { const fontData = await fs.readFile(this.fontFileNames[moniker]); this.fonts[moniker] = await this.co.uploadFontData(fontData); return this.fonts[moniker]; } throw new Error('Error! Unknown font requested!'); } clear() { this.images = {}; this.fonts = {}; } }