fix(core): update

This commit is contained in:
2023-01-09 18:17:33 +01:00
parent 0ca656306a
commit 746447ec44
17 changed files with 5087 additions and 2269 deletions

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @pushrocks/commitinfo
*/
export const commitinfo = {
name: '@pushrocks/smartjimp',
version: '1.0.4',
description: 'a tool fr working with images in TypeScript'
}

View File

@ -1 +1 @@
export * from './smartjimp.classes.smartjimp';
export * from './smartjimp.classes.smartjimp.js';

View File

@ -1,5 +1,89 @@
import * as plugins from './smartjimp.plugins';
import * as plugins from './smartjimp.plugins.js';
export interface IDimensions {
width?: number;
height?: number;
}
export class SmartJimp {
public levelCache = new plugins.levelcache.LevelCache({
cacheId: 'mastercache',
maxMemoryStorageInMB: 100,
maxDiskStorageInMB: 5000,
});
/**
* get a key that is unique for a wanted asset variation
*/
private getCacheKey(
sourceTypeArg: 'path' | 'url' | 'smartfile',
sourceIdArg: string,
wantedDimensionsArg?: IDimensions
) {
return `${sourceTypeArg}_${sourceIdArg}_${
wantedDimensionsArg
? `${wantedDimensionsArg.width || 'auto' }x${wantedDimensionsArg.height || 'auto'}`
: 'original'
}`;
}
private async computeAssetVariation(assetBuffer: Buffer, wantedDimensions?: IDimensions) {
if (!wantedDimensions) {
return assetBuffer;
}
let sharpImage = plugins.sharp(assetBuffer);
sharpImage = sharpImage.resize(wantedDimensions.width, wantedDimensions.height);
const result = await sharpImage.resize(wantedDimensions.width, wantedDimensions.height).jpeg().toBuffer();
return result;
}
public async getFromPath(pathArg: string, wantedDimensionsArg?: IDimensions) {
const cacheKey = this.getCacheKey('path', pathArg, wantedDimensionsArg);
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
if (existingCacheEntry) {
return existingCacheEntry.contents;
} else {
const originalAssetSmartfile = await plugins.smartfile.Smartfile.fromFilePath(pathArg);
const computedAssetBuffer = await this.computeAssetVariation(originalAssetSmartfile.contentBuffer, wantedDimensionsArg);
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
contents: computedAssetBuffer,
ttl: 600000
}));
return computedAssetBuffer;
}
}
public async getFromUrl(urlArg: string, wantedDimensionsArg?: IDimensions) {
const cacheKey = this.getCacheKey('url', urlArg, wantedDimensionsArg);
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
if (existingCacheEntry) {
return existingCacheEntry.contents;
} else {
const originalAssetBuffer = (await plugins.smartrequest.getBinary(urlArg)).body;
const computedAssetBuffer = await this.computeAssetVariation(originalAssetBuffer, wantedDimensionsArg);
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
contents: computedAssetBuffer,
ttl: 600000
}));
return computedAssetBuffer;
}
}
public async getFromSmartfile(
smartfileArg: plugins.smartfile.Smartfile,
wantedDimensionsArg?: IDimensions
) {
const cacheKey = this.getCacheKey('url', 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
}));
return computedAssetBuffer;
}
}
}

4
ts/smartjimp.paths.ts Normal file
View File

@ -0,0 +1,4 @@
import * as plugins from './smartjimp.plugins.js';
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
export const nogitDir = plugins.path.join(packageDir, '.nogit/')

View File

@ -1,6 +1,18 @@
// third party scope
import jimp from 'jimp';
// node native
import * as path from 'path';
export {
jimp
}
export { path };
// @pushrocks scope
import * as levelcache from '@pushrocks/levelcache';
import * as smartfile from '@pushrocks/smartfile';
import * as smarthash from '@pushrocks/smarthash';
import * as smartpath from '@pushrocks/smartpath';
import * as smartrequest from '@pushrocks/smartrequest';
export { levelcache, smartpath, smarthash, smartfile, smartrequest };
// third party scope
import sharp from 'sharp';
export { sharp };