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