Skip to content

Commit

Permalink
Save OBS connection info.
Browse files Browse the repository at this point in the history
  • Loading branch information
grantjbutler committed Nov 19, 2021
1 parent 7cfe435 commit c1fff53
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0",
"@vue/eslint-config-typescript": "^7.0.0",
"electron": "^13.0.0",
"electron": "^16.0.0",
"electron-devtools-installer": "^3.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0",
Expand Down
59 changes: 59 additions & 0 deletions src/preferences/index.ts
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)
}
}
39 changes: 39 additions & 0 deletions src/preferences/store.ts
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;
}
}
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@
ajv "^6.12.0"
ajv-keywords "^3.4.1"

"@electron/get@^1.0.1":
"@electron/get@^1.13.0":
version "1.13.1"
resolved "https://registry.yarnpkg.com/@electron/get/-/get-1.13.1.tgz#42a0aa62fd1189638bd966e23effaebb16108368"
integrity sha512-U5vkXDZ9DwXtkPqlB45tfYnnYBN8PePp1z/XDCupnSpdrxT8/ThCv9WCwPLf9oqiSGZTkH6dx2jDUPuoXpjkcA==
Expand Down Expand Up @@ -4156,12 +4156,12 @@ electron-to-chromium@^1.3.896:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.901.tgz#ce2c3157d61bce9f42f1e83225c17358ae9f4918"
integrity sha512-ToJdV2vzwT2jeAsw8zIggTFllJ4Kxvwghk39AhJEHHlIxor10wsFI3wo69p8nFc0s/ATWBqugPv/k3nW4Y9Mww==

electron@^13.0.0:
version "13.6.2"
resolved "https://registry.yarnpkg.com/electron/-/electron-13.6.2.tgz#878e01d78cd442a8ec28340b271608ba5b7c7ebd"
integrity sha512-ZXx9t68yXftvNZVnQ7v2XHcnH+MPUF6LNStoz4MMXuWpkF9gq3qwjcYSqnbM4wiVkvWVHIyYvt1yemmStza9dQ==
electron@^16.0.0:
version "16.0.1"
resolved "https://registry.yarnpkg.com/electron/-/electron-16.0.1.tgz#34c0946bd6b6fa8de5181338a04cb690611f140c"
integrity sha512-6TSDBcoKGgmKL/+W+LyaXidRVeRl1V4I81ZOWcqsVksdTMfM4AlxTgfaoYdK/nUhqBrUtuPDcqOyJE6Bc4qMpw==
dependencies:
"@electron/get" "^1.0.1"
"@electron/get" "^1.13.0"
"@types/node" "^14.6.2"
extract-zip "^1.0.3"

Expand Down

0 comments on commit c1fff53

Please sign in to comment.