diff --git a/public/locales/en/common.json b/public/locales/en/common.json index afba679a..46a69588 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -319,5 +319,11 @@ "uptime": "Uptime", "alerts": "Alerts", "time": "{{value, number(style: unit; unitDisplay: long;)}}" + }, + "pyload": { + "speed": "Speed", + "active": "Active", + "queue": "Queue", + "total": "Total" } } diff --git a/src/widgets/components.js b/src/widgets/components.js index 28b5a92f..f501e012 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -26,6 +26,7 @@ const components = { portainer: dynamic(() => import("./portainer/component")), prowlarr: dynamic(() => import("./prowlarr/component")), proxmox: dynamic(() => import("./proxmox/component")), + pyload: dynamic(() => import("./pyload/component")), qbittorrent: dynamic(() => import("./qbittorrent/component")), radarr: dynamic(() => import("./radarr/component")), readarr: dynamic(() => import("./readarr/component")), diff --git a/src/widgets/pyload/component.jsx b/src/widgets/pyload/component.jsx new file mode 100644 index 00000000..958733c3 --- /dev/null +++ b/src/widgets/pyload/component.jsx @@ -0,0 +1,35 @@ +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: pyloadData, error: pyloadError } = useWidgetAPI(widget, "status"); + + if (pyloadError || pyloadData?.error) { + return ; + } + + if (!pyloadData) { + return ( + + + + + + + ); + } + + return ( + + + + + + + ); +} diff --git a/src/widgets/pyload/proxy.js b/src/widgets/pyload/proxy.js new file mode 100644 index 00000000..86989ad0 --- /dev/null +++ b/src/widgets/pyload/proxy.js @@ -0,0 +1,75 @@ +import cache from 'memory-cache'; + +import getServiceWidget from 'utils/config/service-helpers'; +import { formatApiCall } from 'utils/proxy/api-helpers'; +import widgets from 'widgets/widgets'; +import createLogger from 'utils/logger'; +import { httpProxy } from 'utils/proxy/http'; + +const proxyName = 'pyloadProxyHandler'; +const logger = createLogger(proxyName); +const sessionCacheKey = `${proxyName}__sessionId`; + +async function fetchFromPyloadAPI(url, sessionId, params) { + const options = { + body: params + ? Object.keys(params) + .map((prop) => `${prop}=${params[prop]}`) + .join('&') + : `session=${sessionId}`, + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }; + + // eslint-disable-next-line no-unused-vars + const [status, contentType, data] = await httpProxy(url, options); + return [status, JSON.parse(Buffer.from(data).toString())]; +} + +async function login(loginUrl, username, password = '') { + const [status, sessionId] = await fetchFromPyloadAPI(loginUrl, null, { username, password }); + if (status !== 200) { + throw new Error(`HTTP error ${status} logging into Pyload API, returned: ${sessionId}`); + } else { + cache.put(sessionCacheKey, sessionId); + return sessionId; + } +} + +export default async function pyloadProxyHandler(req, res) { + const { group, service, endpoint } = req.query; + + try { + if (group && service) { + const widget = await getServiceWidget(group, service); + + if (widget) { + const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget })); + const loginUrl = `${widget.url}/api/login`; + + let sessionId = cache.get(sessionCacheKey) ?? await login(loginUrl, widget.username, widget.password); + let [status, data] = await fetchFromPyloadAPI(url, sessionId); + + if (status === 403) { + logger.debug('Failed to retrieve data from Pyload API, login and re-try'); + cache.del(sessionCacheKey); + sessionId = await login(loginUrl, widget.username, widget.password); + [status, data] = await fetchFromPyloadAPI(url, sessionId); + } + + if (data?.error || status !== 200) { + return res.status(500).send(Buffer.from(data).toString()); + } + + return res.json(data); + } + } + } catch (e) { + logger.error(e); + return res.status(500).send(e.toString()); + } + + return res.status(400).json({ error: 'Invalid proxy service type' }); +} diff --git a/src/widgets/pyload/widget.js b/src/widgets/pyload/widget.js new file mode 100644 index 00000000..71073c0f --- /dev/null +++ b/src/widgets/pyload/widget.js @@ -0,0 +1,14 @@ +import pyloadProxyHandler from "./proxy"; + +const widget = { + api: "{url}/api/{endpoint}", + proxyHandler: pyloadProxyHandler, + + mappings: { + "status": { + endpoint: "statusServer", + } + } +} + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index aaf0a025..fd67357d 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -21,6 +21,7 @@ import plex from "./plex/widget"; import portainer from "./portainer/widget"; import prowlarr from "./prowlarr/widget"; import proxmox from "./proxmox/widget"; +import pyload from "./pyload/widget"; import qbittorrent from "./qbittorrent/widget"; import radarr from "./radarr/widget"; import readarr from "./readarr/widget"; @@ -62,6 +63,7 @@ const widgets = { portainer, prowlarr, proxmox, + pyload, qbittorrent, radarr, readarr,