Files
crowd-funder-for-time-pwa/electron/src/preload.ts
Matthew Raymer e883029531 feat: implement secure IPC-based file export for Electron
Replace sandboxed Capacitor filesystem with native IPC for reliable file exports:
- Add IPC handler in main process for direct Downloads folder access
- Expose secure electronAPI via contextBridge in preload script
- Update ElectronPlatformService to use native IPC with web fallback
- Add TypeScript definitions for electron APIs
- Fix file export issues where files were trapped in virtual filesystem
- Enable proper date-stamped backup filenames in Downloads folder
- Follow Electron security best practices with process isolation

Files now export directly to ~/Downloads with exact path feedback.
2025-07-06 03:46:28 +00:00

35 lines
1.1 KiB
TypeScript

import { contextBridge, ipcRenderer } from 'electron';
require('./rt/electron-rt');
//////////////////////////////
// User Defined Preload scripts below
console.log('User Preload!');
/**
* Expose secure IPC APIs to the renderer process.
*
* This creates a bridge between the sandboxed renderer and the main process,
* allowing secure file operations while maintaining Electron's security model.
*/
contextBridge.exposeInMainWorld('electronAPI', {
/**
* Export data to the user's Downloads folder.
*
* @param fileName - The name of the file to save (e.g., 'backup-2025-07-06.json')
* @param data - The content to write to the file (string)
* @returns Promise<{success: boolean, path?: string, error?: string}>
*
* @example
* ```typescript
* const result = await window.electronAPI.exportData('my-backup.json', JSON.stringify(data));
* if (result.success) {
* console.log('File saved to:', result.path);
* } else {
* console.error('Export failed:', result.error);
* }
* ```
*/
exportData: (fileName: string, data: string) =>
ipcRenderer.invoke('export-data-to-downloads', fileName, data)
});