Merge pull request #586 from benphelps/add-diskstation
Add Synology DiskStation widgetpull/607/head
commit
537b1b344d
@ -0,0 +1,41 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
const { data: listData, error: listError } = useWidgetAPI(widget, "list");
|
||||
|
||||
if (listError) {
|
||||
return <Container error={listError} />;
|
||||
}
|
||||
|
||||
const tasks = listData?.data?.tasks;
|
||||
if (!tasks) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="diskstation.leech" />
|
||||
<Block label="diskstation.download" />
|
||||
<Block label="diskstation.seed" />
|
||||
<Block label="diskstation.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const rateDl = tasks.reduce((acc, task) => acc + (task?.additional?.transfer?.speed_download ?? 0), 0);
|
||||
const rateUl = tasks.reduce((acc, task) => acc + (task?.additional?.transfer?.speed_upload ?? 0), 0);
|
||||
const completed = tasks.filter((task) => task?.additional?.transfer?.size_downloaded === task?.size)?.length || 0;
|
||||
const leech = tasks.length - completed || 0;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="diskstation.leech" value={t("common.number", { value: leech })} />
|
||||
<Block label="diskstation.download" value={t("common.bitrate", { value: rateDl })} />
|
||||
<Block label="diskstation.seed" value={t("common.number", { value: completed })} />
|
||||
<Block label="diskstation.upload" value={t("common.bitrate", { value: rateUl })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
|
||||
const logger = createLogger("diskstationProxyHandler");
|
||||
const authApi = "{url}/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account={username}&passwd={password}&session=DownloadStation&format=cookie"
|
||||
|
||||
async function login(widget) {
|
||||
const loginUrl = formatApiCall(authApi, widget);
|
||||
const [status, contentType, data] = await httpProxy(loginUrl);
|
||||
if (status !== 200) {
|
||||
return [status, contentType, data];
|
||||
}
|
||||
|
||||
const json = JSON.parse(data.toString());
|
||||
if (json?.success !== true) {
|
||||
// from https://global.download.synology.com/download/Document/Software/DeveloperGuide/Package/DownloadStation/All/enu/Synology_Download_Station_Web_API.pdf
|
||||
/*
|
||||
Code Description
|
||||
400 No such account or incorrect password
|
||||
401 Account disabled
|
||||
402 Permission denied
|
||||
403 2-step verification code required
|
||||
404 Failed to authenticate 2-step verification code
|
||||
*/
|
||||
let message = "Authentication failed.";
|
||||
if (json?.error?.code >= 403) message += " 2FA enabled.";
|
||||
logger.warn("Unable to login. Code: %d", json?.error?.code);
|
||||
return [401, "application/json", JSON.stringify({ code: json?.error?.code, message })];
|
||||
}
|
||||
|
||||
return [status, contentType, data];
|
||||
}
|
||||
|
||||
export default async function diskstationProxyHandler(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);
|
||||
const api = widgets?.[widget.type]?.api;
|
||||
if (!api) {
|
||||
return res.status(403).json({ error: "Service does not support API calls" });
|
||||
}
|
||||
|
||||
const url = formatApiCall(api, { endpoint, ...widget });
|
||||
let [status, contentType, data] = await httpProxy(url);
|
||||
if (status !== 200) {
|
||||
logger.debug("Error %d calling endpoint %s", status, url);
|
||||
return res.status(status, data);
|
||||
}
|
||||
|
||||
const json = JSON.parse(data.toString());
|
||||
if (json?.success !== true) {
|
||||
logger.debug("Logging in to DiskStation");
|
||||
[status, contentType, data] = await login(widget);
|
||||
if (status !== 200) {
|
||||
return res.status(status).end(data)
|
||||
}
|
||||
|
||||
[status, contentType, data] = await httpProxy(url);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import diskstationProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=1&method={endpoint}",
|
||||
proxyHandler: diskstationProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"list": {
|
||||
endpoint: "list&additional=transfer",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue