feat(api): validate plex when settings are saved

This commit is contained in:
sct
2020-09-19 22:13:48 +09:00
parent e0c39aeca1
commit 8f6247d821
7 changed files with 182 additions and 8 deletions

39
server/api/plexapi.ts Normal file
View File

@@ -0,0 +1,39 @@
import NodePlexAPI from 'plex-api';
import { getSettings } from '../lib/settings';
class PlexAPI {
private plexClient: typeof NodePlexAPI;
constructor({ plexToken }: { plexToken?: string }) {
const settings = getSettings();
this.plexClient = new NodePlexAPI({
hostname: settings.plex.ip,
post: settings.plex.port,
token: plexToken,
authenticator: {
authenticate: (
_plexApi: typeof PlexAPI,
cb: (err?: string, token?: string) => void
) => {
if (!plexToken) {
return cb('Plex Token not found!');
}
cb(undefined, plexToken);
},
},
options: {
identifier: settings.clientId,
product: 'Overseerr',
deviceName: 'Overseerr',
platform: 'Overseerr',
},
});
}
public async getStatus() {
return await this.plexClient.query('/');
}
}
export default PlexAPI;

View File

@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
interface Library {
id: string;
@@ -47,6 +48,7 @@ interface PublicSettings {
}
interface AllSettings {
clientId?: string;
main: MainSettings;
plex: PlexSettings;
radarr: RadarrSettings[];
@@ -122,6 +124,15 @@ class Settings {
this.data.public = data;
}
get clientId(): string {
if (!this.data.clientId) {
this.data.clientId = uuidv4();
this.save();
}
return this.data.clientId;
}
/**
* Settings Load
*

View File

@@ -68,9 +68,11 @@ components:
name:
type: string
example: 'Main Server'
readOnly: true
machineId:
type: string
example: '1234-1234-1234-1234'
example: '1234123412341234'
readOnly: true
ip:
type: string
example: '127.0.0.1'
@@ -79,6 +81,7 @@ components:
example: 32400
libraries:
type: array
readOnly: true
items:
$ref: '#/components/schemas/PlexLibrary'
required:

View File

@@ -1,5 +1,8 @@
import { Router } from 'express';
import { getSettings, RadarrSettings, SonarrSettings } from '../lib/settings';
import { getRepository } from 'typeorm';
import { User } from '../entity/User';
import PlexAPI from '../api/plexapi';
const settingsRoutes = Router();
@@ -24,11 +27,33 @@ settingsRoutes.get('/plex', (_req, res) => {
res.status(200).json(settings.plex);
});
settingsRoutes.post('/plex', (req, res) => {
settingsRoutes.post('/plex', async (req, res, next) => {
const userRepository = getRepository(User);
const settings = getSettings();
try {
const admin = await userRepository.findOneOrFail({
select: ['id', 'plexToken'],
order: { id: 'ASC' },
});
settings.plex = req.body;
settings.save();
Object.assign(settings.plex, req.body);
const plexClient = new PlexAPI({ plexToken: admin.plexToken });
const result = await plexClient.getStatus();
if (result?.MediaContainer?.machineIdentifier) {
settings.plex.machineId = result.MediaContainer.machineIdentifier;
settings.plex.name = result.MediaContainer.friendlyName;
settings.save();
}
} catch (e) {
return next({
status: 500,
message: `Failed to connect to Plex: ${e.message}`,
});
}
return res.status(200).json(settings.plex);
});

1
server/types/plex-api.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
declare module 'plex-api';