Files
Homarr/src/pages/api/modules/usenet/history.ts

121 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-08-25 18:10:23 +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-08-25 18:47:06 +02:00
import { UsenetHistoryItem } from '../../../../modules';
import { getConfig } from '../../../../tools/getConfig';
2022-08-26 10:46:34 +02:00
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
2022-08-25 18:47:06 +02:00
import { Config } from '../../../../tools/types';
2022-11-06 10:05:35 -06:00
import { NzbgetHistoryItem } from './nzbget/types';
import { NzbgetClient } from './nzbget/nzbget-client';
2022-08-25 18:10:23 +02:00
dayjs.extend(duration);
2022-08-26 10:46:34 +02:00
export interface UsenetHistoryRequestParams {
serviceId: string;
offset: number;
limit: number;
}
export interface UsenetHistoryResponse {
items: UsenetHistoryItem[];
total: number;
}
2022-08-25 18:10:23 +02:00
async function Get(req: NextApiRequest, res: NextApiResponse) {
try {
const configName = getCookie('config-name', { req });
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
2022-08-26 10:46:34 +02:00
const { limit, offset, serviceId } = req.query as any as UsenetHistoryRequestParams;
const service = getServiceById(config, serviceId);
if (!service) {
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
}
2022-11-06 10:05:35 -06:00
let response: UsenetHistoryResponse;
switch (service.type) {
case 'NZBGet': {
const url = new URL(service.url);
const options = {
host: url.hostname,
port: url.port,
login: service.username,
hash: service.password,
};
const nzbGet = NzbgetClient(options);
2022-11-12 08:12:54 +09:00
const nzbgetHistory: NzbgetHistoryItem[] = await new Promise((resolve, reject) => {
2022-11-06 10:05:35 -06:00
nzbGet.history(false, (err: any, result: NzbgetHistoryItem[]) => {
if (!err) {
resolve(result);
} else {
reject(err);
}
});
});
if (!nzbgetHistory) {
throw new Error('Error while getting NZBGet history');
}
const nzbgetItems: UsenetHistoryItem[] = nzbgetHistory.map((item: NzbgetHistoryItem) => ({
id: item.NZBID.toString(),
name: item.Name,
// Convert from MB to bytes
size: item.DownloadedSizeMB * 1000000,
time: item.DownloadTimeSec,
}));
response = {
items: nzbgetItems,
total: nzbgetItems.length,
};
break;
}
case 'Sabnzbd': {
const { origin } = new URL(service.url);
if (!service.apiKey) {
throw new Error(`API Key for service "${service.name}" is missing`);
}
const history = await new Client(origin, service.apiKey).history(offset, limit);
const items: UsenetHistoryItem[] = history.slots.map((slot) => ({
id: slot.nzo_id,
name: slot.name,
size: slot.bytes,
time: slot.download_time,
}));
response = {
items,
total: history.noofslots,
};
break;
}
default:
throw new Error(`Service type "${service.type}" unrecognized.`);
2022-08-26 10:46:34 +02:00
}
return res.status(200).json(response);
2022-08-25 18:10:23 +02:00
} catch (err) {
2022-08-26 11:10:40 +02:00
return res.status(500).send((err as any).message);
2022-08-25 18:10:23 +02:00
}
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
// Filter out if the reuqest is a POST or a GET
if (req.method === 'GET') {
return Get(req, res);
}
return res.status(405).json({
statusCode: 405,
message: 'Method not allowed',
});
};