2023-12-03 23:09:48 +01:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
import * as pluginsTyped from './plugins.typed.js';
|
2023-01-09 18:17:33 +01:00
|
|
|
|
2023-12-03 18:10:35 +01:00
|
|
|
export interface IAssetVariation {
|
2025-08-02 15:18:04 +00:00
|
|
|
format?: 'avif' | 'webp' | 'png' | 'jpeg';
|
2023-01-09 18:17:33 +01:00
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2023-12-03 23:30:58 +01:00
|
|
|
invert?: boolean;
|
2025-08-02 15:18:04 +00:00
|
|
|
progressive?: boolean;
|
2025-08-02 17:17:52 +00:00
|
|
|
quality?: number;
|
2023-01-09 18:17:33 +01:00
|
|
|
}
|
2020-02-02 20:10:42 +00:00
|
|
|
|
2023-12-03 23:09:48 +01:00
|
|
|
export interface ISmartJimpOptions {
|
|
|
|
mode: 'sharp' | 'jimp';
|
|
|
|
}
|
|
|
|
|
2020-02-02 20:10:42 +00:00
|
|
|
export class SmartJimp {
|
2023-01-09 18:17:33 +01:00
|
|
|
public levelCache = new plugins.levelcache.LevelCache({
|
|
|
|
cacheId: 'mastercache',
|
|
|
|
maxMemoryStorageInMB: 100,
|
2023-12-03 23:09:48 +01:00
|
|
|
maxDiskStorageInMB: 5000,
|
2023-01-09 18:17:33 +01:00
|
|
|
});
|
|
|
|
|
2023-12-03 23:09:48 +01:00
|
|
|
public options: ISmartJimpOptions;
|
|
|
|
constructor(optionsArg: ISmartJimpOptions) {
|
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
|
2023-01-09 18:17:33 +01:00
|
|
|
/**
|
|
|
|
* get a key that is unique for a wanted asset variation
|
|
|
|
*/
|
|
|
|
private getCacheKey(
|
2023-11-24 20:08:48 +01:00
|
|
|
sourceTypeArg: 'streamfile' | 'smartfile',
|
2023-01-09 18:17:33 +01:00
|
|
|
sourceIdArg: string,
|
2023-12-03 18:10:35 +01:00
|
|
|
assetVariationArg?: IAssetVariation
|
2023-01-09 18:17:33 +01:00
|
|
|
) {
|
|
|
|
return `${sourceTypeArg}_${sourceIdArg}_${
|
2023-12-03 18:10:35 +01:00
|
|
|
assetVariationArg
|
2023-12-03 23:09:48 +01:00
|
|
|
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
2023-01-09 18:17:33 +01:00
|
|
|
: 'original'
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
2023-12-03 23:09:48 +01:00
|
|
|
sharpMod: typeof pluginsTyped.sharpType.default;
|
|
|
|
public async getSharpMod(): Promise<
|
|
|
|
typeof pluginsTyped.sharpType.default
|
|
|
|
> {
|
|
|
|
if (!this.sharpMod) {
|
|
|
|
this.sharpMod = (await import('sharp')).default;
|
|
|
|
}
|
|
|
|
return this.sharpMod;
|
|
|
|
}
|
|
|
|
|
|
|
|
jimpMod: typeof pluginsTyped.jimpType;
|
2025-08-02 15:18:04 +00:00
|
|
|
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType> {
|
2023-12-03 23:09:48 +01:00
|
|
|
if (!this.jimpMod) {
|
|
|
|
this.jimpMod = await import('jimp');
|
2023-01-09 18:17:33 +01:00
|
|
|
}
|
2025-08-02 15:18:04 +00:00
|
|
|
return this.jimpMod;
|
2023-12-03 23:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg: IAssetVariation) {
|
|
|
|
if (this.options.mode === 'sharp') {
|
|
|
|
const sharp = await this.getSharpMod();
|
|
|
|
if (!assetVariationArg) {
|
|
|
|
return assetBufferArg;
|
|
|
|
}
|
|
|
|
let sharpImage = sharp(assetBufferArg);
|
|
|
|
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
|
|
|
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
2023-12-03 23:32:47 +01:00
|
|
|
if (assetVariationArg.invert) {
|
|
|
|
// TODO: implement invert
|
|
|
|
}
|
2023-12-03 23:09:48 +01:00
|
|
|
switch (assetVariationArg.format) {
|
|
|
|
case 'avif':
|
2025-08-02 17:17:52 +00:00
|
|
|
const avifOptions: any = {};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
avifOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
sharpImage = resultResize.avif(avifOptions);
|
2025-08-02 15:18:04 +00:00
|
|
|
break;
|
2023-12-03 23:09:48 +01:00
|
|
|
case 'webp':
|
2025-08-02 17:17:52 +00:00
|
|
|
const webpOptions: any = {};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
webpOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
sharpImage = resultResize.webp(webpOptions);
|
2025-08-02 15:18:04 +00:00
|
|
|
break;
|
2023-12-03 23:09:48 +01:00
|
|
|
case 'png':
|
2025-08-02 17:17:52 +00:00
|
|
|
const pngOptions: any = {};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
pngOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
sharpImage = resultResize.png(pngOptions);
|
2025-08-02 15:18:04 +00:00
|
|
|
break;
|
|
|
|
case 'jpeg':
|
2025-08-02 17:17:52 +00:00
|
|
|
const jpegOptions: any = {
|
2025-08-02 15:18:04 +00:00
|
|
|
progressive: assetVariationArg.progressive || false
|
2025-08-02 17:17:52 +00:00
|
|
|
};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
jpegOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
sharpImage = resultResize.jpeg(jpegOptions);
|
2025-08-02 15:18:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Default to JPEG
|
2025-08-02 17:17:52 +00:00
|
|
|
const defaultJpegOptions: any = {
|
2025-08-02 15:18:04 +00:00
|
|
|
progressive: assetVariationArg.progressive || false
|
2025-08-02 17:17:52 +00:00
|
|
|
};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
defaultJpegOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
sharpImage = resultResize.jpeg(defaultJpegOptions);
|
2023-12-03 23:09:48 +01:00
|
|
|
}
|
|
|
|
return sharpImage.toBuffer();
|
|
|
|
} else if (this.options.mode === 'jimp') {
|
2025-08-02 15:18:04 +00:00
|
|
|
const jimpMod = await this.getJimpMod();
|
|
|
|
let jimpImage = await jimpMod.Jimp.read(assetBufferArg);
|
2023-12-03 23:09:48 +01:00
|
|
|
if (assetVariationArg.width || assetVariationArg.height) {
|
2025-08-02 15:18:04 +00:00
|
|
|
const resizeOptions: any = {};
|
|
|
|
if (assetVariationArg.width) resizeOptions.w = assetVariationArg.width;
|
|
|
|
if (assetVariationArg.height) resizeOptions.h = assetVariationArg.height;
|
|
|
|
jimpImage.resize(resizeOptions);
|
2023-12-03 23:09:48 +01:00
|
|
|
}
|
2023-12-03 23:30:58 +01:00
|
|
|
if (assetVariationArg.invert) {
|
2025-08-02 15:18:04 +00:00
|
|
|
jimpImage.invert();
|
2023-12-03 23:30:58 +01:00
|
|
|
}
|
2025-08-02 15:18:04 +00:00
|
|
|
// Note: Jimp does not support progressive JPEG encoding
|
|
|
|
// Progressive option is ignored in jimp mode
|
2025-08-02 17:17:52 +00:00
|
|
|
const jpegOptions: any = {};
|
|
|
|
if (assetVariationArg.quality !== undefined) {
|
|
|
|
jpegOptions.quality = assetVariationArg.quality;
|
|
|
|
}
|
|
|
|
|
2023-12-03 23:09:48 +01:00
|
|
|
switch (assetVariationArg.format) {
|
|
|
|
case 'png':
|
2025-08-02 15:18:04 +00:00
|
|
|
return await jimpImage.getBuffer("image/png");
|
|
|
|
case 'webp':
|
|
|
|
case 'avif':
|
2025-08-02 17:17:52 +00:00
|
|
|
console.log(`Jimp doesn't support ${assetVariationArg.format}, falling back to JPEG`);
|
|
|
|
// Fall through to JPEG
|
|
|
|
case 'jpeg':
|
2023-12-03 23:09:48 +01:00
|
|
|
default:
|
2025-08-02 15:18:04 +00:00
|
|
|
// Default to JPEG
|
2025-08-02 17:17:52 +00:00
|
|
|
return await jimpImage.getBuffer("image/jpeg", jpegOptions);
|
2023-12-03 23:09:48 +01:00
|
|
|
}
|
2023-12-03 18:10:35 +01:00
|
|
|
}
|
2023-01-09 18:17:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getFromSmartfile(
|
2023-11-24 20:08:48 +01:00
|
|
|
smartfileArg: plugins.smartfile.SmartFile,
|
2023-12-03 18:10:35 +01:00
|
|
|
wantedDimensionsArg?: IAssetVariation
|
2023-01-09 18:17:33 +01:00
|
|
|
) {
|
2023-12-03 23:09:48 +01:00
|
|
|
const cacheKey = this.getCacheKey(
|
|
|
|
'smartfile',
|
|
|
|
await smartfileArg.getHash(),
|
|
|
|
wantedDimensionsArg
|
|
|
|
);
|
2023-01-09 18:17:33 +01:00
|
|
|
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
|
|
|
|
if (existingCacheEntry) {
|
|
|
|
return existingCacheEntry.contents;
|
|
|
|
} else {
|
2023-12-03 23:09:48 +01:00
|
|
|
const computedAssetBuffer = await this.computeAssetVariation(
|
|
|
|
smartfileArg.contentBuffer,
|
|
|
|
wantedDimensionsArg
|
|
|
|
);
|
|
|
|
this.levelCache.storeCacheEntryByKey(
|
|
|
|
cacheKey,
|
|
|
|
new plugins.levelcache.CacheEntry({
|
|
|
|
contents: computedAssetBuffer,
|
|
|
|
ttl: 600000,
|
|
|
|
})
|
|
|
|
);
|
2023-01-09 18:17:33 +01:00
|
|
|
return computedAssetBuffer;
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 20:08:48 +01:00
|
|
|
|
|
|
|
public async createAvifImageFromBuffer(bufferArg: Buffer) {
|
2023-12-03 23:09:48 +01:00
|
|
|
const sharp = await this.getSharpMod();
|
|
|
|
const sharpImage = sharp(bufferArg);
|
2023-11-24 20:08:48 +01:00
|
|
|
const result = await sharpImage.avif().toBuffer();
|
|
|
|
return result;
|
|
|
|
}
|
2020-02-02 20:10:42 +00:00
|
|
|
}
|