-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
595c5d8
commit 5b56d98
Showing
12 changed files
with
255 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { ipcMain } from 'electron' | ||
import { app } from 'electron' | ||
import * as fs from 'fs/promises' | ||
import { dirname, join } from 'path' | ||
|
||
import { StorageDB } from '../../src/types/general' | ||
|
||
// Create a new storage interface for filesystem | ||
const cockpitFolderPath = join(app.getPath('home'), 'Cockpit') | ||
fs.mkdir(cockpitFolderPath, { recursive: true }) | ||
|
||
export const filesystemStorage: StorageDB = { | ||
async setItem(key: string, value: ArrayBuffer): Promise<void> { | ||
const buffer = Buffer.from(value) | ||
const filePath = join(cockpitFolderPath, key) | ||
await fs.mkdir(dirname(filePath), { recursive: true }) | ||
await fs.writeFile(filePath, buffer) | ||
}, | ||
async getItem(key: string): Promise<ArrayBuffer | null> { | ||
const filePath = join(cockpitFolderPath, key) | ||
try { | ||
return await fs.readFile(filePath) | ||
} catch (error) { | ||
if (error.code === 'ENOENT') return null | ||
throw error | ||
} | ||
}, | ||
async removeItem(key: string): Promise<void> { | ||
const filePath = join(cockpitFolderPath, key) | ||
await fs.unlink(filePath) | ||
}, | ||
async clear(): Promise<void> { | ||
throw new Error( | ||
`Clear functionality is not available in the filesystem storage, so we don't risk losing important data. If you | ||
want to clear the storage, please delete the Cockpit folder in your user data directory manually.` | ||
) | ||
}, | ||
async keys(): Promise<string[]> { | ||
const dirPath = cockpitFolderPath | ||
try { | ||
return await fs.readdir(dirPath) | ||
} catch (error) { | ||
if (error.code === 'ENOENT') return [] | ||
throw error | ||
} | ||
}, | ||
async iterate(callback: (value: unknown, key: string, iterationNumber: number) => void): Promise<void> { | ||
throw new Error('Iterate functionality is not available in the filesystem storage.') | ||
}, | ||
} | ||
|
||
export const setupFilesystemStorage = (): void => { | ||
ipcMain.handle('setItem', async (_, data) => { | ||
await filesystemStorage.setItem(data.key, data.value) | ||
}) | ||
ipcMain.handle('getItem', async (_, key) => { | ||
return await filesystemStorage.getItem(key) | ||
}) | ||
ipcMain.handle('removeItem', async (_, key) => { | ||
await filesystemStorage.removeItem(key) | ||
}) | ||
ipcMain.handle('clear', async () => { | ||
await filesystemStorage.clear() | ||
}) | ||
ipcMain.handle('keys', async () => { | ||
return await filesystemStorage.keys() | ||
}) | ||
ipcMain.handle('iterate', async (_, callback) => { | ||
await filesystemStorage.iterate(callback) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { StorageDB } from '@/types/general' | ||
|
||
import { isElectron } from '../utils' | ||
|
||
const throwIfNotElectron = (): void => { | ||
if (!isElectron()) { | ||
console.warn('Filesystem storage is only available in Electron.') | ||
return | ||
} | ||
if (!window.electronAPI) { | ||
console.error('electronAPI is not available on window object') | ||
console.debug('Available window properties:', Object.keys(window)) | ||
throw new Error('Electron filesystem API is not properly initialized. This is likely a setup issue.') | ||
} | ||
} | ||
|
||
export const electronStorage: StorageDB = { | ||
setItem: async (key: string, value: Blob): Promise<void> => { | ||
throwIfNotElectron() | ||
await window.electronAPI.setItem(key, value) | ||
}, | ||
getItem: async (key: string): Promise<ArrayBuffer | null | undefined> => { | ||
throwIfNotElectron() | ||
return await window.electronAPI.getItem(key) | ||
}, | ||
removeItem: async (key: string): Promise<void> => { | ||
throwIfNotElectron() | ||
await window.electronAPI.removeItem(key) | ||
}, | ||
clear: async (): Promise<void> => { | ||
throwIfNotElectron() | ||
await window.electronAPI.clear() | ||
}, | ||
keys: async (): Promise<string[]> => { | ||
throwIfNotElectron() | ||
return await window.electronAPI.keys() | ||
}, | ||
iterate: async (callback: (value: Blob, key: string, iterationNumber: number) => void): Promise<void> => { | ||
throwIfNotElectron() | ||
await window.electronAPI.iterate(callback) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import localforage from 'localforage' | ||
|
||
import { electronStorage } from '@/libs/electron/filesystemStorageRendererAPI' | ||
import { StorageDB } from '@/types/general' | ||
|
||
import { isElectron } from './utils' | ||
|
||
const tempVideoChunksIndexdedDB = localforage.createInstance({ | ||
driver: localforage.INDEXEDDB, | ||
name: 'Cockpit - Temporary Video', | ||
storeName: 'cockpit-temp-video-db', | ||
version: 1.0, | ||
description: 'Database for storing the chunks of an ongoing recording, to be merged afterwards.', | ||
}) | ||
|
||
const videoStoringIndexedDB = localforage.createInstance({ | ||
driver: localforage.INDEXEDDB, | ||
name: 'Cockpit - Video Recovery', | ||
storeName: 'cockpit-video-recovery-db', | ||
version: 1.0, | ||
description: 'Local backups of Cockpit video recordings to be retrieved in case of failure.', | ||
}) | ||
|
||
export const videoStorage: StorageDB = isElectron() ? electronStorage : videoStoringIndexedDB | ||
export const tempVideoStorage: StorageDB = isElectron() ? electronStorage : tempVideoChunksIndexdedDB |
Oops, something went wrong.