u
This commit is contained in:
@@ -11,13 +11,19 @@ const log = createLogger('import');
|
|||||||
// #region SkeleHash Computation
|
// #region SkeleHash Computation
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes SkeleHash base62 hash for an image buffer.
|
* Computes SkeleHash base62 hash for an image buffer or base64 string.
|
||||||
*
|
*
|
||||||
* @param {Buffer} buffer
|
* @param {Buffer|string} buffer
|
||||||
* @returns {Promise<{base62: string, width: number, height: number, hashLength: number}|null>}
|
* @returns {Promise<{base62: string, width: number, height: number, hashLength: number}|null>}
|
||||||
*/
|
*/
|
||||||
export async function computeSkeleHash(buffer) {
|
export async function computeSkeleHash(buffer) {
|
||||||
const { data, info } = await sharp(buffer)
|
if (!buffer) return null;
|
||||||
|
let input = buffer;
|
||||||
|
if (typeof input === 'string') {
|
||||||
|
const base64Data = input.includes(',') ? input.split(',')[1] : input;
|
||||||
|
input = Buffer.from(base64Data, 'base64');
|
||||||
|
}
|
||||||
|
const { data, info } = await sharp(input)
|
||||||
.rotate()
|
.rotate()
|
||||||
.resize({ width: 200, height: 200, fit: 'inside' })
|
.resize({ width: 200, height: 200, fit: 'inside' })
|
||||||
.ensureAlpha()
|
.ensureAlpha()
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { computeSkeleHash } from '../services/companyImport/imageHasher.js';
|
import { computeSkeleHash } from '../services/companyImport/imageHasher.js';
|
||||||
|
import { createLogger } from './logger.js';
|
||||||
|
|
||||||
|
const log = createLogger('attachment');
|
||||||
const skeleCache = new Map();
|
const skeleCache = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,11 +20,15 @@ export async function formatAttachment(file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!skeledash && file.thumbavif) {
|
if (!skeledash && file.thumbavif) {
|
||||||
|
try {
|
||||||
const result = await computeSkeleHash(file.thumbavif);
|
const result = await computeSkeleHash(file.thumbavif);
|
||||||
skeledash = result.base62;
|
skeledash = result?.base62 ?? null;
|
||||||
if (file.hash && skeledash) {
|
if (file.hash && skeledash) {
|
||||||
skeleCache.set(file.hash, skeledash);
|
skeleCache.set(file.hash, skeledash);
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
log.warn(`Failed to compute skelehash for attachment ${file.hash || file.name || file.id}:`, err?.message || err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const { thumbavif: _thumbavif, ...rest } = file;
|
const { thumbavif: _thumbavif, ...rest } = file;
|
||||||
|
|||||||
Reference in New Issue
Block a user