-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
347 lines (303 loc) · 11 KB
/
server.js
File metadata and controls
347 lines (303 loc) · 11 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const express = require('express');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = 3000;
// Load configuration
let config = {};
try {
const configPath = path.join(__dirname, 'config.json');
const configData = fs.readFileSync(configPath, 'utf8');
config = JSON.parse(configData);
console.log('Configuration loaded successfully');
} catch (error) {
console.error('Error loading config.json:', error.message);
console.log('Using empty configuration - no folder mappings available');
}
// Logging function
function log(message) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${message}`);
}
// Helper function to get descriptive name for a directory path
function getDescriptiveName(dirPath) {
const homeDir = process.env.HOME || require('os').homedir();
// Common directory mappings
const commonNames = {
[path.join(homeDir, 'Library/CloudStorage/Dropbox')]: 'Dropbox',
[path.join(homeDir, 'Dropbox')]: 'Dropbox (Classic)',
[path.join(homeDir, 'Library/CloudStorage/Dropbox-Personal')]: 'Dropbox Personal',
[path.join(homeDir, 'Library/CloudStorage/Dropbox-Business')]: 'Dropbox Business',
[path.join(homeDir, 'Library/Mobile Documents/com~apple~CloudDocs')]: 'iCloud Drive',
[homeDir]: 'Home Folder',
[path.join(homeDir, 'Desktop')]: 'Desktop',
[path.join(homeDir, 'Documents')]: 'Documents'
};
// Check for exact matches first
if (commonNames[dirPath]) {
return commonNames[dirPath];
}
// For other paths, try to create a descriptive name
if (dirPath.includes('CloudStorage/Dropbox')) {
const parts = dirPath.split('/');
const dropboxPart = parts.find(part => part.startsWith('Dropbox'));
return dropboxPart ? dropboxPart.replace('Dropbox-', 'Dropbox ') : 'Dropbox Folder';
}
if (dirPath.includes('iCloud') || dirPath.includes('CloudDocs')) {
return 'iCloud Drive';
}
// Fallback: use the last directory name
const lastDir = path.basename(dirPath);
return lastDir.charAt(0).toUpperCase() + lastDir.slice(1);
}
// Helper function to return HTML with auto-close functionality
function sendAutoCloseResponse(res, data) {
const html = `
<!DOCTYPE html>
<html>
<head>
<title>Open Folder Server</title>
<meta charset="utf-8">
<style>
body { font-family: system-ui, -apple-system, sans-serif; padding: 20px; text-align: center; background: #f5f5f5; }
.container { max-width: 500px; margin: 50px auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.success { color: #28a745; }
.fallback { color: #fd7e14; }
.error { color: #dc3545; }
.message { font-size: 18px; margin: 20px 0; }
.details { font-size: 14px; color: #666; margin: 10px 0; }
</style>
</head>
<body>
<div class="container">
<h2>🗂️ Open Folder Server</h2>
<div class="message ${data.success ? (data.fallback ? 'fallback' : 'success') : 'error'}">
${data.message}
</div>
${data.requested ? `<div class="details">Requested: ${data.requested}</div>` : ''}
${data.opened ? `<div class="details">Opened: ${data.opened}</div>` : ''}
</div>
</body>
</html>`;
res.setHeader('Content-Type', 'text/html');
res.send(html);
}
// Route handler for opening folders
app.get('/open', (req, res) => {
const { folder, path: folderPath } = req.query;
if (!folder && !folderPath) {
log('Request missing folder or path parameter');
return res.status(400).json({
error: 'Missing folder or path parameter',
usage: 'GET /open?folder=<folder_key> OR /open?path=<folder_path>'
});
}
let targetPath;
// Mode 1: Use dynamic path directly
if (folderPath) {
// Validate path (security check)
if (folderPath.includes('..') || folderPath.startsWith('/')) {
log(`Invalid path rejected: ${folderPath}`);
return res.status(400).json({
error: 'Invalid path',
details: 'Path cannot contain ".." or start with "/"'
});
}
targetPath = folderPath;
log(`Using dynamic path: ${targetPath}`);
}
// Mode 2: Use config mapping (legacy)
else if (config[folder]) {
targetPath = config[folder];
log(`Using config mapping: ${folder} -> ${targetPath}`);
}
// Mode 3: Error - neither valid
else {
log(`Invalid folder key: ${folder}`);
return res.status(404).json({
error: 'Folder key not found',
requested: folder,
available: Object.keys(config),
hint: 'Use ?path=<folder_path> for dynamic paths'
});
}
// Find Dropbox path (try common locations)
const homeDir = process.env.HOME || require('os').homedir();
const possibleDropboxPaths = [
path.join(homeDir, 'Library/CloudStorage/Dropbox'),
path.join(homeDir, 'Dropbox'),
path.join(homeDir, 'Library/CloudStorage/Dropbox-Personal'),
path.join(homeDir, 'Library/CloudStorage/Dropbox-Business')
];
let dropboxBasePath = null;
for (const testPath of possibleDropboxPaths) {
if (fs.existsSync(testPath)) {
dropboxBasePath = testPath;
break;
}
}
if (!dropboxBasePath) {
log(`Dropbox folder not found in any common location`);
return res.status(500).json({
error: 'Dropbox folder not found',
details: 'Could not locate Dropbox folder in standard locations'
});
}
const dropboxPath = path.join(dropboxBasePath, targetPath);
log(`Opening folder: ${folder || folderPath} -> ${dropboxPath}`);
// Check if the target folder exists
if (fs.existsSync(dropboxPath)) {
// Folder exists, open it directly
exec(`open "${dropboxPath}"`, (error, stdout, stderr) => {
if (error) {
log(`Error opening folder: ${error.message}`);
return res.status(500).json({
error: 'Failed to open folder',
details: error.message
});
}
if (stderr) {
log(`Warning: ${stderr}`);
}
log(`Successfully opened folder: ${folder || folderPath}`);
sendAutoCloseResponse(res, {
success: true,
message: `✅ Opened folder: ${folder || folderPath}`,
requested: folder || folderPath,
opened: folder || folderPath,
path: dropboxPath
});
});
} else {
// Folder doesn't exist, try fallback options
log(`Target folder not found: ${dropboxPath}`);
// Build fallback options, starting with user-configured roots
const fallbackOptions = [];
// Add user-configured root directories first
if (config.userRoots && config.userRoots.length > 0) {
config.userRoots.forEach((userRoot) => {
fallbackOptions.push({
name: getDescriptiveName(userRoot),
path: userRoot
});
});
}
// Add common root directories as additional fallbacks
fallbackOptions.push(
{ name: getDescriptiveName(dropboxBasePath), path: dropboxBasePath },
{ name: getDescriptiveName(path.join(homeDir, 'Library/Mobile Documents/com~apple~CloudDocs')), path: path.join(homeDir, 'Library/Mobile Documents/com~apple~CloudDocs') },
{ name: getDescriptiveName(homeDir), path: homeDir },
{ name: getDescriptiveName(path.join(homeDir, 'Desktop')), path: path.join(homeDir, 'Desktop') },
{ name: getDescriptiveName(path.join(homeDir, 'Documents')), path: path.join(homeDir, 'Documents') }
);
// Find the first available fallback
let fallbackPath = null;
let fallbackName = null;
for (const option of fallbackOptions) {
if (fs.existsSync(option.path)) {
fallbackPath = option.path;
fallbackName = option.name;
break;
}
}
if (fallbackPath) {
log(`Opening fallback directory: ${fallbackName} -> ${fallbackPath}`);
exec(`open "${fallbackPath}"`, (error, stdout, stderr) => {
if (error) {
log(`Error opening fallback directory: ${error.message}`);
return res.status(500).json({
error: 'Failed to open fallback directory',
details: error.message
});
}
if (stderr) {
log(`Warning: ${stderr}`);
}
log(`Successfully opened fallback directory: ${fallbackName}`);
sendAutoCloseResponse(res, {
success: true,
message: `Folder not found. Opened ${fallbackName} instead.`,
requested: folder || folderPath,
requestedPath: dropboxPath,
opened: fallbackName,
openedPath: fallbackPath,
fallback: true
});
});
} else {
// No fallback directories found
log(`No fallback directories available`);
sendAutoCloseResponse(res, {
success: false,
message: 'Folder not found and no fallback directories available',
requested: folder || folderPath,
requestedPath: dropboxPath,
error: true
});
}
}
});
// Browse for root directory endpoint
app.get('/browse-root', (req, res) => {
log('Opening directory browser for root selection');
// Use AppleScript to show a folder picker dialog directly via Finder
const appleScript = `
set chosenFolder to choose folder with prompt "Select your preferred root directory (Dropbox, iCloud, etc.)"
return POSIX path of chosenFolder
`;
exec(`osascript -e '${appleScript}'`, (error, stdout, stderr) => {
if (error) {
log(`Error with directory browser: ${error.message}`);
return res.status(500).json({
error: 'Failed to open directory browser',
details: error.message
});
}
const selectedPath = stdout.trim();
log(`User selected root directory: ${selectedPath}`);
// Update config with user's preferred root
if (!config.userRoots) {
config.userRoots = [];
}
// Add to user roots if not already present
if (!config.userRoots.includes(selectedPath)) {
config.userRoots.unshift(selectedPath); // Add to front
// Save updated config
const configPath = path.join(__dirname, 'config.json');
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
log(`Saved new root directory to config: ${selectedPath}`);
}
res.json({
success: true,
message: 'Root directory selected and saved',
selectedPath: selectedPath,
userRoots: config.userRoots
});
});
});
// Health check endpoint
app.get('/health', (req, res) => {
// Filter out userRoots from folder keys
const folderKeys = Object.keys(config).filter(key => key !== 'userRoots');
res.json({
status: 'running',
port: PORT,
folders: folderKeys,
userRoots: config.userRoots || []
});
});
// Start server
app.listen(PORT, 'localhost', () => {
log(`Open Folder Server running on http://localhost:${PORT}`);
log(`Available folders: ${Object.keys(config).join(', ')}`);
});
// Graceful shutdown
process.on('SIGINT', () => {
log('Shutting down server...');
process.exit(0);
});
process.on('SIGTERM', () => {
log('Shutting down server...');
process.exit(0);
});