import { Injectable, inject } from '@angular/core'; import { ElectronBridgeService } from '../../../../core/platform/electron/electron-bridge.service'; import type { AttachmentFileStore } from './attachment-file-store'; /** Attachment file store backed by the Electron main-process filesystem (IPC). */ @Injectable({ providedIn: 'root' }) export class ElectronAttachmentFileStore implements AttachmentFileStore { readonly maxPersistableBytes = Number.POSITIVE_INFINITY; readonly supportsStreamingToDisk = true; readonly supportsChunkedReads = true; readonly providesInlineObjectUrl = false; private readonly electronBridge = inject(ElectronBridgeService); get isAvailable(): boolean { const electronApi = this.electronBridge.getApi(); return !!electronApi?.appendFile && !!electronApi.writeFile && !!electronApi.getAppDataPath; } async getAppDataPath(): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi) { return null; } try { return await electronApi.getAppDataPath(); } catch { return null; } } async ensureDir(dirPath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.ensureDir || !dirPath) { return false; } try { return await electronApi.ensureDir(dirPath); } catch { return false; } } async writeFile(filePath: string, base64Data: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.writeFile || !filePath) { return false; } try { return await electronApi.writeFile(filePath, base64Data); } catch { return false; } } async appendFile(filePath: string, base64Data: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.appendFile || !filePath) { return false; } try { return await electronApi.appendFile(filePath, base64Data); } catch { return false; } } async readFile(filePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi || !filePath) { return null; } try { return await electronApi.readFile(filePath); } catch { return null; } } async readFileChunk(filePath: string, start: number, end: number): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.readFileChunk || !filePath) { return null; } try { return await electronApi.readFileChunk(filePath, start, end); } catch { return null; } } async getFileSize(filePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.getFileSize || !filePath) { return null; } try { return await electronApi.getFileSize(filePath); } catch { return null; } } async fileExists(filePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.fileExists || !filePath) { return false; } try { return await electronApi.fileExists(filePath); } catch { return false; } } async copyFile(sourceFilePath: string, destinationFilePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.copyFile || !sourceFilePath || !destinationFilePath) { return false; } try { return await electronApi.copyFile(sourceFilePath, destinationFilePath); } catch { return false; } } async deleteFile(filePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi || !filePath) { return; } try { await electronApi.deleteFile(filePath); } catch { /* best-effort cleanup */ } } async getFileUrl(filePath: string): Promise { const electronApi = this.electronBridge.getApi(); if (!electronApi?.getFileUrl || !filePath) { return null; } try { return await electronApi.getFileUrl(filePath); } catch { return null; } } }