filemayor 2.1.0 → 4.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * ═══════════════════════════════════════════════════════════════════
5
+ * FILEMAYOR CORE — METADATA STORE
6
+ * Lightweight, persistent indexing for S2 compliance (<300ms search).
7
+ * ═══════════════════════════════════════════════════════════════════
8
+ */
9
+
10
+ 'use strict';
11
+
12
+ const fs = require('fs').promises;
13
+ const fssync = require('fs');
14
+ const path = require('path');
15
+ const os = require('os');
16
+ const crypto = require('crypto');
17
+
18
+ class MetadataStore {
19
+ constructor(dbPath) {
20
+ this.rootPath = dbPath || path.join(os.homedir(), '.filemayor', 'metadata_index.json');
21
+ this.index = {
22
+ version: '1.0',
23
+ lastUpdated: null,
24
+ files: {} // path -> metadata
25
+ };
26
+ this._ensureDir();
27
+ this.load();
28
+ }
29
+
30
+ _ensureDir() {
31
+ const dir = path.dirname(this.rootPath);
32
+ if (!fssync.existsSync(dir)) {
33
+ fssync.mkdirSync(dir, { recursive: true });
34
+ }
35
+ }
36
+
37
+ load() {
38
+ try {
39
+ if (fssync.existsSync(this.rootPath)) {
40
+ const data = fssync.readFileSync(this.rootPath, 'utf8');
41
+ this.index = JSON.parse(data);
42
+ }
43
+ } catch (err) {
44
+ console.error('[IDX] Failed to load index:', err.message);
45
+ }
46
+ }
47
+
48
+ async save() {
49
+ try {
50
+ this.index.lastUpdated = new Date().toISOString();
51
+ await fs.writeFile(this.rootPath, JSON.stringify(this.index, null, 2));
52
+ } catch (err) {
53
+ console.error('[IDX] Failed to save index:', err.message);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Update the index with fresh file metadata
59
+ * @param {Object[]} files
60
+ */
61
+ update(files) {
62
+ for (const file of files) {
63
+ this.index.files[file.path] = {
64
+ name: file.name,
65
+ size: file.size,
66
+ mtime: file.modified,
67
+ category: file.category,
68
+ ext: file.ext
69
+ };
70
+ }
71
+ }
72
+
73
+ /**
74
+ * Search the index with < 300ms latency
75
+ * @param {string} query
76
+ * @returns {Object[]}
77
+ */
78
+ search(query) {
79
+ const startTime = Date.now();
80
+ const results = [];
81
+ const lowerQuery = query.toLowerCase();
82
+
83
+ for (const [filePath, meta] of Object.entries(this.index.files)) {
84
+ if (meta.name.toLowerCase().includes(lowerQuery) || filePath.toLowerCase().includes(lowerQuery)) {
85
+ results.push({ path: filePath, ...meta });
86
+ }
87
+ if (results.length > 100) break; // Cap results for performance
88
+ }
89
+
90
+ const duration = Date.now() - startTime;
91
+ return {
92
+ results,
93
+ duration,
94
+ totalFound: results.length
95
+ };
96
+ }
97
+
98
+ clear() {
99
+ this.index.files = {};
100
+ this.save();
101
+ }
102
+ }
103
+
104
+ module.exports = new MetadataStore();