This commit is contained in:
Metazla
2024-04-06 16:57:21 +02:00
parent 5045120fbb
commit b28ccd5479
24 changed files with 88 additions and 81 deletions
+6 -6
View File
@@ -1,10 +1,10 @@
import { FileIDComputer } from "./fileID/FileIDComputer";
import {FileIDComputer} from "./fileID/FileIDComputer";
import {CID_ALGORITHM_NAMES, ComputeInterface, MultiHashData} from "./MultiHashData";
export class HashComputer implements ComputeInterface{
private fileIDComputer:FileIDComputer;
export class HashComputer implements ComputeInterface {
private fileIDComputer: FileIDComputer;
constructor(private targetHash: CID_ALGORITHM_NAMES[],workerPath?:string) {
constructor(private targetHash: CID_ALGORITHM_NAMES[], workerPath?: string) {
this.fileIDComputer = new FileIDComputer(workerPath);
}
@@ -21,9 +21,9 @@ export class HashComputer implements ComputeInterface{
const cids = await this.fileIDComputer.computeCIDs(filePath, neededHashes);
// Map the computed CIDs back to their respective metadata properties
cids.forEach((cid, index) => {
for (const [index, cid] of cids.entries()) {
const hashType = neededHashes[index];
metadata[hashType] = cid;
});
}
}
}
+6 -1
View File
@@ -273,7 +273,12 @@ export class HashIndexManager {
const baseName = path.basename(filePath);
let indexLine = this.cache.get(baseName);
if (!indexLine) {
const data = {path: baseName, size: size, mtime: mtime, ...hashs};
//filter only the hashes we need
let filteredHash = {};
for (const hash of this.targetHash) {
filteredHash[hash] = hashs[hash];
}
const data = {path: baseName, size: size, mtime: mtime, ...filteredHash};
this.cache.set(baseName, data);
} else {
//update the cache with the latest data
+5 -4
View File
@@ -1,9 +1,10 @@
export interface MultiHashData {
"cid_sha2-256"?: string;//0x12 sha2-256
cid_sha1?: string; // 0x11 sha1
cid_md5?: string; // 0xd5 md5
"cid_sha3-256"?: string; // 0x16 sha3-256
cid_crc32?: string; //0x0132 CRC32 (SFV)
cid_md5?: string; // 0xd5 md5
cid_sha1?: string; // 0x11 sha1
"cid_sha2-256"?: string;//0x12 sha2-256
"cid_sha3-256"?: string; // 0x16 sha3-256
"cid_sha3_384"?: string; // 0x16 sha3-256
}
export interface ComputeInterface {
-1
View File
@@ -21,7 +21,6 @@ class Crc32Hash implements SimpleHash {
}
digest(): Buffer {
console.log((this._crc>>>0).toString(16)) ;
const buffer = Buffer.alloc(4); // Create a buffer of 4 bytes (32 bits)
buffer.writeInt32BE(this._crc, 0); // Write the unsigned integer to the buffer in big-endian format
return buffer;