use folder instead of file for configuration
This commit is contained in:
@@ -7,7 +7,6 @@ let distFolder = import.meta.dirname;
|
||||
distFolder = distFolder.replace('src', 'dist');
|
||||
distFolder = distFolder + "/worker.js";
|
||||
const workerUrl = new URL(distFolder).href;
|
||||
console.log("Worker URL: ", workerUrl);
|
||||
|
||||
export class FileIDComputerWorker{
|
||||
private piscina: Piscina;
|
||||
|
||||
@@ -12,7 +12,7 @@ export class FolderWatcher {
|
||||
queueSize = -1;
|
||||
current = 0;
|
||||
processing = new Set<string>();
|
||||
private readonly updateDebounced: () => Promise<void>;
|
||||
private readonly finalizeDebounced: () => Promise<void>;
|
||||
|
||||
constructor(fileProcessor:FileProcessorInterface,private WATCH_FOLDER_LIST: string, private config:{
|
||||
interval?: number,
|
||||
@@ -23,7 +23,7 @@ export class FolderWatcher {
|
||||
this.fileProcessor = fileProcessor;
|
||||
if(this.fileProcessor.finalize) {
|
||||
const debounced = debounce(() => this.fileProcessor.finalize(), this.config.outputFolderUpdateIntervalMs || 10000, {immediate: true});
|
||||
this.updateDebounced = async () => {
|
||||
this.finalizeDebounced = async () => {
|
||||
if (this.initialized) {
|
||||
return debounced();
|
||||
}
|
||||
@@ -120,7 +120,9 @@ export class FolderWatcher {
|
||||
.on('add', async (filePath) => {
|
||||
try {
|
||||
await this.processFileExtended(filePath);
|
||||
await this.updateDebounced();
|
||||
if(this.finalizeDebounced) {
|
||||
await this.finalizeDebounced();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing file ${filePath}:`, error);
|
||||
}
|
||||
@@ -128,7 +130,9 @@ export class FolderWatcher {
|
||||
.on('change', async (filePath) => {
|
||||
try {
|
||||
await this.processFileExtended(filePath);
|
||||
await this.updateDebounced();
|
||||
if(this.finalizeDebounced) {
|
||||
await this.finalizeDebounced();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Error processing file ${filePath}:`, e);
|
||||
}
|
||||
@@ -139,7 +143,9 @@ export class FolderWatcher {
|
||||
await this.fileProcessor.deleteFile(filePath);
|
||||
}
|
||||
await this.processFileExtended(filePath);
|
||||
await this.updateDebounced();
|
||||
if(this.finalizeDebounced) {
|
||||
await this.finalizeDebounced();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Error processing file deletion:`, e);
|
||||
}
|
||||
@@ -175,7 +181,9 @@ export class FolderWatcher {
|
||||
}
|
||||
await Promise.all(promises);
|
||||
this.initialized = true;
|
||||
await this.updateDebounced();
|
||||
if(this.finalizeDebounced) {
|
||||
await this.finalizeDebounced();
|
||||
}
|
||||
|
||||
console.log(`Watcher ready`);
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ export class HashComputerIndexCache implements ComputeInterface {
|
||||
private hashIndexManager: HashIndexManager;
|
||||
private hashComputer: HashComputerWorker;
|
||||
|
||||
constructor(indexFilePath: string,private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256],workerPath?:string) {
|
||||
constructor(indexFolderPath: string,private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256],workerPath?:string) {
|
||||
this.hashComputer = new HashComputerWorker(targetHash,workerPath);
|
||||
this.hashIndexManager = new HashIndexManager(indexFilePath,targetHash);
|
||||
this.hashIndexManager = new HashIndexManager(indexFolderPath,targetHash);
|
||||
}
|
||||
|
||||
public async computeMissingHash(filePath: string, metadata: MultiHashData): Promise<void> {
|
||||
|
||||
@@ -27,14 +27,8 @@ export class HashIndexManager {
|
||||
private initialLoad: Promise<void>;
|
||||
private filePaths: { [key in CID_ALGORITHM_NAMES]?: string } = {};
|
||||
|
||||
constructor(filePath: string, private targetHash: CID_ALGORITHM_NAMES[] = [CID_ALGORITHM_NAMES.sha1, CID_ALGORITHM_NAMES.sha256]) {
|
||||
filePath = filePath.replace(".csv", "");
|
||||
for (const hash of this.targetHash) {
|
||||
this.filePaths[hash] = `${filePath}-${hash}.csv`;
|
||||
if (!this.filePaths[hash]) {
|
||||
throw new Error(`Invalid index file path for ${hash}`);
|
||||
}
|
||||
}
|
||||
constructor(private indexFolderPath: string,
|
||||
private targetHash: CID_ALGORITHM_NAMES[]) {
|
||||
}
|
||||
|
||||
public getCache(): Map<string, IndexLine> {
|
||||
@@ -49,6 +43,17 @@ export class HashIndexManager {
|
||||
public async init(autosave = true) {
|
||||
if (!this.initialLoad) {
|
||||
this.initialLoad = new Promise<void>(async (resolve, reject) => {
|
||||
const stat = await fs.stat(this.indexFolderPath);
|
||||
if (!stat.isDirectory()) {
|
||||
throw new Error(`Invalid index folder path ${this.indexFolderPath}`);
|
||||
}
|
||||
for (const hash of this.targetHash) {
|
||||
this.filePaths[hash] = path.join(this.indexFolderPath,`index-${hash}.csv`);
|
||||
console.log(`Index file path for ${hash} is ${this.filePaths[hash]}`);
|
||||
if (!this.filePaths[hash]) {
|
||||
throw new Error(`Invalid index file path for ${hash}`);
|
||||
}
|
||||
}
|
||||
try {
|
||||
for (const hash of this.targetHash) {
|
||||
if (!this.checkCSVHeaders(this.filePaths[hash], hash)) {
|
||||
@@ -226,12 +231,12 @@ export class HashIndexManager {
|
||||
public getCidForFile(filePath: string, fileSize: number, mtime: string): IndexLine {
|
||||
const fileName = path.basename(filePath);
|
||||
let fileNameIndex = this.cache.get(fileName);
|
||||
for (const hash of this.targetHash) {
|
||||
if (!fileNameIndex[hash]) {
|
||||
delete fileNameIndex[hash];
|
||||
}
|
||||
}
|
||||
if (fileNameIndex) {
|
||||
for (const hash of this.targetHash) {
|
||||
if (!fileNameIndex[hash]) {
|
||||
delete fileNameIndex[hash];
|
||||
}
|
||||
}
|
||||
if (fileNameIndex.mtime) {
|
||||
//if we have a mtime, we need to check it
|
||||
if (fileNameIndex.size === (fileSize + "") && fileNameIndex.mtime === mtime) {
|
||||
|
||||
+3
-1
@@ -11,4 +11,6 @@ export * from './hash-compute/HashComputerFile.js';
|
||||
|
||||
//folder-watcher
|
||||
export * from './folder-watcher/FolderWatcher.js';
|
||||
export * from './index-interface.js';
|
||||
export * from './index-interface.js';
|
||||
|
||||
export * from './utils/ExistsAsync.js';
|
||||
Reference in New Issue
Block a user