47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
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) {
|
|
const result = await computeSkeleHash(file.thumbavif);
|
|
skeledash = result.base62;
|
|
if (file.hash && skeledash) {
|
|
skeleCache.set(file.hash, skeledash);
|
|
}
|
|
}
|
|
|
|
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));
|
|
}
|