initial commit

This commit is contained in:
Metazla
2024-04-01 17:17:48 +02:00
commit efc760be6b
19 changed files with 5362 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import { FileIDComputer } from "./fileID/FileIDComputer.js";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData.js";
export class HashComputer implements ComputeInterface{
private fileIDComputer = new FileIDComputer();
constructor(private targetHash: CID_ALGORITHM_NAMES[]) {
}
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;
});
}
}