2024-08-18 01:22:17 +00:00
|
|
|
"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");
|
2024-10-27 01:47:13 +00:00
|
|
|
const path_1 = require("path");
|
|
|
|
const os_1 = require("os");
|
|
|
|
const promises_1 = require("fs/promises");
|
|
|
|
const shell_1 = require("../shell");
|
2024-08-18 01:22:17 +00:00
|
|
|
class Image {
|
|
|
|
constructor() {
|
2024-10-27 01:47:13 +00:00
|
|
|
this.prefix = 'fm_thumbs';
|
2024-08-18 01:22:17 +00:00
|
|
|
this.thumbnailCache = null;
|
|
|
|
this.thumbnailHash = null;
|
|
|
|
this.blankCache = null;
|
|
|
|
this.blankHash = null;
|
2024-10-27 01:47:13 +00:00
|
|
|
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
|
|
|
|
];
|
2024-11-04 20:13:59 +00:00
|
|
|
const shell = new shell_1.Shell(args, null, null, null, null, true);
|
2024-10-27 01:47:13 +00:00
|
|
|
try {
|
|
|
|
await shell.execute();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//
|
|
|
|
}
|
2024-08-18 01:22:17 +00:00
|
|
|
}
|
|
|
|
async thumbnail(path, width, height) {
|
|
|
|
const hash = hash_1.Hashes.stringHash(`${path},${width},${height}`);
|
2024-10-27 01:47:13 +00:00
|
|
|
let newPath = null;
|
2024-08-18 01:22:17 +00:00
|
|
|
if (hash !== this.thumbnailHash) {
|
2024-10-27 01:47:13 +00:00
|
|
|
if ((0, path_1.extname)(path).toLowerCase() === '.dpx') {
|
|
|
|
try {
|
|
|
|
newPath = await this.mktemp('png');
|
|
|
|
await this.dpx2png(path, newPath);
|
|
|
|
path = newPath;
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 01:22:17 +00:00
|
|
|
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;
|
2024-10-27 01:47:13 +00:00
|
|
|
if (newPath !== null) {
|
|
|
|
try {
|
|
|
|
await (0, promises_1.unlink)(newPath);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
2024-08-18 01:22:17 +00:00
|
|
|
}
|
|
|
|
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
|