2023-12-03 22:09:48 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
import * as pluginsTyped from './plugins.typed.js';
|
2023-01-09 17:17:33 +00:00
|
|
|
|
2023-12-03 17:10:35 +00:00
|
|
|
export interface IAssetVariation {
|
|
|
|
format?: 'avif' | 'webp' | 'png';
|
2023-01-09 17:17:33 +00:00
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2023-12-03 22:30:58 +00:00
|
|
|
invert?: boolean;
|
2023-01-09 17:17:33 +00:00
|
|
|
}
|
2020-02-02 20:10:42 +00:00
|
|
|
|
2023-12-03 22:09:48 +00:00
|
|
|
export interface ISmartJimpOptions {
|
|
|
|
mode: 'sharp' | 'jimp';
|
|
|
|
}
|
|
|
|
|
2020-02-02 20:10:42 +00:00
|
|
|
export class SmartJimp {
|
2023-01-09 17:17:33 +00:00
|
|
|
public levelCache = new plugins.levelcache.LevelCache({
|
|
|
|
cacheId: 'mastercache',
|
|
|
|
maxMemoryStorageInMB: 100,
|
2023-12-03 22:09:48 +00:00
|
|
|
maxDiskStorageInMB: 5000,
|
2023-01-09 17:17:33 +00:00
|
|
|
});
|
|
|
|
|
2023-12-03 22:09:48 +00:00
|
|
|
public options: ISmartJimpOptions;
|
|
|
|
constructor(optionsArg: ISmartJimpOptions) {
|
|
|
|
this.options = optionsArg;
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:17:33 +00:00
|
|
|
/**
|
|
|
|
* get a key that is unique for a wanted asset variation
|
|
|
|
*/
|
|
|
|
private getCacheKey(
|
2023-11-24 19:08:48 +00:00
|
|
|
sourceTypeArg: 'streamfile' | 'smartfile',
|
2023-01-09 17:17:33 +00:00
|
|
|
sourceIdArg: string,
|
2023-12-03 17:10:35 +00:00
|
|
|
assetVariationArg?: IAssetVariation
|
2023-01-09 17:17:33 +00:00
|
|
|
) {
|
|
|
|
return `${sourceTypeArg}_${sourceIdArg}_${
|
2023-12-03 17:10:35 +00:00
|
|
|
assetVariationArg
|
2023-12-03 22:09:48 +00:00
|
|
|
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
2023-01-09 17:17:33 +00:00
|
|
|
: 'original'
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
2023-12-03 22:09:48 +00: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;
|
|
|
|
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
|
|
|
|
if (!this.jimpMod) {
|
|
|
|
this.jimpMod = await import('jimp');
|
2023-01-09 17:17:33 +00:00
|
|
|
}
|
2023-12-03 22:09:48 +00:00
|
|
|
return this.jimpMod.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
switch (assetVariationArg.format) {
|
|
|
|
case 'avif':
|
|
|
|
sharpImage = resultResize.avif();
|
|
|
|
case 'webp':
|
|
|
|
sharpImage = resultResize.webp();
|
|
|
|
case 'png':
|
|
|
|
sharpImage = resultResize.png();
|
|
|
|
}
|
|
|
|
return sharpImage.toBuffer();
|
|
|
|
} else if (this.options.mode === 'jimp') {
|
|
|
|
const jimp = await this.getJimpMod();
|
|
|
|
let jimpImage = await jimp.read(assetBufferArg);
|
|
|
|
if (assetVariationArg.width || assetVariationArg.height) {
|
|
|
|
jimpImage = jimpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
|
|
|
}
|
2023-12-03 22:30:58 +00:00
|
|
|
if (assetVariationArg.invert) {
|
|
|
|
jimpImage = jimpImage.invert();
|
|
|
|
}
|
2023-12-03 22:09:48 +00:00
|
|
|
switch (assetVariationArg.format) {
|
|
|
|
case 'png':
|
|
|
|
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
|
|
|
|
default:
|
|
|
|
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
|
|
|
|
}
|
2023-12-03 17:10:35 +00:00
|
|
|
}
|
2023-01-09 17:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getFromSmartfile(
|
2023-11-24 19:08:48 +00:00
|
|
|
smartfileArg: plugins.smartfile.SmartFile,
|
2023-12-03 17:10:35 +00:00
|
|
|
wantedDimensionsArg?: IAssetVariation
|
2023-01-09 17:17:33 +00:00
|
|
|
) {
|
2023-12-03 22:09:48 +00:00
|
|
|
const cacheKey = this.getCacheKey(
|
|
|
|
'smartfile',
|
|
|
|
await smartfileArg.getHash(),
|
|
|
|
wantedDimensionsArg
|
|
|
|
);
|
2023-01-09 17:17:33 +00:00
|
|
|
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
|
|
|
|
if (existingCacheEntry) {
|
|
|
|
return existingCacheEntry.contents;
|
|
|
|
} else {
|
2023-12-03 22:09:48 +00:00
|
|
|
const computedAssetBuffer = await this.computeAssetVariation(
|
|
|
|
smartfileArg.contentBuffer,
|
|
|
|
wantedDimensionsArg
|
|
|
|
);
|
|
|
|
this.levelCache.storeCacheEntryByKey(
|
|
|
|
cacheKey,
|
|
|
|
new plugins.levelcache.CacheEntry({
|
|
|
|
contents: computedAssetBuffer,
|
|
|
|
ttl: 600000,
|
|
|
|
})
|
|
|
|
);
|
2023-01-09 17:17:33 +00:00
|
|
|
return computedAssetBuffer;
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 19:08:48 +00:00
|
|
|
|
|
|
|
public async createAvifImageFromBuffer(bufferArg: Buffer) {
|
2023-12-03 22:09:48 +00:00
|
|
|
const sharp = await this.getSharpMod();
|
|
|
|
const sharpImage = sharp(bufferArg);
|
2023-11-24 19:08:48 +00:00
|
|
|
const result = await sharpImage.avif().toBuffer();
|
|
|
|
return result;
|
|
|
|
}
|
2020-02-02 20:10:42 +00:00
|
|
|
}
|