u
This commit is contained in:
50
src/util/manifestAttachment.js
Normal file
50
src/util/manifestAttachment.js
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user