mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-12 00:15:48 +01:00
Merge branch 'dev' into ui/docker
This commit is contained in:
69
src/pages/api/modules/usenet/history.ts
Normal file
69
src/pages/api/modules/usenet/history.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
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';
|
||||
import { UsenetHistoryItem } from '../../../../modules';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
export interface UsenetHistoryRequestParams {
|
||||
serviceId: string;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface UsenetHistoryResponse {
|
||||
items: UsenetHistoryItem[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
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.`);
|
||||
}
|
||||
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
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,
|
||||
}));
|
||||
const response: UsenetHistoryResponse = {
|
||||
items,
|
||||
total: history.noofslots,
|
||||
};
|
||||
|
||||
return res.status(200).json(response);
|
||||
} 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 === 'GET') {
|
||||
return Get(req, res);
|
||||
}
|
||||
return res.status(405).json({
|
||||
statusCode: 405,
|
||||
message: 'Method not allowed',
|
||||
});
|
||||
};
|
||||
72
src/pages/api/modules/usenet/index.ts
Normal file
72
src/pages/api/modules/usenet/index.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
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';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
export interface UsenetInfoRequestParams {
|
||||
serviceId: string;
|
||||
}
|
||||
|
||||
export interface UsenetInfoResponse {
|
||||
paused: boolean;
|
||||
sizeLeft: number;
|
||||
speed: number;
|
||||
eta: number;
|
||||
}
|
||||
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const { serviceId } = req.query as any as UsenetInfoRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
const queue = await new Client(origin, service.apiKey).queue(0, -1);
|
||||
|
||||
const [hours, minutes, seconds] = queue.timeleft.split(':');
|
||||
const eta = dayjs.duration({
|
||||
hour: parseInt(hours, 10),
|
||||
minutes: parseInt(minutes, 10),
|
||||
seconds: parseInt(seconds, 10),
|
||||
} as any);
|
||||
|
||||
const response: UsenetInfoResponse = {
|
||||
paused: queue.paused,
|
||||
sizeLeft: parseFloat(queue.mbleft) * 1024 * 1024,
|
||||
speed: parseFloat(queue.kbpersec) * 1000,
|
||||
eta: eta.asSeconds(),
|
||||
};
|
||||
|
||||
return res.status(200).json(response);
|
||||
} 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 === 'GET') {
|
||||
return Get(req, res);
|
||||
}
|
||||
return res.status(405).json({
|
||||
statusCode: 405,
|
||||
message: 'Method not allowed',
|
||||
});
|
||||
};
|
||||
51
src/pages/api/modules/usenet/pause.ts
Normal file
51
src/pages/api/modules/usenet/pause.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
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';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
export interface UsenetPauseRequestParams {
|
||||
serviceId: string;
|
||||
}
|
||||
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const { serviceId } = req.query as any as UsenetPauseRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
const result = await new Client(origin, service.apiKey).queuePause();
|
||||
|
||||
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',
|
||||
});
|
||||
};
|
||||
81
src/pages/api/modules/usenet/queue.ts
Normal file
81
src/pages/api/modules/usenet/queue.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
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';
|
||||
import { UsenetQueueItem } from '../../../../modules';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
export interface UsenetQueueRequestParams {
|
||||
serviceId: string;
|
||||
offset: number;
|
||||
limit: number;
|
||||
}
|
||||
|
||||
export interface UsenetQueueResponse {
|
||||
items: UsenetQueueItem[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const { limit, offset, serviceId } = req.query as any as UsenetQueueRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
const queue = await new Client(origin, service.apiKey).queue(offset, limit);
|
||||
|
||||
const items: UsenetQueueItem[] = queue.slots.map((slot) => {
|
||||
const [hours, minutes, seconds] = slot.timeleft.split(':');
|
||||
const eta = dayjs.duration({
|
||||
hour: parseInt(hours, 10),
|
||||
minutes: parseInt(minutes, 10),
|
||||
seconds: parseInt(seconds, 10),
|
||||
} as any);
|
||||
|
||||
return {
|
||||
id: slot.nzo_id,
|
||||
eta: eta.asSeconds(),
|
||||
name: slot.filename,
|
||||
progress: parseFloat(slot.percentage),
|
||||
size: parseFloat(slot.mb) * 1000 * 1000,
|
||||
state: slot.status.toLowerCase() as any,
|
||||
};
|
||||
});
|
||||
|
||||
const response: UsenetQueueResponse = {
|
||||
items,
|
||||
total: queue.noofslots,
|
||||
};
|
||||
|
||||
return res.status(200).json(response);
|
||||
} 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 === 'GET') {
|
||||
return Get(req, res);
|
||||
}
|
||||
return res.status(405).json({
|
||||
statusCode: 405,
|
||||
message: 'Method not allowed',
|
||||
});
|
||||
};
|
||||
52
src/pages/api/modules/usenet/resume.ts
Normal file
52
src/pages/api/modules/usenet/resume.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
export interface UsenetResumeRequestParams {
|
||||
serviceId: string;
|
||||
nzbId?: string;
|
||||
}
|
||||
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const { serviceId } = req.query as any as UsenetResumeRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
const result = await new Client(origin, service.apiKey).queueResume();
|
||||
|
||||
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',
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user