2022-08-26 16:12:40 +02:00
|
|
|
import { getCookie } from 'cookies-next';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import duration from 'dayjs/plugin/duration';
|
|
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
import { Client } from 'sabnzbd-api';
|
2022-12-11 16:06:41 +01:00
|
|
|
import { getConfig } from '../../../../tools/config/getConfig';
|
2022-11-06 10:05:35 -06:00
|
|
|
import { NzbgetClient } from './nzbget/nzbget-client';
|
2022-08-26 16:12:40 +02:00
|
|
|
|
|
|
|
|
dayjs.extend(duration);
|
|
|
|
|
|
|
|
|
|
export interface UsenetPauseRequestParams {
|
|
|
|
|
serviceId: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
try {
|
|
|
|
|
const configName = getCookie('config-name', { req });
|
2022-12-11 16:06:41 +01:00
|
|
|
const config = getConfig(configName?.toString() ?? 'default');
|
2022-08-26 16:12:40 +02:00
|
|
|
const { serviceId } = req.query as any as UsenetPauseRequestParams;
|
|
|
|
|
|
2022-12-11 16:06:41 +01:00
|
|
|
const service = config.services.find((x) => x.id === serviceId);
|
2022-08-26 16:12:40 +02:00
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
|
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
let result;
|
2022-12-11 16:06:41 +01:00
|
|
|
switch (service.integration?.type) {
|
|
|
|
|
case 'nzbGet': {
|
2022-11-06 10:05:35 -06:00
|
|
|
const url = new URL(service.url);
|
|
|
|
|
const options = {
|
|
|
|
|
host: url.hostname,
|
|
|
|
|
port: url.port,
|
2022-12-11 19:33:54 +01:00
|
|
|
login:
|
|
|
|
|
service.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
|
|
|
|
|
hash:
|
|
|
|
|
service.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
|
2022-11-06 10:05:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const nzbGet = NzbgetClient(options);
|
2022-08-26 16:12:40 +02:00
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
result = await new Promise((resolve, reject) => {
|
|
|
|
|
nzbGet.pauseDownload(false, (err: any, result: any) => {
|
|
|
|
|
if (!err) {
|
|
|
|
|
resolve(result);
|
|
|
|
|
} else {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-12-11 16:06:41 +01:00
|
|
|
case 'sabnzbd': {
|
2022-12-11 19:33:54 +01:00
|
|
|
const apiKey = service.integration.properties.find((x) => x.field === 'apiKey')?.value;
|
|
|
|
|
if (!apiKey) {
|
2022-11-06 10:05:35 -06:00
|
|
|
throw new Error(`API Key for service "${service.name}" is missing`);
|
|
|
|
|
}
|
2022-08-31 19:13:34 +02:00
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
const { origin } = new URL(service.url);
|
|
|
|
|
|
2022-12-11 19:33:54 +01:00
|
|
|
result = await new Client(origin, apiKey).queuePause();
|
2022-11-06 10:05:35 -06:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2022-12-11 16:06:41 +01:00
|
|
|
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
2022-11-06 10:05:35 -06:00
|
|
|
}
|
2022-08-26 16:12:40 +02:00
|
|
|
|
|
|
|
|
return res.status(200).json(result);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return res.status(500).send((err as any).message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
// Filter out if the reuqest is a POST or a GET
|
|
|
|
|
if (req.method === 'POST') {
|
|
|
|
|
return Post(req, res);
|
|
|
|
|
}
|
|
|
|
|
return res.status(405).json({
|
|
|
|
|
statusCode: 405,
|
|
|
|
|
message: 'Method not allowed',
|
|
|
|
|
});
|
|
|
|
|
};
|