-
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
e4a7f22
commit 5ecff0a
Showing
11 changed files
with
146 additions
and
47 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
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,62 @@ | ||
import { app } from 'electron' | ||
import fs from 'fs/promises' | ||
import { dirname, join } from 'path' | ||
|
||
import { isElectron } from '@/libs/utils' | ||
import { StorageDB } from '@/types/video' | ||
|
||
// Create a new storage interface for filesystem | ||
const cockpitVideosDir = join(app.getPath('userData'), 'Cockpit', 'videos') | ||
const filesystemOnlyInElectronErrorMsg = 'Filesystem storage is only available in Electron' | ||
|
||
export const filesystemStorage: StorageDB = { | ||
async setItem(key: string, value: Blob): Promise<void> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
const filePath = join(cockpitVideosDir, key) | ||
await fs.mkdir(dirname(filePath), { recursive: true }) | ||
await fs.writeFile(filePath, Buffer.from(await value.arrayBuffer())) | ||
}, | ||
async getItem(key: string): Promise<Blob | null> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
const filePath = join(cockpitVideosDir, key) | ||
try { | ||
const buffer = await fs.readFile(filePath) | ||
return new Blob([buffer]) | ||
} catch (error) { | ||
if (error.code === 'ENOENT') return null | ||
throw error | ||
} | ||
}, | ||
async removeItem(key: string): Promise<void> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
const filePath = join(cockpitVideosDir, key) | ||
await fs.unlink(filePath) | ||
}, | ||
async clear(): Promise<void> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
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 video storage, please delete the "videos" folder inside the Cockpit folder in your user data | ||
directory manually.` | ||
) | ||
}, | ||
async keys(): Promise<string[]> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
const dirPath = cockpitVideosDir | ||
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> { | ||
if (!isElectron()) throw new Error(filesystemOnlyInElectronErrorMsg) | ||
const keys = await this.keys() | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i] | ||
const value = await this.getItem(key) | ||
callback(value, key, i) | ||
} | ||
}, | ||
} |
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,20 @@ | ||
import localforage from 'localforage' | ||
|
||
const tempVideoChunksDB = 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 videoStoringDB = 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 = videoStoringDB | ||
export const tempVideoStorage = tempVideoChunksDB |
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,4 @@ | ||
import { filesystemStorage } from './electron/video' | ||
|
||
export const videoStorage = filesystemStorage | ||
export const tempVideoStorage = filesystemStorage |
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
Oops, something went wrong.