This commit is contained in:
seb
2026-10-15 00:30:23 +02:00
parent bdd7ebf5b8
commit 8c55f899b5
27 changed files with 136 additions and 46 deletions

View File

@@ -0,0 +1,50 @@
import { computeSkeleHash } from '../services/companyImport/imageHasher.js';
const skeleCache = new Map();
/**
* Strips raw binary `thumbavif` buffer and attaches `skeledash` (base62 skelehash string).
*
* @param {object} file - Raw attachment file object from libdashstore.
* @returns {Promise<object>} Formatted attachment with `skeledash` and without `thumbavif`.
*/
export async function formatAttachment(file) {
if (!file || typeof file !== 'object') return file;
let skeledash = file.skeledash || file.skelehash || file.meta?.skeledash || file.meta?.skelehash || null;
if (!skeledash && file.hash && skeleCache.has(file.hash)) {
skeledash = skeleCache.get(file.hash);
}
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 { thumbavif: _thumbavif, ...rest } = file;
return {
...rest,
skeledash,
skelehash: skeledash,
};
}
/**
* Formats a list of attachments in parallel.
*
* @param {Array<object>} files - Array of raw file attachment objects.
* @returns {Promise<Array<object>>}
*/
export async function formatAttachments(files = []) {
if (!Array.isArray(files) || files.length === 0) return [];
return Promise.all(files.map(formatAttachment));
}