@trap_stevo/vault-link
Version:
Unlock the ultimate gateway to secure file and directory access. VaultLink transforms your files into guarded, time-sensitive links—sealed, tamper-proof, and effortlessly shareable. Elevate your storage game with unparalleled precision and flexibility, tu
64 lines (63 loc) • 2.02 kB
JavaScript
"use strict";
const path = require("path");
const fs = require("fs");
class ReferenceManager {
constructor(coreDirectory = "VaultLinkCore") {
this.groups = {};
this.coreDirectory = path.resolve(__dirname, "..", "..", "..", coreDirectory);
this.loadGroups();
}
getGroupFilePath(group) {
return path.join(this.coreDirectory, `${group}.json`);
}
addReference(group, reference, data) {
if (!this.groups[group]) {
this.groups[group] = {};
}
this.groups[group][reference] = data;
this.saveGroup(group);
}
getReference(group, reference) {
return this.groups[group]?.[reference] || null;
}
deleteReference(group, reference) {
if (this.groups[group]) {
delete this.groups[group][reference];
this.saveGroup(group);
}
}
saveGroup(group) {
const groupFilePath = this.getGroupFilePath(group);
fs.writeFileSync(groupFilePath, JSON.stringify(this.groups[group], null, 2), "utf8");
}
loadGroups() {
if (!fs.existsSync(this.coreDirectory)) {
fs.mkdirSync(this.coreDirectory, {
recursive: true
});
}
const files = fs.readdirSync(this.coreDirectory);
for (const file of files) {
const groupName = path.basename(file, ".json");
const groupData = JSON.parse(fs.readFileSync(path.join(this.coreDirectory, file), "utf8"));
this.groups[groupName] = groupData;
this.cleanupGroup(groupName);
}
}
cleanupGroup(group) {
if (this.groups[group]) {
const currentTime = Math.floor(Date.now() / 1000);
const cleanedReferences = Object.entries(this.groups[group]).filter(([_, data]) => {
return !data.expires || data.expires > currentTime;
});
this.groups[group] = Object.fromEntries(cleanedReferences);
this.saveGroup(group);
}
}
scheduleCleanup(interval = 3600000) {
setInterval(() => {
Object.keys(this.groups).forEach(group => this.cleanupGroup(group));
}, interval * 1000);
}
}
module.exports = new ReferenceManager();