-
Notifications
You must be signed in to change notification settings - Fork 0
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
7cfe435
commit c1fff53
Showing
4 changed files
with
105 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Store, StoreOptions } from './store' | ||
import { safeStorage } from 'electron' | ||
|
||
interface OBSConnection { | ||
host: string; | ||
port?: number; | ||
password?: string; | ||
} | ||
|
||
function isOBSConnection(obj: any): obj is OBSConnection { | ||
let hasRequiredFields = (obj as OBSConnection).host !== undefined | ||
&& typeof (obj as OBSConnection).host === 'string'; | ||
if (!hasRequiredFields) { | ||
return false; | ||
} | ||
|
||
if ((obj as OBSConnection).port !== undefined) { | ||
if (typeof (obj as OBSConnection).port !== 'number') { | ||
return false; | ||
} | ||
} | ||
|
||
if ((obj as OBSConnection).password !== undefined) { | ||
if (typeof (obj as OBSConnection).password !== 'string') { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export default class Preferences { | ||
store: Store; | ||
|
||
constructor(options: StoreOptions) { | ||
this.store = new Store(options); | ||
} | ||
|
||
get obsConnection(): OBSConnection | null { | ||
let connection = this.store.get('obs-connection'); | ||
if (isOBSConnection(connection)) { | ||
if (connection.password) { | ||
connection.password = safeStorage.decryptString(Buffer.from(connection.password, 'base64')) | ||
} | ||
|
||
return connection; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
set obsConnection(value: OBSConnection | null) { | ||
if (value && value.password) { | ||
value.password = safeStorage.encryptString(value.password).toString('base64') | ||
} | ||
|
||
this.store.set('obs-connection', value) | ||
} | ||
} |
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,39 @@ | ||
import { app } from 'electron' | ||
import * as path from 'path' | ||
import * as fs from 'fs' | ||
|
||
export interface StoreOptions { | ||
name: string; | ||
defaults: { [index: string]: any}; | ||
}; | ||
|
||
export class Store { | ||
path: string; | ||
data: { [index: string]: any}; | ||
|
||
constructor(options: StoreOptions) { | ||
const userDataPath = app.getPath('userData'); | ||
this.path = path.join(userDataPath, options.name + '.json'); | ||
this.data = parseDataFile(this.path, options.defaults); | ||
} | ||
|
||
get(key: string): any { | ||
return this.data[key] | ||
} | ||
|
||
set(key: string, value: any) { | ||
this.data[key] = value | ||
|
||
try { | ||
fs.writeFileSync(this.path, JSON.stringify(this.data)); | ||
} catch {} | ||
} | ||
} | ||
|
||
function parseDataFile(path: string, defaults: any) { | ||
try { | ||
return JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })); | ||
} catch { | ||
return defaults; | ||
} | ||
} |
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