widget refactoring

This commit is contained in:
Ben Phelps 2022-09-26 00:35:54 +03:00
parent 03fa2f86d7
commit 035dd25ece
29 changed files with 851 additions and 10 deletions

View file

@ -0,0 +1,69 @@
import useSWR from "swr";
import { useTranslation } from "next-i18next";
import Widget from "components/services/widgets/widget";
import Block from "components/services/widgets/block";
import { formatProxyUrl } from "utils/api-helpers";
export default function Component({ service }) {
const { t } = useTranslation();
const config = service.widget;
const { data: torrentData, error: torrentError } = useSWR(formatProxyUrl(config));
if (torrentError) {
return <Widget error={t("widget.api_error")} />;
}
if (!torrentData) {
return (
<Widget>
<Block label={t("transmission.leech")} />
<Block label={t("transmission.download")} />
<Block label={t("transmission.seed")} />
<Block label={t("transmission.upload")} />
</Widget>
);
}
const { torrents } = torrentData.arguments;
let rateDl = 0;
let rateUl = 0;
let completed = 0;
for (let i = 0; i < torrents.length; i += 1) {
const torrent = torrents[i];
rateDl += torrent.rateDownload;
rateUl += torrent.rateUpload;
if (torrent.percentDone === 1) {
completed += 1;
}
}
const leech = torrents.length - completed;
let unitsDl = "KB/s";
let unitsUl = "KB/s";
rateDl /= 1024;
rateUl /= 1024;
if (rateDl > 1024) {
rateDl /= 1024;
unitsDl = "MB/s";
}
if (rateUl > 1024) {
rateUl /= 1024;
unitsUl = "MB/s";
}
return (
<Widget>
<Block label={t("transmission.leech")} value={t("common.number", { value: leech })} />
<Block label={t("transmission.download")} value={`${rateDl.toFixed(2)} ${unitsDl}`} />
<Block label={t("transmission.seed")} value={t("common.number", { value: completed })} />
<Block label={t("transmission.upload")} value={`${rateUl.toFixed(2)} ${unitsUl}`} />
</Widget>
);
}

View file

@ -0,0 +1,56 @@
import { httpProxy } from "utils/http";
import { formatApiCall } from "utils/api-helpers";
import getServiceWidget from "utils/service-helpers";
export default async function transmissionProxyHandler(req, res) {
const { group, service, endpoint } = req.query;
if (!group || !service) {
return res.status(400).json({ error: "Invalid proxy service type" });
}
const widget = await getServiceWidget(group, service);
if (!widget) {
return res.status(400).json({ error: "Invalid proxy service type" });
}
const url = new URL(formatApiCall(widget.type, { endpoint, ...widget }));
const csrfHeaderName = "x-transmission-session-id";
const method = "POST";
const auth = `${widget.username}:${widget.password}`;
const body = JSON.stringify({
method: "torrent-get",
arguments: {
fields: ["percentDone", "status", "rateDownload", "rateUpload"]
}
});
const headers = {
"content-type": "application/json",
};
let [status, contentType, data, responseHeaders] = await httpProxy(url, {
method,
auth,
body,
headers,
});
if (status === 409) {
// Transmission is rejecting the request, but returning a CSRF token
headers[csrfHeaderName] = responseHeaders[csrfHeaderName];
// retry the request, now with the CSRF token
[status, contentType, data, responseHeaders] = await httpProxy(url, {
method,
auth,
body,
headers,
});
}
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
}

View file

@ -0,0 +1,8 @@
import transmissionProxyHandler from "./proxy";
const widget = {
api: "{url}/transmission/rpc",
proxyHandler: transmissionProxyHandler,
};
export default widget;