fix(core): update
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartjimp.plugins.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import * as pluginsTyped from './plugins.typed.js';
|
||||
|
||||
export interface IAssetVariation {
|
||||
format?: 'avif' | 'webp' | 'png';
|
||||
@@ -6,13 +7,22 @@ export interface IAssetVariation {
|
||||
height?: number;
|
||||
}
|
||||
|
||||
export interface ISmartJimpOptions {
|
||||
mode: 'sharp' | 'jimp';
|
||||
}
|
||||
|
||||
export class SmartJimp {
|
||||
public levelCache = new plugins.levelcache.LevelCache({
|
||||
cacheId: 'mastercache',
|
||||
maxMemoryStorageInMB: 100,
|
||||
maxDiskStorageInMB: 5000,
|
||||
maxDiskStorageInMB: 5000,
|
||||
});
|
||||
|
||||
public options: ISmartJimpOptions;
|
||||
constructor(optionsArg: ISmartJimpOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* get a key that is unique for a wanted asset variation
|
||||
*/
|
||||
@@ -23,51 +33,94 @@ export class SmartJimp {
|
||||
) {
|
||||
return `${sourceTypeArg}_${sourceIdArg}_${
|
||||
assetVariationArg
|
||||
? `${assetVariationArg.width || 'auto' }x${assetVariationArg.height || 'auto'}`
|
||||
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
||||
: 'original'
|
||||
}`;
|
||||
}
|
||||
|
||||
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg?: IAssetVariation) {
|
||||
if (!assetVariationArg) {
|
||||
return assetBufferArg;
|
||||
sharpMod: typeof pluginsTyped.sharpType.default;
|
||||
public async getSharpMod(): Promise<
|
||||
typeof pluginsTyped.sharpType.default
|
||||
> {
|
||||
if (!this.sharpMod) {
|
||||
this.sharpMod = (await import('sharp')).default;
|
||||
}
|
||||
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 this.sharpMod;
|
||||
}
|
||||
|
||||
jimpMod: typeof pluginsTyped.jimpType;
|
||||
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
|
||||
if (!this.jimpMod) {
|
||||
this.jimpMod = await import('jimp');
|
||||
}
|
||||
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);
|
||||
}
|
||||
switch (assetVariationArg.format) {
|
||||
case 'png':
|
||||
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
|
||||
default:
|
||||
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
|
||||
}
|
||||
}
|
||||
return sharpImage.toBuffer();
|
||||
}
|
||||
|
||||
public async getFromSmartfile(
|
||||
smartfileArg: plugins.smartfile.SmartFile,
|
||||
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);
|
||||
if (existingCacheEntry) {
|
||||
return existingCacheEntry.contents;
|
||||
} else {
|
||||
const computedAssetBuffer = await this.computeAssetVariation(smartfileArg.contentBuffer, wantedDimensionsArg);
|
||||
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
|
||||
contents: computedAssetBuffer,
|
||||
ttl: 600000
|
||||
}));
|
||||
const computedAssetBuffer = await this.computeAssetVariation(
|
||||
smartfileArg.contentBuffer,
|
||||
wantedDimensionsArg
|
||||
);
|
||||
this.levelCache.storeCacheEntryByKey(
|
||||
cacheKey,
|
||||
new plugins.levelcache.CacheEntry({
|
||||
contents: computedAssetBuffer,
|
||||
ttl: 600000,
|
||||
})
|
||||
);
|
||||
return computedAssetBuffer;
|
||||
}
|
||||
}
|
||||
|
||||
public async createAvifImageFromBuffer(bufferArg: Buffer) {
|
||||
const sharpImage = plugins.sharp(bufferArg);
|
||||
const sharp = await this.getSharpMod();
|
||||
const sharpImage = sharp(bufferArg);
|
||||
const result = await sharpImage.avif().toBuffer();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user