- Update http.js to support writing request bodies - Update http.js to support returning all response headers resolves: #104pull/141/head
parent
406358aae9
commit
b3db549a65
@ -0,0 +1,69 @@
|
||||
import useSWR from "swr";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import Widget from "../widget";
|
||||
import Block from "../block";
|
||||
|
||||
import { formatApiUrl } from "utils/api-helpers";
|
||||
|
||||
export default function Transmission({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const config = service.widget;
|
||||
|
||||
const { data: torrentData, error: torrentError } = useSWR(formatApiUrl(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.torrents;
|
||||
let rateDl = 0;
|
||||
let rateUl = 0;
|
||||
let completed = 0;
|
||||
|
||||
for (let torrent of torrents) {
|
||||
rateDl += torrent.rateDownload;
|
||||
rateUl += torrent.rateUpload;
|
||||
if (torrent.percentDone === 1) {
|
||||
completed++;
|
||||
}
|
||||
}
|
||||
|
||||
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={leech} />
|
||||
<Block label={t("transmission.download")} value={`${rateDl.toFixed(2)} ${unitsDl}`} />
|
||||
<Block label={t("transmission.seed")} value={completed} />
|
||||
<Block label={t("transmission.upload")} value={`${rateUl.toFixed(2)} ${unitsUl}`} />
|
||||
</Widget>
|
||||
);
|
||||
}
|
@ -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 body = JSON.stringify({
|
||||
method: "torrent-get",
|
||||
arguments: {
|
||||
fields: ["percentDone", "status", "rateDownload", "rateUpload"]
|
||||
}
|
||||
});
|
||||
|
||||
const reqHeaders = {
|
||||
"content-type": "application/json",
|
||||
};
|
||||
|
||||
let [status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||
method: method,
|
||||
auth: `${widget.username}:${widget.password}`,
|
||||
body: body,
|
||||
headers: reqHeaders,
|
||||
});
|
||||
|
||||
if (status === 409) {
|
||||
// Transmission is rejecting the request, but returning a CSRF token
|
||||
reqHeaders[csrfHeaderName] = responseHeaders[csrfHeaderName];
|
||||
|
||||
// retry the request, now with the CSRF token
|
||||
[status, contentType, data] = await httpProxy(url, {
|
||||
method: method,
|
||||
auth: `${widget.username}:${widget.password}`,
|
||||
body: body,
|
||||
headers: reqHeaders,
|
||||
});
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
Loading…
Reference in new issue