fix(core): update

This commit is contained in:
Philipp Kunz 2023-12-03 18:10:35 +01:00
parent 2d83ebac4d
commit 703d21c426
2 changed files with 22 additions and 13 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartjimp', name: '@push.rocks/smartjimp',
version: '1.0.10', version: '1.0.11',
description: 'a tool fr working with images in TypeScript' description: 'a tool fr working with images in TypeScript'
} }

View File

@ -1,6 +1,7 @@
import * as plugins from './smartjimp.plugins.js'; import * as plugins from './smartjimp.plugins.js';
export interface IDimensions { export interface IAssetVariation {
format?: 'avif' | 'webp' | 'png';
width?: number; width?: number;
height?: number; height?: number;
} }
@ -18,28 +19,36 @@ export class SmartJimp {
private getCacheKey( private getCacheKey(
sourceTypeArg: 'streamfile' | 'smartfile', sourceTypeArg: 'streamfile' | 'smartfile',
sourceIdArg: string, sourceIdArg: string,
wantedDimensionsArg?: IDimensions assetVariationArg?: IAssetVariation
) { ) {
return `${sourceTypeArg}_${sourceIdArg}_${ return `${sourceTypeArg}_${sourceIdArg}_${
wantedDimensionsArg assetVariationArg
? `${wantedDimensionsArg.width || 'auto' }x${wantedDimensionsArg.height || 'auto'}` ? `${assetVariationArg.width || 'auto' }x${assetVariationArg.height || 'auto'}`
: 'original' : 'original'
}`; }`;
} }
private async computeAssetVariation(assetBuffer: Buffer, wantedDimensions?: IDimensions) { public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg?: IAssetVariation) {
if (!wantedDimensions) { if (!assetVariationArg) {
return assetBuffer; return assetBufferArg;
} }
let sharpImage = plugins.sharp(assetBuffer); let sharpImage = plugins.sharp(assetBufferArg);
sharpImage = sharpImage.resize(wantedDimensions.width, wantedDimensions.height); sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
const result = await sharpImage.resize(wantedDimensions.width, wantedDimensions.height).avif().toBuffer(); const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
return result; switch (assetVariationArg.format) {
case 'avif':
sharpImage = resultResize.avif();
case 'webp':
sharpImage = resultResize.webp();
case 'png':
sharpImage = resultResize.png();
}
return sharpImage.toBuffer();
} }
public async getFromSmartfile( public async getFromSmartfile(
smartfileArg: plugins.smartfile.SmartFile, smartfileArg: plugins.smartfile.SmartFile,
wantedDimensionsArg?: IDimensions wantedDimensionsArg?: IAssetVariation
) { ) {
const cacheKey = this.getCacheKey('smartfile', await smartfileArg.getHash(), wantedDimensionsArg); const cacheKey = this.getCacheKey('smartfile', await smartfileArg.getHash(), wantedDimensionsArg);
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey); const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);