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-21 19:59:25 +02:00
|
|
|
const { config }: { config: Config } = req.body;
|
2022-06-21 19:16:29 +02:00
|
|
|
const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent');
|
2022-06-20 17:10:54 -04:00
|
|
|
const delugeServices = config.services.filter((service) => service.type === 'Deluge');
|
2022-06-21 19:16:29 +02:00
|
|
|
const transmissionServices = config.services.filter((service) => service.type === 'Transmission');
|
|
|
|
|
|
|
|
|
|
const torrents: NormalizedTorrent[] = [];
|
2022-06-20 17:10:54 -04:00
|
|
|
|
|
|
|
|
if (!qBittorrentServices && !delugeServices && !transmissionServices) {
|
2022-06-06 21:38:50 +02:00
|
|
|
return res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
2022-06-20 17:10:54 -04:00
|
|
|
message: 'Missing services',
|
2022-05-26 21:08:16 +02:00
|
|
|
});
|
|
|
|
|
}
|
2022-06-21 19:59:25 +02:00
|
|
|
await Promise.all(
|
|
|
|
|
qBittorrentServices.map((service) =>
|
|
|
|
|
new QBittorrent({
|
|
|
|
|
baseUrl: service.url,
|
|
|
|
|
username: service.username,
|
|
|
|
|
password: service.password,
|
|
|
|
|
})
|
|
|
|
|
.getAllData()
|
2022-06-21 21:04:21 +02:00
|
|
|
.then((e) => torrents.push(...e.torrents))
|
2022-06-21 19:59:25 +02:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
await Promise.all(
|
|
|
|
|
delugeServices.map((service) =>
|
|
|
|
|
new Deluge({
|
|
|
|
|
baseUrl: service.url,
|
2022-06-21 20:35:40 +02:00
|
|
|
password: 'password' in service ? service.password : '',
|
2022-06-21 19:59:25 +02:00
|
|
|
})
|
|
|
|
|
.getAllData()
|
2022-06-21 21:04:21 +02:00
|
|
|
.then((e) => torrents.push(...e.torrents))
|
2022-06-21 19:59:25 +02:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
// Map transmissionServices
|
|
|
|
|
await Promise.all(
|
|
|
|
|
transmissionServices.map((service) =>
|
|
|
|
|
new Transmission({
|
|
|
|
|
baseUrl: service.url,
|
2022-06-21 20:35:40 +02:00
|
|
|
username: 'username' in service ? service.username : '',
|
|
|
|
|
password: 'password' in service ? service.password : '',
|
2022-06-21 19:59:25 +02:00
|
|
|
})
|
|
|
|
|
.getAllData()
|
2022-06-21 21:04:21 +02:00
|
|
|
.then((e) => torrents.push(...e.torrents))
|
2022-06-21 19:59:25 +02:00
|
|
|
)
|
|
|
|
|
);
|
2022-06-06 21:38:50 +02:00
|
|
|
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',
|
|
|
|
|
});
|
|
|
|
|
};
|