You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
453 lines
12 KiB
453 lines
12 KiB
4 years ago
|
import { Router } from 'express';
|
||
4 years ago
|
import {
|
||
|
getSettings,
|
||
|
RadarrSettings,
|
||
|
SonarrSettings,
|
||
|
Library,
|
||
4 years ago
|
MainSettings,
|
||
4 years ago
|
} from '../../lib/settings';
|
||
4 years ago
|
import { getRepository } from 'typeorm';
|
||
4 years ago
|
import { User } from '../../entity/User';
|
||
|
import PlexAPI from '../../api/plexapi';
|
||
|
import { jobPlexFullSync } from '../../job/plexsync';
|
||
|
import SonarrAPI from '../../api/sonarr';
|
||
|
import RadarrAPI from '../../api/radarr';
|
||
|
import logger from '../../logger';
|
||
|
import { scheduledJobs } from '../../job/schedule';
|
||
|
import { Permission } from '../../lib/permissions';
|
||
|
import { isAuthenticated } from '../../middleware/auth';
|
||
4 years ago
|
import { merge, omit } from 'lodash';
|
||
4 years ago
|
import Media from '../../entity/Media';
|
||
|
import { MediaRequest } from '../../entity/MediaRequest';
|
||
|
import { getAppVersion } from '../../utils/appVersion';
|
||
|
import { SettingsAboutResponse } from '../../interfaces/api/settingsInterfaces';
|
||
|
import notificationRoutes from './notifications';
|
||
4 years ago
|
|
||
|
const settingsRoutes = Router();
|
||
|
|
||
4 years ago
|
settingsRoutes.use('/notifications', notificationRoutes);
|
||
|
|
||
4 years ago
|
const filteredMainSettings = (
|
||
|
user: User,
|
||
|
main: MainSettings
|
||
|
): Partial<MainSettings> => {
|
||
|
if (!user?.hasPermission(Permission.ADMIN)) {
|
||
4 years ago
|
return omit(main, 'apiKey');
|
||
4 years ago
|
}
|
||
|
|
||
|
return main;
|
||
|
};
|
||
|
|
||
|
settingsRoutes.get('/main', (req, res, next) => {
|
||
4 years ago
|
const settings = getSettings();
|
||
|
|
||
4 years ago
|
if (!req.user) {
|
||
|
return next({ status: 500, message: 'User missing from request' });
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
res.status(200).json(filteredMainSettings(req.user, settings.main));
|
||
4 years ago
|
});
|
||
|
|
||
|
settingsRoutes.post('/main', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
4 years ago
|
settings.main = merge(settings.main, req.body);
|
||
4 years ago
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(settings.main);
|
||
4 years ago
|
});
|
||
|
|
||
|
settingsRoutes.get('/main/regenerate', (req, res, next) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const main = settings.regenerateApiKey();
|
||
|
|
||
|
if (!req.user) {
|
||
|
return next({ status: 500, message: 'User missing from request' });
|
||
|
}
|
||
|
|
||
|
return res.status(200).json(filteredMainSettings(req.user, main));
|
||
4 years ago
|
});
|
||
|
|
||
|
settingsRoutes.get('/plex', (_req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
res.status(200).json(settings.plex);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.post('/plex', async (req, res, next) => {
|
||
|
const userRepository = getRepository(User);
|
||
4 years ago
|
const settings = getSettings();
|
||
4 years ago
|
try {
|
||
|
const admin = await userRepository.findOneOrFail({
|
||
|
select: ['id', 'plexToken'],
|
||
|
order: { id: 'ASC' },
|
||
|
});
|
||
4 years ago
|
|
||
4 years ago
|
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}`,
|
||
|
});
|
||
|
}
|
||
4 years ago
|
|
||
|
return res.status(200).json(settings.plex);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get('/plex/library', async (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
if (req.query.sync) {
|
||
|
const userRepository = getRepository(User);
|
||
|
const admin = await userRepository.findOneOrFail({
|
||
|
select: ['id', 'plexToken'],
|
||
|
order: { id: 'ASC' },
|
||
|
});
|
||
|
const plexapi = new PlexAPI({ plexToken: admin.plexToken });
|
||
|
|
||
|
const libraries = await plexapi.getLibraries();
|
||
|
|
||
4 years ago
|
const newLibraries: Library[] = libraries
|
||
|
// Remove libraries that are not movie or show
|
||
|
.filter((library) => library.type === 'movie' || library.type === 'show')
|
||
|
// Remove libraries that do not have a metadata agent set (usually personal video libraries)
|
||
|
.filter((library) => library.agent !== 'com.plexapp.agents.none')
|
||
|
.map((library) => {
|
||
|
const existing = settings.plex.libraries.find(
|
||
4 years ago
|
(l) => l.id === library.key && l.name === library.title
|
||
4 years ago
|
);
|
||
|
|
||
|
return {
|
||
|
id: library.key,
|
||
|
name: library.title,
|
||
|
enabled: existing?.enabled ?? false,
|
||
|
};
|
||
|
});
|
||
4 years ago
|
|
||
|
settings.plex.libraries = newLibraries;
|
||
|
}
|
||
|
|
||
|
const enabledLibraries = req.query.enable
|
||
|
? (req.query.enable as string).split(',')
|
||
|
: [];
|
||
|
settings.plex.libraries = settings.plex.libraries.map((library) => ({
|
||
|
...library,
|
||
|
enabled: enabledLibraries.includes(library.id),
|
||
|
}));
|
||
|
settings.save();
|
||
|
return res.status(200).json(settings.plex.libraries);
|
||
|
});
|
||
|
|
||
|
settingsRoutes.get('/plex/sync', (req, res) => {
|
||
|
if (req.query.cancel) {
|
||
4 years ago
|
jobPlexFullSync.cancel();
|
||
4 years ago
|
} else if (req.query.start) {
|
||
4 years ago
|
jobPlexFullSync.run();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
return res.status(200).json(jobPlexFullSync.status());
|
||
4 years ago
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get('/radarr', (_req, res) => {
|
||
4 years ago
|
const settings = getSettings();
|
||
|
|
||
|
res.status(200).json(settings.radarr);
|
||
|
});
|
||
|
|
||
|
settingsRoutes.post('/radarr', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const newRadarr = req.body as RadarrSettings;
|
||
|
const lastItem = settings.radarr[settings.radarr.length - 1];
|
||
|
newRadarr.id = lastItem ? lastItem.id + 1 : 0;
|
||
|
|
||
4 years ago
|
// If we are setting this as the default, clear any previous defaults for the same type first
|
||
|
// ex: if is4k is true, it will only remove defaults for other servers that have is4k set to true
|
||
|
// and are the default
|
||
|
if (req.body.isDefault) {
|
||
|
settings.radarr
|
||
|
.filter((radarrInstance) => radarrInstance.is4k === req.body.is4k)
|
||
|
.forEach((radarrInstance) => {
|
||
|
radarrInstance.isDefault = false;
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
settings.radarr = [...settings.radarr, newRadarr];
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(201).json(newRadarr);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.post('/radarr/test', async (req, res, next) => {
|
||
|
try {
|
||
|
const radarr = new RadarrAPI({
|
||
|
apiKey: req.body.apiKey,
|
||
|
url: `${req.body.useSsl ? 'https' : 'http'}://${req.body.hostname}:${
|
||
|
req.body.port
|
||
|
}${req.body.baseUrl ?? ''}/api`,
|
||
|
});
|
||
|
|
||
|
const profiles = await radarr.getProfiles();
|
||
|
const folders = await radarr.getRootFolders();
|
||
|
|
||
|
return res.status(200).json({
|
||
|
profiles,
|
||
|
rootFolders: folders.map((folder) => ({
|
||
|
id: folder.id,
|
||
|
path: folder.path,
|
||
|
})),
|
||
|
});
|
||
|
} catch (e) {
|
||
|
logger.error('Failed to test Radarr', {
|
||
|
label: 'Radarr',
|
||
|
message: e.message,
|
||
|
});
|
||
|
|
||
|
next({ status: 500, message: 'Failed to connect to Radarr' });
|
||
|
}
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.put<{ id: string }>('/radarr/:id', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const radarrIndex = settings.radarr.findIndex(
|
||
|
(r) => r.id === Number(req.params.id)
|
||
|
);
|
||
|
|
||
|
if (radarrIndex === -1) {
|
||
|
return res
|
||
|
.status(404)
|
||
|
.json({ status: '404', message: 'Settings instance not found' });
|
||
|
}
|
||
|
|
||
4 years ago
|
// If we are setting this as the default, clear any previous defaults for the same type first
|
||
|
// ex: if is4k is true, it will only remove defaults for other servers that have is4k set to true
|
||
|
// and are the default
|
||
|
if (req.body.isDefault) {
|
||
|
settings.radarr
|
||
|
.filter((radarrInstance) => radarrInstance.is4k === req.body.is4k)
|
||
|
.forEach((radarrInstance) => {
|
||
|
radarrInstance.isDefault = false;
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
settings.radarr[radarrIndex] = {
|
||
|
...req.body,
|
||
|
id: Number(req.params.id),
|
||
|
} as RadarrSettings;
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(settings.radarr[radarrIndex]);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get<{ id: string }>('/radarr/:id/profiles', async (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const radarrSettings = settings.radarr.find(
|
||
|
(r) => r.id === Number(req.params.id)
|
||
|
);
|
||
|
|
||
|
if (!radarrSettings) {
|
||
|
return res
|
||
|
.status(404)
|
||
|
.json({ status: '404', message: 'Settings instance not found' });
|
||
|
}
|
||
|
|
||
|
const radarr = new RadarrAPI({
|
||
|
apiKey: radarrSettings.apiKey,
|
||
|
url: `${radarrSettings.useSsl ? 'https' : 'http'}://${
|
||
|
radarrSettings.hostname
|
||
|
}:${radarrSettings.port}${radarrSettings.baseUrl ?? ''}/api`,
|
||
|
});
|
||
|
|
||
|
const profiles = await radarr.getProfiles();
|
||
|
|
||
|
return res.status(200).json(
|
||
|
profiles.map((profile) => ({
|
||
|
id: profile.id,
|
||
|
name: profile.name,
|
||
|
}))
|
||
|
);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.delete<{ id: string }>('/radarr/:id', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const radarrIndex = settings.radarr.findIndex(
|
||
|
(r) => r.id === Number(req.params.id)
|
||
|
);
|
||
|
|
||
|
if (radarrIndex === -1) {
|
||
|
return res
|
||
|
.status(404)
|
||
|
.json({ status: '404', message: 'Settings instance not found' });
|
||
|
}
|
||
|
|
||
|
const removed = settings.radarr.splice(radarrIndex, 1);
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(removed[0]);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get('/sonarr', (_req, res) => {
|
||
4 years ago
|
const settings = getSettings();
|
||
|
|
||
|
res.status(200).json(settings.sonarr);
|
||
|
});
|
||
|
|
||
|
settingsRoutes.post('/sonarr', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const newSonarr = req.body as SonarrSettings;
|
||
|
const lastItem = settings.sonarr[settings.sonarr.length - 1];
|
||
|
newSonarr.id = lastItem ? lastItem.id + 1 : 0;
|
||
|
|
||
4 years ago
|
// If we are setting this as the default, clear any previous defaults for the same type first
|
||
|
// ex: if is4k is true, it will only remove defaults for other servers that have is4k set to true
|
||
|
// and are the default
|
||
|
if (req.body.isDefault) {
|
||
|
settings.sonarr
|
||
|
.filter((sonarrInstance) => sonarrInstance.is4k === req.body.is4k)
|
||
|
.forEach((sonarrInstance) => {
|
||
|
sonarrInstance.isDefault = false;
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
settings.sonarr = [...settings.sonarr, newSonarr];
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(201).json(newSonarr);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.post('/sonarr/test', async (req, res, next) => {
|
||
|
try {
|
||
|
const sonarr = new SonarrAPI({
|
||
|
apiKey: req.body.apiKey,
|
||
|
url: `${req.body.useSsl ? 'https' : 'http'}://${req.body.hostname}:${
|
||
|
req.body.port
|
||
|
}${req.body.baseUrl ?? ''}/api`,
|
||
|
});
|
||
|
|
||
|
const profiles = await sonarr.getProfiles();
|
||
|
const folders = await sonarr.getRootFolders();
|
||
|
|
||
|
return res.status(200).json({
|
||
|
profiles,
|
||
|
rootFolders: folders.map((folder) => ({
|
||
|
id: folder.id,
|
||
|
path: folder.path,
|
||
|
})),
|
||
|
});
|
||
|
} catch (e) {
|
||
|
logger.error('Failed to test Sonarr', {
|
||
|
label: 'Sonarr',
|
||
|
message: e.message,
|
||
|
});
|
||
|
|
||
|
next({ status: 500, message: 'Failed to connect to Sonarr' });
|
||
|
}
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.put<{ id: string }>('/sonarr/:id', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const sonarrIndex = settings.sonarr.findIndex(
|
||
|
(r) => r.id === Number(req.params.id)
|
||
|
);
|
||
|
|
||
|
if (sonarrIndex === -1) {
|
||
|
return res
|
||
|
.status(404)
|
||
|
.json({ status: '404', message: 'Settings instance not found' });
|
||
|
}
|
||
|
|
||
4 years ago
|
// If we are setting this as the default, clear any previous defaults for the same type first
|
||
|
// ex: if is4k is true, it will only remove defaults for other servers that have is4k set to true
|
||
|
// and are the default
|
||
|
if (req.body.isDefault) {
|
||
|
settings.sonarr
|
||
|
.filter((sonarrInstance) => sonarrInstance.is4k === req.body.is4k)
|
||
|
.forEach((sonarrInstance) => {
|
||
|
sonarrInstance.isDefault = false;
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
settings.sonarr[sonarrIndex] = {
|
||
|
...req.body,
|
||
|
id: Number(req.params.id),
|
||
|
} as SonarrSettings;
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(settings.sonarr[sonarrIndex]);
|
||
|
});
|
||
|
|
||
|
settingsRoutes.delete<{ id: string }>('/sonarr/:id', (req, res) => {
|
||
|
const settings = getSettings();
|
||
|
|
||
|
const sonarrIndex = settings.sonarr.findIndex(
|
||
|
(r) => r.id === Number(req.params.id)
|
||
|
);
|
||
|
|
||
|
if (sonarrIndex === -1) {
|
||
|
return res
|
||
|
.status(404)
|
||
|
.json({ status: '404', message: 'Settings instance not found' });
|
||
|
}
|
||
|
|
||
|
const removed = settings.sonarr.splice(sonarrIndex, 1);
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(removed[0]);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get('/jobs', (_req, res) => {
|
||
4 years ago
|
return res.status(200).json(
|
||
|
scheduledJobs.map((job) => ({
|
||
|
name: job.name,
|
||
|
nextExecutionTime: job.job.nextInvocation(),
|
||
|
}))
|
||
|
);
|
||
|
});
|
||
|
|
||
4 years ago
|
settingsRoutes.get(
|
||
|
'/initialize',
|
||
|
isAuthenticated(Permission.ADMIN),
|
||
4 years ago
|
(_req, res) => {
|
||
4 years ago
|
const settings = getSettings();
|
||
|
|
||
|
settings.public.initialized = true;
|
||
|
settings.save();
|
||
|
|
||
|
return res.status(200).json(settings.public);
|
||
|
}
|
||
|
);
|
||
|
|
||
4 years ago
|
settingsRoutes.get('/about', async (req, res) => {
|
||
|
const mediaRepository = getRepository(Media);
|
||
|
const mediaRequestRepository = getRepository(MediaRequest);
|
||
|
|
||
|
const totalMediaItems = await mediaRepository.count();
|
||
|
const totalRequests = await mediaRequestRepository.count();
|
||
|
|
||
|
return res.status(200).json({
|
||
4 years ago
|
version: getAppVersion(),
|
||
4 years ago
|
totalMediaItems,
|
||
|
totalRequests,
|
||
4 years ago
|
tz: process.env.TZ,
|
||
|
} as SettingsAboutResponse);
|
||
4 years ago
|
});
|
||
|
|
||
4 years ago
|
export default settingsRoutes;
|