Files
meta-hash/src/lib/HashComputer.ts
T
2024-04-02 19:57:03 +02:00

30 lines
1.1 KiB
TypeScript

import { FileIDComputer } from "./fileID/FileIDComputer";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData";
export class HashComputer implements ComputeInterface{
private fileIDComputer:FileIDComputer;
constructor(private targetHash: CID_ALGORITHM_NAMES[],workerPath?:string) {
this.fileIDComputer = new FileIDComputer(workerPath);
}
async computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void> {
// Dynamically determine which hashes are needed
const neededHashes = this.targetHash.filter(hashName => !metadata[hashName]);
// If all hashes are already computed, skip the processing
if (neededHashes.length === 0) {
return;
}
// Compute only the needed CIDs
const cids = await this.fileIDComputer.computeCIDs(filePath, neededHashes);
// Map the computed CIDs back to their respective metadata properties
cids.forEach((cid, index) => {
const hashType = neededHashes[index];
metadata[hashType] = cid;
});
}
}