-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
452 lines (383 loc) · 14.9 KB
/
main.js
File metadata and controls
452 lines (383 loc) · 14.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
const { app, BrowserWindow, ipcMain, dialog, Menu } = require('electron');
const path = require('path');
const SimpleStore = require('./simple-store');
const fs = require('fs');
// Import storage client factory
const StorageClientFactory = require('./storage-clients/StorageClientFactory');
const store = new SimpleStore();
// Handle uncaught exceptions to prevent EPIPE errors from crashing the app
process.on('uncaughtException', (error) => {
// Ignore EPIPE errors which occur when stdout/stderr is closed
if (error.code === 'EPIPE' || error.errno === 'EPIPE') {
return;
}
// Log other errors
console.error('Uncaught Exception:', error);
});
// Set application ID for Windows
if (process.platform === 'win32') {
app.setAppUserModelId('com.obrowser.app');
}
function createWindow() {
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
icon: path.resolve(__dirname, process.platform === 'darwin' ? 'obrowser.icns' : process.platform === 'win32' ? 'obrowser.ico' : 'obrowser.png'),
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: true // Allow DevTools but don't open by default
}
});
mainWindow.loadFile('index.html');
// Create the application menu
const template = [
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'delete' },
{ type: 'separator' },
{ role: 'selectAll' }
]
},
{
label: 'View',
submenu: [
{ role: 'reload', accelerator: 'CmdOrCtrl+R' },
{ role: 'forceReload', accelerator: 'CmdOrCtrl+Shift+R' },
{ type: 'separator' },
{ role: 'toggleDevTools', accelerator: 'CmdOrCtrl+Shift+I' }
]
}
];
// Add macOS specific menu items
if (process.platform === 'darwin') {
template.unshift({
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
});
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
// Log errors - with error handling to prevent EPIPE errors
mainWindow.webContents.on('console-message', (event, level, message, line, sourceId) => {
// Use setImmediate to defer logging and avoid EPIPE errors
setImmediate(() => {
try {
if (!mainWindow.isDestroyed() && process.stdout && process.stdout.writable) {
const levels = ['debug', 'info', 'warning', 'error'];
process.stdout.write(`[${levels[level] || 'info'}] ${message}\n`);
}
} catch (error) {
// Silently ignore write errors when stdout is closed
}
});
});
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Get storage client for a connection
async function getStorageClient(connectionId) {
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
if (!connection) {
console.error(`Connection not found with ID: ${connectionId}`);
console.log('Available connections:', connections.map(c => ({ id: c.id, name: c.name })));
throw new Error(`Connection not found: ${connectionId}`);
}
// Map storage type from connection type
// Default to 'aws-s3' for backward compatibility with existing connections
const storageType = connection.type || 'aws-s3';
// Log connection details for debugging
console.log(`Creating storage client for connection ID: ${connectionId}`);
console.log(`Connection type: ${storageType}`);
console.log(`Connection endpoint: ${connection.endpoint}`);
console.log(`Connection region: ${connection.region}`);
// Always create a new client instance to avoid caching issues
return await StorageClientFactory.createClient(storageType, {...connection});
}
// Get supported client types
ipcMain.handle('get-supported-client-types', async () => {
return StorageClientFactory.getSupportedClientTypes();
});
// S3 Connection Management
// Connection Management
ipcMain.handle('save-connection', async (event, connection) => {
const connections = store.get('connections', []);
connections.push(connection);
store.set('connections', connections);
return connections;
});
ipcMain.handle('save-connections', async (event, connections) => {
store.set('connections', connections);
return connections;
});
ipcMain.handle('get-connections', async () => {
return store.get('connections', []);
});
ipcMain.handle('delete-connection', async (event, id) => {
const connections = store.get('connections', []);
const updatedConnections = connections.filter(conn => conn.id !== id);
store.set('connections', updatedConnections);
return updatedConnections;
});
// File Dialog Operations
ipcMain.handle('show-save-dialog', async () => {
const result = await dialog.showSaveDialog({
properties: ['createDirectory']
});
return result.filePath;
});
ipcMain.handle('show-open-dialog', async () => {
const result = await dialog.showOpenDialog({
properties: ['openFile', 'multiSelections']
});
return result.filePaths;
});
// Storage Operations
ipcMain.handle('list-buckets', async (event, connectionId) => {
try {
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
if (!connection) {
console.error(`Connection not found with ID: ${connectionId}`);
throw new Error('Connection not found');
}
console.log(`Listing buckets for connection: ${connection.name} (ID: ${connectionId})`);
console.log(`Connection type: ${connection.type}, Region: ${connection.region}, Endpoint: ${connection.endpoint}`);
const client = await getStorageClient(connectionId);
const buckets = await client.listBuckets();
console.log(`Retrieved ${buckets.length} buckets`);
return buckets;
} catch (error) {
console.error('Error listing buckets:', error);
throw error;
}
});
ipcMain.handle('list-objects', async (event, connectionId, bucket = null, prefix = '') => {
try {
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
if (!connection) {
console.error(`Connection not found with ID: ${connectionId}`);
throw new Error('Connection not found');
}
console.log(`Listing objects for connection: ${connection.name} (ID: ${connectionId})`);
console.log(`Connection type: ${connection.type}, Region: ${connection.region}, Endpoint: ${connection.endpoint}`);
console.log(`Target bucket: ${bucket || connection.bucket || 'None specified'}`);
console.log(`Prefix: ${prefix || 'root'}`);
// Create a fresh storage client for this operation
const client = await getStorageClient(connectionId);
// If no bucket is specified, use the connection's default bucket if available
const targetBucket = bucket || connection.bucket;
if (!targetBucket) {
console.log('No bucket specified, listing available buckets');
const buckets = await client.listBuckets();
return { type: 'buckets', data: buckets };
}
console.log(`Listing objects in bucket: ${targetBucket} with prefix: ${prefix}`);
const response = await client.listObjects(targetBucket, prefix);
console.log(`Received ${response.data ? response.data.length : 0} objects/folders`);
return response;
} catch (error) {
console.error('Error listing objects:', error);
// Enhanced error handling for region mismatch
if (error.Code === 'AuthorizationHeaderMalformed' && error.Region) {
const expectedRegion = error.Region;
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
const configuredRegion = connection ? connection.region : 'unknown';
// Create a more user-friendly error message
const enhancedError = new Error(
`Region mismatch: You configured the connection to use '${configuredRegion}' but the bucket is in '${expectedRegion}'. ` +
`Please update your connection settings to use the correct region.`
);
// Preserve original error properties
enhancedError.originalError = error;
enhancedError.Code = error.Code;
enhancedError.Region = error.Region;
throw enhancedError;
}
throw error;
}
});
ipcMain.handle('get-object-url', async (event, connectionId, bucket, key, operation) => {
try {
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
if (!connection) {
console.error(`Connection not found with ID: ${connectionId}`);
throw new Error('Connection not found');
}
console.log(`Getting ${operation} URL for object: ${key}`);
console.log(`Connection: ${connection.name} (ID: ${connectionId})`);
console.log(`Bucket: ${bucket}, Connection type: ${connection.type}, Region: ${connection.region}`);
const client = await getStorageClient(connectionId);
const url = await client.getObjectUrl(bucket, key, operation);
console.log(`Generated URL for ${operation}: ${url.substring(0, 100)}...`);
return url;
} catch (error) {
console.error(`Error getting ${operation} URL:`, error);
throw error;
}
});
ipcMain.handle('upload-object', async (event, connectionId, bucket, key, filePath) => {
try {
const connections = store.get('connections', []);
const connection = connections.find(conn => conn.id === connectionId);
if (!connection) {
console.error(`Connection not found with ID: ${connectionId}`);
throw new Error('Connection not found');
}
console.log(`Uploading file to: ${bucket}/${key}`);
console.log(`Connection: ${connection.name} (ID: ${connectionId})`);
console.log(`Connection type: ${connection.type}, Region: ${connection.region}`);
console.log(`File path: ${filePath}`);
// Read the file content
const fileContent = fs.readFileSync(filePath);
const client = await getStorageClient(connectionId);
await client.uploadObject(bucket, key, fileContent);
console.log('Upload completed successfully');
return { success: true };
} catch (error) {
console.error('Error uploading object:', error);
throw error;
}
});
ipcMain.handle('create-folder', async (event, connectionId, bucket, folderPath) => {
try {
const client = await getStorageClient(connectionId);
return await client.createFolder(bucket, folderPath);
} catch (error) {
console.error('Error creating folder:', error);
throw error;
}
});
ipcMain.handle('preview-object', async (event, connectionId, bucket, key) => {
try {
const client = await getStorageClient(connectionId);
const ext = key.split('.').pop()?.toLowerCase();
// For text files, fetch and return content directly
if (ext && ext.match(/^(txt|json|js|css|xml|md|markdown|yaml|yml|ini|csv|log)$/)) {
const response = await client.getObject(bucket, key);
let text;
if (response.Body.transformToString) {
// AWS S3 SDK response
text = await response.Body.transformToString();
} else if (Buffer.isBuffer(response.Body)) {
// Buffer directly
text = response.Body.toString('utf-8');
} else if (response.Body.read) {
// Readable stream
text = await streamToString(response.Body);
} else {
// Handle other formats (e.g., Azure Blob)
text = await streamToString(response.Body);
}
return { type: 'text', content: text };
}
// For all other files, generate a signed URL that expires in 1 hour
const signedUrl = await client.getSignedUrl(bucket, key, 3600);
return { type: ext, content: signedUrl };
} catch (error) {
console.error('Error previewing object:', error);
throw error;
}
});
// Helper function to convert stream to string
function streamToString(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
});
}
ipcMain.handle('download-object', async (event, connectionId, key, bucket) => {
try {
const client = await getStorageClient(connectionId);
const response = await client.getObject(bucket, key);
// Show save dialog with default filename
const defaultPath = path.basename(key);
const { filePath } = await dialog.showSaveDialog({
defaultPath: defaultPath,
properties: ['createDirectory']
});
if (!filePath) return { success: false, cancelled: true };
const writeStream = fs.createWriteStream(filePath);
if (response.Body.pipe) {
// Handle stream response
await new Promise((resolve, reject) => {
response.Body.pipe(writeStream)
.on('finish', resolve)
.on('error', reject);
});
} else if (Buffer.isBuffer(response.Body)) {
// Handle buffer response
fs.writeFileSync(filePath, response.Body);
} else {
// Handle other types of response
throw new Error('Unsupported response type');
}
return { success: true, path: filePath };
} catch (error) {
console.error('Error downloading object:', error);
throw error;
}
});
ipcMain.handle('delete-object', async (event, connectionId, key, bucket, isFolder = false) => {
try {
const client = await getStorageClient(connectionId);
if (isFolder) {
let count = 0;
// Ensure the folder key ends with '/'
const folderKey = key.endsWith('/') ? key : key + '/';
console.log('Deleting folder:', folderKey);
// List all objects in the folder
const result = await client.listObjects(bucket, folderKey);
const objectsToDelete = result.data.map(item => item.Key);
if (objectsToDelete.length > 0) {
// Delete all objects in batch
const deleteResult = await client.deleteObjects(bucket, objectsToDelete);
count = deleteResult.deleted ? deleteResult.deleted.length : objectsToDelete.length;
}
// Delete the folder marker itself
await client.deleteObject(bucket, folderKey);
console.log(`Deleted folder marker ${folderKey}`);
return { success: true, message: `${count} files and folder deleted.` };
} else {
await client.deleteObject(bucket, key);
return { success: true, message: 'File deleted.' };
}
} catch (error) {
console.error('Error deleting object:', error);
throw error;
}
});