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';
|
|
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
|
|
|
|
|
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
2022-05-26 21:08:16 +02:00
|
|
|
// Get the type of service from the request url
|
|
|
|
|
const { dlclient } = req.query;
|
2022-05-26 18:18:30 +02:00
|
|
|
const { body } = req;
|
|
|
|
|
// Get login, password and url from the body
|
|
|
|
|
const { username, password, url } = body;
|
2022-05-26 21:08:16 +02:00
|
|
|
if (!dlclient || (!username && !password) || !url) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error: 'Wrong request',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
let client: Deluge | QBittorrent;
|
|
|
|
|
switch (dlclient) {
|
|
|
|
|
case 'qbit':
|
|
|
|
|
client = new QBittorrent({
|
|
|
|
|
baseUrl: new URL(url).origin,
|
|
|
|
|
username,
|
|
|
|
|
password,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case 'deluge':
|
|
|
|
|
client = new Deluge({
|
|
|
|
|
baseUrl: new URL(url).origin,
|
|
|
|
|
password,
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error: 'Wrong request',
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-05-26 18:18:30 +02:00
|
|
|
const data = await client.getAllData();
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
torrents: data.torrents,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
});
|
|
|
|
|
};
|