continue work

This commit is contained in:
Raven Scott
2025-11-24 19:16:23 -05:00
parent 9ed79d5209
commit fc91f4c6bb
2 changed files with 171 additions and 47 deletions
+11 -3
View File
@@ -1291,18 +1291,22 @@ swarm.on('connection', (peer) => {
console.log(`[INFO] Handling 'browseDirectory' command for path: ${parsedData.args?.path || '/'}`);
try {
const requestedPath = parsedData.args?.path || '/';
console.log(`[DEBUG] Requested path: ${requestedPath}`);
// Validate and sanitize path
if (!validation.isValidDirectoryPath(requestedPath)) {
console.error(`[ERROR] Invalid directory path: ${requestedPath}`);
throw new Error('Invalid directory path');
}
const safePath = validation.sanitizeDirectoryPath(requestedPath);
console.log(`[DEBUG] Sanitized path: ${safePath}`);
// Read directory contents
const contents = [];
try {
const items = fs.readdirSync(safePath, { withFileTypes: true });
console.log(`[DEBUG] Found ${items.length} items in directory`);
for (const item of items) {
try {
@@ -1322,19 +1326,23 @@ swarm.on('connection', (peer) => {
}
}
console.log(`[DEBUG] Returning ${contents.length} items to client`);
response = { success: true, contents, path: safePath };
} catch (readError) {
console.error(`[ERROR] Failed to read directory ${safePath}:`, readError);
if (readError.code === 'EACCES') {
throw new Error('Permission denied');
throw new Error('Permission denied: You do not have permission to access this directory');
} else if (readError.code === 'ENOENT') {
throw new Error('Directory not found');
throw new Error('Directory not found: The specified path does not exist');
} else if (readError.code === 'ENOTDIR') {
throw new Error('Not a directory: The specified path is not a directory');
} else {
throw new Error(`Failed to read directory: ${readError.message}`);
}
}
} catch (error) {
console.error(`[ERROR] Failed to browse directory: ${error.message}`);
response = { error: `Failed to browse directory: ${error.message}` };
response = { success: false, error: error.message };
}
break;