This commit is contained in:
seb
2026-10-18 14:44:23 +02:00
parent d0a2ede749
commit 0463003681
2 changed files with 19 additions and 25 deletions

View File

@@ -17,25 +17,23 @@ const log = createLogger('import');
* @returns {Promise<{base62: string, width: number, height: number, hashLength: number}|null>}
*/
export async function computeSkeleHash(buffer) {
try {
const { data, info } = await sharp(buffer)
.rotate()
.resize({ width: 200, height: 200, fit: 'inside' })
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const { data, info } = await sharp(buffer)
.rotate()
.resize({ width: 200, height: 200, fit: 'inside' })
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true });
const result = await encodeRGBA(info.width, info.height, data);
return {
base62: result.base62,
width: info.width,
height: info.height,
hashLength: result.hash?.length ?? 0,
};
} catch (err) {
log.warn('Failed to compute skelehash for image:', err.message);
return null;
const result = await encodeRGBA(info.width, info.height, data);
if (!result?.base62) {
throw new Error('encodeRGBA failed to produce a base62 skelehash');
}
return {
base62: result.base62,
width: info.width,
height: info.height,
hashLength: result.hash?.length ?? 0,
};
}
// #endregion

View File

@@ -18,14 +18,10 @@ export async function formatAttachment(file) {
}
if (!skeledash && file.thumbavif) {
try {
const result = await computeSkeleHash(file.thumbavif);
skeledash = result?.base62 || null;
if (file.hash && skeledash) {
skeleCache.set(file.hash, skeledash);
}
} catch {
skeledash = null;
const result = await computeSkeleHash(file.thumbavif);
skeledash = result.base62;
if (file.hash && skeledash) {
skeleCache.set(file.hash, skeledash);
}
}