mirror of
https://github.com/enricoros/big-AGI.git
synced 2026-05-10 21:50:14 -07:00
addDBImage: more Blob usage with removal of resizeBase64ImageIfNeeded
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { Is } from '~/common/util/pwaUtils';
|
||||
import { convert_Base64WithMimeType_To_Blob, convert_Blob_To_Base64 } from '~/common/util/blobUtils';
|
||||
import { resizeBase64ImageIfNeeded } from '~/common/util/imageUtils';
|
||||
import { imageBlobResizeIfNeeded } from '~/common/util/imageUtils';
|
||||
|
||||
import { _addDBAsset, gcDBAssetsByScope, getDBAsset } from './dblobs.db';
|
||||
import { _createAssetObject, DBlobAssetId, DBlobAssetType, DBlobDBContextId, DBlobDBScopeId, DBlobImageAsset, DBlobMimeType } from './dblobs.types';
|
||||
@@ -8,6 +8,7 @@ import { _createAssetObject, DBlobAssetId, DBlobAssetType, DBlobDBContextId, DBl
|
||||
|
||||
// configuration
|
||||
const THUMBNAIL_ENCODING_MIMETYPE = !Is.Browser.Safari ? DBlobMimeType.IMG_WEBP : DBlobMimeType.IMG_JPEG;
|
||||
const THUMBNAIL_ENCODING_LOSSY_QUALITY = 0.9; // 90% quality for JPEG/WEBP thumbnails
|
||||
|
||||
|
||||
export async function addDBImageAsset(
|
||||
@@ -38,35 +39,38 @@ export async function addDBImageAsset(
|
||||
image.metadata,
|
||||
);
|
||||
|
||||
// add to the DB
|
||||
return _addDBImageAsset(imageAsset, 'global', scopeId);
|
||||
}
|
||||
|
||||
async function _addDBImageAsset(imageAsset: DBlobImageAsset, contextId: 'global', scopeId: DBlobDBScopeId): Promise<DBlobAssetId> {
|
||||
|
||||
// Auto-Thumbnail: when adding an image, generate a thumbnail-256 cache level
|
||||
if (!imageAsset.cache?.thumb256) {
|
||||
try {
|
||||
// create a thumbnail-256 from the image
|
||||
const resizedDataForCache = await imageBlobResizeIfNeeded(
|
||||
imageBlob,
|
||||
'thumbnail-256',
|
||||
THUMBNAIL_ENCODING_MIMETYPE,
|
||||
THUMBNAIL_ENCODING_LOSSY_QUALITY,
|
||||
);
|
||||
|
||||
// create a thumbnail-256 from the image
|
||||
const resizedDataForCache = await resizeBase64ImageIfNeeded(
|
||||
imageAsset.data.mimeType,
|
||||
imageAsset.data.base64,
|
||||
'thumbnail-256',
|
||||
THUMBNAIL_ENCODING_MIMETYPE,
|
||||
0.9,
|
||||
).catch((error: any) => console.error('addDBAsset: Error resizing image', error));
|
||||
// set the cached data
|
||||
if (resizedDataForCache) {
|
||||
const thumbBase64Data = await convert_Blob_To_Base64(resizedDataForCache.blob, 'addDBImageAsset.thumb256');
|
||||
imageAsset.cache = {
|
||||
...imageAsset.cache, // ensure we don't overwrite existing cache levels
|
||||
thumb256: {
|
||||
base64: thumbBase64Data,
|
||||
mimeType: THUMBNAIL_ENCODING_MIMETYPE,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// set the cached data
|
||||
if (resizedDataForCache) {
|
||||
imageAsset.cache.thumb256 = {
|
||||
base64: resizedDataForCache.base64,
|
||||
mimeType: THUMBNAIL_ENCODING_MIMETYPE,
|
||||
};
|
||||
} catch (thumbnailError) {
|
||||
console.warn('[DEV] addDBImageAsset: Error creating thumbnail-256', thumbnailError);
|
||||
// ignore error, this is not critical
|
||||
}
|
||||
}
|
||||
|
||||
// DB add
|
||||
return _addDBAsset<typeof imageAsset>(imageAsset, contextId, scopeId);
|
||||
return _addDBAsset<typeof imageAsset>(imageAsset, 'global', scopeId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user