2022-12-31 16:07:05 +01:00
|
|
|
import Consola from 'consola';
|
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-07-22 17:18:33 +02:00
|
|
|
import { getCookie } from 'cookies-next';
|
2022-05-26 18:18:30 +02:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
2022-12-21 20:29:09 +01:00
|
|
|
import { getConfig } from '../../../tools/config/getConfig';
|
2022-05-26 18:18:30 +02:00
|
|
|
|
|
|
|
|
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
2022-12-18 22:27:01 +01:00
|
|
|
// Get the type of app from the request url
|
2022-07-22 17:18:33 +02:00
|
|
|
const configName = getCookie('config-name', { req });
|
2022-12-21 20:29:09 +01:00
|
|
|
const config = getConfig(configName?.toString() ?? 'default');
|
|
|
|
|
const qBittorrentApp = config.apps.filter((app) => app.integration?.type === 'qBittorrent');
|
|
|
|
|
const delugeApp = config.apps.filter((app) => app.integration?.type === 'deluge');
|
|
|
|
|
const transmissionApp = config.apps.filter((app) => app.integration?.type === 'transmission');
|
2022-06-21 19:16:29 +02:00
|
|
|
|
|
|
|
|
const torrents: NormalizedTorrent[] = [];
|
2022-06-20 17:10:54 -04:00
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
if (!qBittorrentApp && !delugeApp && !transmissionApp) {
|
2022-06-06 21:38:50 +02:00
|
|
|
return res.status(500).json({
|
|
|
|
|
statusCode: 500,
|
2022-12-18 22:27:01 +01:00
|
|
|
message: 'Missing apps',
|
2022-05-26 21:08:16 +02:00
|
|
|
});
|
|
|
|
|
}
|
2022-12-31 16:07:05 +01:00
|
|
|
|
2022-07-22 17:18:33 +02:00
|
|
|
try {
|
|
|
|
|
await Promise.all(
|
2022-12-21 20:29:09 +01:00
|
|
|
qBittorrentApp.map((app) =>
|
2022-07-22 17:18:33 +02:00
|
|
|
new QBittorrent({
|
2022-12-21 20:29:09 +01:00
|
|
|
baseUrl: app.url,
|
2022-12-31 16:07:05 +01:00
|
|
|
username:
|
|
|
|
|
app.integration!.properties.find((x) => x.field === 'username')?.value ?? undefined,
|
|
|
|
|
password:
|
|
|
|
|
app.integration!.properties.find((x) => x.field === 'password')?.value ?? undefined,
|
2022-07-22 17:18:33 +02:00
|
|
|
})
|
|
|
|
|
.getAllData()
|
|
|
|
|
.then((e) => torrents.push(...e.torrents))
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
await Promise.all(
|
2022-12-31 16:07:05 +01:00
|
|
|
delugeApp.map((app) => {
|
2023-01-06 01:11:02 +09:00
|
|
|
const password =
|
|
|
|
|
app.integration?.properties.find((x) => x.field === 'password')?.value ?? undefined;
|
2022-12-31 16:07:05 +01:00
|
|
|
const test = new Deluge({
|
2022-12-21 20:29:09 +01:00
|
|
|
baseUrl: app.url,
|
2022-12-31 16:07:05 +01:00
|
|
|
password,
|
2022-07-22 17:18:33 +02:00
|
|
|
})
|
|
|
|
|
.getAllData()
|
2022-12-31 16:07:05 +01:00
|
|
|
.then((e) => torrents.push(...e.torrents));
|
|
|
|
|
return test;
|
|
|
|
|
})
|
2022-07-22 17:18:33 +02:00
|
|
|
);
|
2022-12-18 22:27:01 +01:00
|
|
|
// Map transmissionApps
|
2022-07-22 17:18:33 +02:00
|
|
|
await Promise.all(
|
2022-12-21 20:29:09 +01:00
|
|
|
transmissionApp.map((app) =>
|
2022-07-22 17:18:33 +02:00
|
|
|
new Transmission({
|
2022-12-21 20:29:09 +01:00
|
|
|
baseUrl: app.url,
|
2022-12-31 16:07:05 +01:00
|
|
|
username:
|
|
|
|
|
app.integration!.properties.find((x) => x.field === 'username')?.value ?? undefined,
|
|
|
|
|
password:
|
|
|
|
|
app.integration!.properties.find((x) => x.field === 'password')?.value ?? undefined,
|
2022-07-22 17:18:33 +02:00
|
|
|
})
|
|
|
|
|
.getAllData()
|
|
|
|
|
.then((e) => torrents.push(...e.torrents))
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
} catch (e: any) {
|
2022-12-31 16:07:05 +01:00
|
|
|
Consola.error('Error while communicating with your torrent applications:\n', e);
|
2022-07-22 17:18:33 +02:00
|
|
|
return res.status(401).json(e);
|
|
|
|
|
}
|
2022-12-31 16:07:05 +01:00
|
|
|
|
|
|
|
|
Consola.debug(`Retrieved ${torrents.length} from all download clients`);
|
2022-07-22 17:18:33 +02:00
|
|
|
return 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',
|
|
|
|
|
});
|
|
|
|
|
};
|