"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Image = void 0; const sharp_1 = __importDefault(require("sharp")); const hash_1 = require("../hash"); const path_1 = require("path"); const os_1 = require("os"); const promises_1 = require("fs/promises"); const shell_1 = require("../shell"); class Image { constructor() { this.prefix = 'fm_thumbs'; this.thumbnailCache = null; this.thumbnailHash = null; this.blankCache = null; this.blankHash = null; this.tmp = null; if (this.tmp === null) { this.tmp = (0, os_1.tmpdir)(); } } async mktemp(ext = '.png') { const randomString = Math.random().toString(36).slice(2); const tempPath = (0, path_1.join)(this.tmp, this.prefix, randomString + `.${ext}`); try { await (0, promises_1.mkdir)((0, path_1.join)(this.tmp, this.prefix)); } catch (err) { // } return tempPath; } async dpx2png(input, output) { let dpx = null; const args = [ 'convert', input, '-colorspace', 'RGB', '-depth', '8', output ]; const shell = new shell_1.Shell(args, null, null, null, null, true); try { await shell.execute(); } catch (err) { // } } async thumbnail(path, width, height) { const hash = hash_1.Hashes.stringHash(`${path},${width},${height}`); let newPath = null; if (hash !== this.thumbnailHash) { if ((0, path_1.extname)(path).toLowerCase() === '.dpx') { try { newPath = await this.mktemp('png'); await this.dpx2png(path, newPath); path = newPath; } catch (err) { // } } const options = { width, height, fit: sharp_1.default.fit.fill }; this.thumbnailCache = await (0, sharp_1.default)(path).resize(options).jpeg().toBuffer(); this.thumbnailHash = hash; if (newPath !== null) { try { await (0, promises_1.unlink)(newPath); } catch (err) { // } } } return this.thumbnailCache; } async blank(width, height) { const hash = hash_1.Hashes.stringHash(`${width},${height}`); if (hash !== this.blankHash) { const options = { create: { width, height, channels: 3, background: { r: 125, g: 125, b: 125 } } }; this.blankCache = await (0, sharp_1.default)(options).jpeg().toBuffer(); this.blankHash = hash; } return this.blankCache; } } exports.Image = Image; module.exports = { Image }; //# sourceMappingURL=index.js.map