2022-05-26 21:08:16 +02:00
|
|
|
import { Deluge } from '@ctrl/deluge';
|
2022-05-26 18:18:30 +02:00
|
|
|
import { QBittorrent } from '@ctrl/qbittorrent';
|
2022-06-06 21:38:50 +02:00
|
|
|
import { NormalizedTorrent } from '@ctrl/shared-torrent';
|
|
|
|
|
import { Transmission } from '@ctrl/transmission';
|
2022-05-26 18:18:30 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
2022-06-06 21:38:50 +02:00
|
|
|
import { Config } from '../../../tools/types';
|
2022-05-26 18:18:30 +02:00
|
|
|
|
|
|
|
|
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
2022-05-26 21:08:16 +02:00
|
|
|
// Get the type of service from the request url
|
2022-06-06 21:38:50 +02:00
|
|
|
const torrents: NormalizedTorrent[] = [];
|
|
|
|
|
const { config }: { config: Config } = req.body;
|
|
|
|
|
const qBittorrentService = config.services
|
|
|
|
|
.filter((service) => service.type === 'qBittorrent')
|
|
|
|
|
.at(0);
|
|
|
|
|
const delugeService = config.services.filter((service) => service.type === 'Deluge').at(0);
|
|
|
|
|
const transmissionService = config.services
|
|
|
|
|
.filter((service) => service.type === 'Transmission')
|
|
|
|
|
.at(0);
|
|
|
|
|
if (!qBittorrentService && !delugeService && !transmissionService) {
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
|
|
|
|
message: 'Missing service',
|
2022-05-26 21:08:16 +02:00
|
|
|
});
|
|
|
|
|
}
|
2022-06-06 21:38:50 +02:00
|
|
|
if (qBittorrentService) {
|
|
|
|
|
torrents.push(
|
|
|
|
|
...(
|
|
|
|
|
await new QBittorrent({
|
|
|
|
|
baseUrl: qBittorrentService.url,
|
|
|
|
|
username: qBittorrentService.username,
|
|
|
|
|
password: qBittorrentService.password,
|
|
|
|
|
}).getAllData()
|
|
|
|
|
).torrents
|
|
|
|
|
);
|
2022-05-26 21:08:16 +02:00
|
|
|
}
|
2022-06-06 21:38:50 +02:00
|
|
|
if (delugeService) {
|
|
|
|
|
const delugeTorrents = (
|
|
|
|
|
await new Deluge({
|
|
|
|
|
baseUrl: delugeService.url,
|
|
|
|
|
username: delugeService.username,
|
|
|
|
|
password: delugeService.password,
|
|
|
|
|
}).getAllData()
|
|
|
|
|
).torrents;
|
|
|
|
|
delugeTorrents.forEach((delugeTorrent) =>
|
|
|
|
|
torrents.push({ ...delugeTorrent, progress: delugeTorrent.progress / 100 })
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (transmissionService) {
|
|
|
|
|
torrents.push(
|
|
|
|
|
...(
|
|
|
|
|
await new Transmission({
|
|
|
|
|
baseUrl: transmissionService.url,
|
|
|
|
|
username: transmissionService.username,
|
|
|
|
|
password: transmissionService.password,
|
|
|
|
|
}).getAllData()
|
|
|
|
|
).torrents
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
res.status(200).json(torrents);
|
2022-05-26 18:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
});
|
|
|
|
|
};
|