BACKEND DONE FOR ALL APPS
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { promises as fs, watch, FSWatcher } from 'fs';
|
||||
import path from 'path';
|
||||
import { MemoryManager } from './memory_manager';
|
||||
import { JsonManager } from './json_manager';
|
||||
|
||||
export class DirectoryWatcher {
|
||||
private directoryPath: string;
|
||||
private directoryMemoryId: string;
|
||||
private directoryScheme: any;
|
||||
private applicationInfo: JsonManager;
|
||||
private memoryManager: MemoryManager;
|
||||
private readonly sourceKey: string;
|
||||
private directoryWatcher: FSWatcher | null; // To store the watcher reference
|
||||
private totalSize: number; // To store total directory size
|
||||
|
||||
constructor(memoryManagerPath: string, applicationInfoPath: string, sourceKey: string) {
|
||||
this.sourceKey = sourceKey;
|
||||
this.applicationInfo = new JsonManager(applicationInfoPath);
|
||||
this.memoryManager = new MemoryManager(memoryManagerPath);
|
||||
this.directoryMemoryId = '';
|
||||
this.directoryPath = '';
|
||||
this.directoryWatcher = null; // Initialize with no watcher
|
||||
this.totalSize = 0; // Initialize size with zero
|
||||
}
|
||||
|
||||
// Method to initialize and validate the backup directory
|
||||
async initialize(): Promise<boolean> {
|
||||
const directoryData = await this.applicationInfo.readValue(this.sourceKey);
|
||||
if (!directoryData) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.directoryPath = directoryData.path;
|
||||
this.directoryMemoryId = directoryData.id;
|
||||
|
||||
if (!this.directoryMemoryId || !this.directoryPath) {
|
||||
console.error('Components of entry in \'DirectoryWatcher\' not found.');
|
||||
await this.applicationInfo.removeValue(this.sourceKey);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this.directoryScheme || Object.keys(this.directoryScheme).length === 0) {
|
||||
// No structure in memory, scan and save it
|
||||
const result = await this.buildDirectoryScheme(this.directoryPath);
|
||||
this.directoryScheme = result.structure;
|
||||
this.totalSize = result.size;
|
||||
|
||||
await this.memoryManager.updateMetaInformation(this.directoryMemoryId, {
|
||||
structure: this.directoryScheme,
|
||||
totalSize: this.totalSize,
|
||||
});
|
||||
}
|
||||
|
||||
// Start watching the directory (after stopping any existing watcher)
|
||||
this.restartWatcher();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Recursively build the directory structure and calculate the total size
|
||||
private async buildDirectoryScheme(dirPath: string): Promise<{ structure: any, size: number }> {
|
||||
const directoryScheme: any = {};
|
||||
let totalSize = 0;
|
||||
|
||||
const items = await fs.readdir(dirPath, { withFileTypes: true });
|
||||
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dirPath, item.name);
|
||||
const stats = await fs.stat(fullPath); // Get stats for each item
|
||||
|
||||
if (item.isDirectory()) {
|
||||
// If it's a directory, recursively build its structure and accumulate size
|
||||
const { structure, size } = await this.buildDirectoryScheme(fullPath);
|
||||
directoryScheme[item.name] = structure;
|
||||
totalSize += size;
|
||||
} else if (item.isFile()) {
|
||||
// If it's a file, store its full path and accumulate size
|
||||
directoryScheme[item.name] = fullPath;
|
||||
totalSize += stats.size; // Add file size
|
||||
}
|
||||
}
|
||||
|
||||
return { structure: directoryScheme, size: totalSize };
|
||||
}
|
||||
|
||||
// Restart the directory watcher, ensuring any previous watcher is closed
|
||||
private restartWatcher(): void {
|
||||
if (this.directoryWatcher) {
|
||||
console.log('Stopping existing watcher...');
|
||||
this.directoryWatcher.close(); // Stop the existing watcher
|
||||
}
|
||||
|
||||
this.startDirectoryWatcher();
|
||||
}
|
||||
|
||||
// Start watching the backup directory for changes
|
||||
private startDirectoryWatcher(): void {
|
||||
if (!this.directoryPath) {
|
||||
throw new Error('Backup directory not set. Cannot start watcher.');
|
||||
}
|
||||
|
||||
this.directoryWatcher = watch(this.directoryPath, { recursive: true }, async (eventType, filename) => {
|
||||
if (filename) {
|
||||
console.log(`File change detected: ${eventType} - ${filename}`);
|
||||
// Rebuild the directory scheme and update memory
|
||||
const result = await this.buildDirectoryScheme(this.directoryPath);
|
||||
this.directoryScheme = result.structure;
|
||||
this.totalSize = result.size;
|
||||
|
||||
await this.memoryManager.updateMetaInformation(this.directoryMemoryId, {
|
||||
structure: this.directoryScheme,
|
||||
totalSize: this.totalSize,
|
||||
});
|
||||
|
||||
console.log('Directory structure and size updated in memory.');
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Watching for changes in: ${this.directoryPath}`);
|
||||
}
|
||||
|
||||
// Close the directory watcher
|
||||
public closeWatcher(): void {
|
||||
if (this.directoryWatcher) {
|
||||
console.log(`Stopping watcher for ${this.directoryPath}`);
|
||||
this.directoryWatcher.close();
|
||||
this.directoryWatcher = null; // Clear the reference after closing
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user