File size: 10,314 Bytes
d58d97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*

   ELYSIA CODE COMPANION v1.2.2 - File System Access

   Read local folders using File System Access API

*/

import Utils from "./utils.js";

const FileSystem = {
    currentHandle: null,
    files: [],
    folderName: "",
    fileContentCache: new Map(), // Cache for file contents
    cacheMaxSize: 50, // Max cached files

    // Check if File System Access API is supported
    isSupported() {
        return "showDirectoryPicker" in window;
    },

    // Open Folder Picker
    async openFolder() {
        if (!this.isSupported()) {
            Utils.toast.error("File System Access API not supported in this browser. Try Chrome or Edge.");
            return null;
        }

        try {
            const handle = await window.showDirectoryPicker({
                mode: "read"
            });

            this.currentHandle = handle;
            this.folderName = handle.name;

            Utils.toast.info("Reading folder...");
            const files = await this.readDirectory(handle);
            this.files = files;

            Utils.toast.success(`Loaded ${files.length} files from "${this.folderName}"`);
            return {
                handle,
                name: this.folderName,
                files
            };
        } catch (err) {
            if (err.name === "AbortError") {
                Utils.toast.info("Folder selection cancelled");
            } else {
                console.error("Failed to open folder:", err);
                Utils.toast.error("Failed to open folder: " + err.message);
            }
            return null;
        }
    },

    // Read Directory Recursively
    async readDirectory(dirHandle, path = "", maxFiles = 1000) {
        const files = [];
        const maxFileSizeMB = 5; // Skip files larger than 5MB

        try {
            for await (const entry of dirHandle.values()) {
                if (files.length >= maxFiles) {
                    Utils.toast.warning(`Reached max file limit (${maxFiles})`);
                    break;
                }

                const fullPath = path ? `${path}/${entry.name}` : entry.name;

                if (entry.kind === "directory") {
                    // Skip common directories
                    if (this.shouldSkipDirectory(entry.name)) continue;

                    // Recursively read subdirectory
                    const subFiles = await this.readDirectory(entry, fullPath, maxFiles - files.length);
                    files.push(...subFiles);
                } else if (entry.kind === "file") {
                    // Skip non-text files
                    if (!Utils.isTextFile(entry.name)) continue;

                    try {
                        const file = await entry.getFile();

                        // Skip large files
                        if (file.size > maxFileSizeMB * 1024 * 1024) {
                            // Skipping large file: logged for debugging
                            // console.log(`Skipping large file: ${fullPath} (${Utils.formatFileSize(file.size)})`);
                            continue;
                        }

                        files.push({
                            name: entry.name,
                            path: fullPath,
                            type: entry.kind,
                            size: file.size,
                            extension: Utils.getFileExtension(entry.name),
                            language: Utils.getLanguageFromExtension(Utils.getFileExtension(entry.name)),
                            handle: entry
                        });
                    } catch (err) {
                        console.warn(`Failed to read file ${fullPath}:`, err);
                    }
                }
            }
        } catch (err) {
            console.error("Failed to read directory:", err);
        }

        return files;
    },

    // Check if directory should be skipped
    shouldSkipDirectory(name) {
        const nameLower = name.toLowerCase();
        const skipDirs = [
            "node_modules",
            ".git",
            ".vscode",
            "dist",
            "build",
            ".cache",
            "coverage",
            ".next",
            ".nuxt",
            "__pycache__",
            "vendor",
            "target"
        ];
        return skipDirs.includes(nameLower) || name.startsWith(".");
    },

    // Read File Content
    async readFile(fileEntry) {
        try {
            // Check cache first
            const cacheKey = fileEntry.path;
            if (this.fileContentCache.has(cacheKey)) {
                // Cache hit: logged for debugging
                // console.log(`Cache hit for: ${fileEntry.path}`);
                return this.fileContentCache.get(cacheKey);
            }

            const file = await fileEntry.handle.getFile();

            // Warn for very large files (> 1MB)
            if (file.size > 1024 * 1024) {
                const sizeMB = (file.size / (1024 * 1024)).toFixed(2);
                console.warn(`Reading large file: ${fileEntry.path} (${sizeMB}MB)`);

                // Block files > 5MB for safety
                if (file.size > 5 * 1024 * 1024) {
                    throw new Error(`File too large (${sizeMB}MB). Maximum size is 5MB.`);
                }
            }

            const content = await file.text();

            // Add to cache
            if (this.fileContentCache.size >= this.cacheMaxSize) {
                // Remove oldest entry (simple LRU)
                const firstKey = this.fileContentCache.keys().next().value;
                this.fileContentCache.delete(firstKey);
            }
            this.fileContentCache.set(cacheKey, content);

            return content;
        } catch (err) {
            console.error("Failed to read file:", err);
            throw err;
        }
    },

    // Get File by Path
    getFileByPath(path) {
        // Validate path to prevent directory traversal
        if (!path || typeof path !== "string") {
            console.warn("Invalid file path provided");
            return null;
        }

        // Normalize path and prevent directory traversal
        const normalizedPath = path.replace(/\\/g, "/"); // Normalize backslashes

        // Prevent directory traversal attacks
        if (
            normalizedPath.includes("..") ||
            normalizedPath.startsWith("/") ||
            normalizedPath.includes(":") || // Windows absolute paths
            /\.\.[\/\\]/.test(path) || // Encoded traversal
            /%2e%2e/i.test(path) // URL encoded traversal
        ) {
            console.warn("Potentially malicious path detected:", path);
            return null;
        }

        return this.files.find(f => f.path === normalizedPath);
    },

    // Search Files
    searchFiles(query) {
        if (!query) return this.files;

        query = query.toLowerCase();
        return this.files.filter(f => f.name.toLowerCase().includes(query) || f.path.toLowerCase().includes(query));
    },

    // Get Project Statistics
    getStats() {
        if (this.files.length === 0) {
            return {
                totalFiles: 0,
                totalSize: 0,
                languages: {},
                fileTypes: {}
            };
        }

        const stats = {
            totalFiles: this.files.length,
            totalSize: this.files.reduce((sum, f) => sum + f.size, 0),
            languages: {},
            fileTypes: {}
        };

        this.files.forEach(file => {
            // Count by language
            const lang = file.language || "unknown";
            stats.languages[lang] = (stats.languages[lang] || 0) + 1;

            // Count by extension
            const ext = file.extension || "none";
            stats.fileTypes[ext] = (stats.fileTypes[ext] || 0) + 1;
        });

        return stats;
    },

    // Build File Tree Structure
    buildTree() {
        const tree = {
            name: this.folderName,
            type: "directory",
            children: []
        };

        this.files.forEach(file => {
            const parts = file.path.split("/");
            let current = tree;

            parts.forEach((part, index) => {
                const isLast = index === parts.length - 1;

                if (isLast) {
                    current.children.push({
                        name: part,
                        type: "file",
                        path: file.path,
                        file
                    });
                } else {
                    let dir = current.children.find(c => c.name === part && c.type === "directory");
                    if (!dir) {
                        dir = {
                            name: part,
                            type: "directory",
                            children: []
                        };
                        current.children.push(dir);
                    }
                    current = dir;
                }
            });
        });

        // Sort: directories first, then files (alphabetically)
        const sortTree = node => {
            if (node.children) {
                node.children.sort((a, b) => {
                    if (a.type === b.type) {
                        return a.name.localeCompare(b.name);
                    }
                    return a.type === "directory" ? -1 : 1;
                });
                node.children.forEach(sortTree);
            }
        };
        sortTree(tree);

        return tree;
    },

    // Close Current Folder
    close() {
        // Clear all references to prevent memory leaks
        this.currentHandle = null;
        this.files = []; // Reset array reference
        this.folderName = "";

        // Clear cache properly
        if (this.fileContentCache) {
            this.fileContentCache.clear();
        }

        // Folder closed: logged for debugging
        // console.log("Folder closed, memory cleaned");
    }
};

export default FileSystem;