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