Files
Homarr/src/pages/api/modules/torrents.ts

87 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-12-31 16:07:05 +01:00
import Consola from 'consola';
import { Deluge } from '@ctrl/deluge';
import { QBittorrent } from '@ctrl/qbittorrent';
import { NormalizedTorrent } from '@ctrl/shared-torrent';
import { Transmission } from '@ctrl/transmission';
import { getCookie } from 'cookies-next';
import { NextApiRequest, NextApiResponse } from 'next';
import { getConfig } from '../../../tools/config/getConfig';
async function Post(req: NextApiRequest, res: NextApiResponse) {
// Get the type of app from the request url
const configName = getCookie('config-name', { req });
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');
const torrents: NormalizedTorrent[] = [];
if (!qBittorrentApp && !delugeApp && !transmissionApp) {
return res.status(500).json({
statusCode: 500,
message: 'Missing apps',
});
}
2022-12-31 16:07:05 +01:00
try {
await Promise.all(
qBittorrentApp.map((app) =>
new QBittorrent({
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,
})
.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({
baseUrl: app.url,
2022-12-31 16:07:05 +01:00
password,
})
.getAllData()
2022-12-31 16:07:05 +01:00
.then((e) => torrents.push(...e.torrents));
return test;
})
);
// Map transmissionApps
await Promise.all(
transmissionApp.map((app) =>
new Transmission({
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,
})
.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);
return res.status(401).json(e);
}
2022-12-31 16:07:05 +01:00
Consola.debug(`Retrieved ${torrents.length} from all download clients`);
return res.status(200).json(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',
});
};