|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */
|
2 |
| -import {app, BrowserWindow, ipcMain, shell} from 'electron'; |
| 2 | +import {app, BrowserWindow, ipcMain, shell, dialog} from 'electron'; |
3 | 3 | import * as path from 'path';
|
4 | 4 | import * as fs from 'fs';
|
5 | 5 | import * as os from 'os';
|
@@ -71,6 +71,62 @@ function createWindow(): BrowserWindow {
|
71 | 71 | }
|
72 | 72 | });
|
73 | 73 | });
|
| 74 | + |
| 75 | + // Save file handler |
| 76 | + ipcMain.handle('save-file', async (event, args) => { |
| 77 | + try { |
| 78 | + if (!win) return { success: false, error: 'Window not available' }; |
| 79 | + |
| 80 | + const { fileBuffer, fileName, fileType } = args; |
| 81 | + |
| 82 | + // Show save dialog |
| 83 | + const result = await dialog.showSaveDialog(win, { |
| 84 | + title: 'Save File', |
| 85 | + defaultPath: fileName, |
| 86 | + filters: [ |
| 87 | + { name: 'All Files', extensions: ['*'] } |
| 88 | + ] |
| 89 | + }); |
| 90 | + |
| 91 | + if (result.canceled || !result.filePath) { |
| 92 | + return { success: false, canceled: true }; |
| 93 | + } |
| 94 | + |
| 95 | + // Convert base64 to buffer if needed |
| 96 | + let buffer; |
| 97 | + if (typeof fileBuffer === 'string') { |
| 98 | + buffer = Buffer.from(fileBuffer, 'base64'); |
| 99 | + } else { |
| 100 | + buffer = Buffer.from(fileBuffer); |
| 101 | + } |
| 102 | + |
| 103 | + // Write file to disk |
| 104 | + fs.writeFileSync(result.filePath, buffer); |
| 105 | + |
| 106 | + return { success: true, filePath: result.filePath }; |
| 107 | + } catch (error) { |
| 108 | + console.error('Error saving file:', error); |
| 109 | + return { success: false, error: String(error) }; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + // Open file handler |
| 114 | + ipcMain.handle('open-file', async (event, filePath) => { |
| 115 | + try { |
| 116 | + if (!filePath) return { success: false, error: 'No file path provided' }; |
| 117 | + |
| 118 | + const result = await shell.openPath(filePath); |
| 119 | + |
| 120 | + if (result) { |
| 121 | + return { success: false, error: result }; |
| 122 | + } |
| 123 | + |
| 124 | + return { success: true }; |
| 125 | + } catch (error) { |
| 126 | + console.error('Error opening file:', error); |
| 127 | + return { success: false, error: String(error) }; |
| 128 | + } |
| 129 | + }); |
74 | 130 |
|
75 | 131 | // Window control handlers
|
76 | 132 |
|
|
0 commit comments