mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-08 14:38:47 +00:00
Run pre-commit hooks over existing codebase
Co-Authored-By: Ben Phelps <ben@phelps.io>
This commit is contained in:
parent
fa50bbad9c
commit
19c25713c4
387 changed files with 4785 additions and 4109 deletions
|
@ -1,12 +1,12 @@
|
|||
import cache from 'memory-cache';
|
||||
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';
|
||||
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 proxyName = "pyloadProxyHandler";
|
||||
const logger = createLogger(proxyName);
|
||||
const sessionCacheKey = `${proxyName}__sessionId`;
|
||||
const isNgCacheKey = `${proxyName}__isNg`;
|
||||
|
@ -16,11 +16,11 @@ async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
|||
body: params
|
||||
? Object.keys(params)
|
||||
.map((prop) => `${prop}=${params[prop]}`)
|
||||
.join('&')
|
||||
.join("&")
|
||||
: `session=${sessionId}`,
|
||||
method: 'POST',
|
||||
method: "POST",
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -36,23 +36,28 @@ async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
|||
let returnData;
|
||||
try {
|
||||
returnData = JSON.parse(Buffer.from(data).toString());
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
logger.error(`Error logging into pyload API: ${JSON.stringify(data)}`);
|
||||
returnData = data;
|
||||
}
|
||||
return [status, returnData, responseHeaders];
|
||||
}
|
||||
|
||||
async function login(loginUrl, service, username, password = '') {
|
||||
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(loginUrl, null, { username, password }, service);
|
||||
|
||||
async function login(loginUrl, service, username, password = "") {
|
||||
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(
|
||||
loginUrl,
|
||||
null,
|
||||
{ username, password },
|
||||
service,
|
||||
);
|
||||
|
||||
// this API actually returns status 200 even on login failure
|
||||
if (status !== 200 || sessionId === false) {
|
||||
logger.error(`HTTP ${status} logging into Pyload API, returned: ${JSON.stringify(sessionId)}`);
|
||||
} else if (responseHeaders['set-cookie']?.join().includes('pyload_session')) {
|
||||
} else if (responseHeaders["set-cookie"]?.join().includes("pyload_session")) {
|
||||
// Support pyload-ng, see https://github.com/gethomepage/homepage/issues/517
|
||||
cache.put(`${isNgCacheKey}.${service}`, true);
|
||||
const sessionCookie = responseHeaders['set-cookie'][0];
|
||||
const sessionCookie = responseHeaders["set-cookie"][0];
|
||||
cache.put(`${sessionCacheKey}.${service}`, sessionCookie, 60 * 60 * 23 * 1000); // cache for 23h
|
||||
} else {
|
||||
cache.put(`${sessionCacheKey}.${service}`, sessionId);
|
||||
|
@ -72,11 +77,13 @@ export default async function pyloadProxyHandler(req, res) {
|
|||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
const loginUrl = `${widget.url}/api/login`;
|
||||
|
||||
let sessionId = cache.get(`${sessionCacheKey}.${service}`) ?? await login(loginUrl, service, widget.username, widget.password);
|
||||
let sessionId =
|
||||
cache.get(`${sessionCacheKey}.${service}`) ??
|
||||
(await login(loginUrl, service, widget.username, widget.password));
|
||||
let [status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
||||
|
||||
if (status === 403 || status === 401) {
|
||||
logger.info('Failed to retrieve data from Pyload API, trying to login again...');
|
||||
logger.info("Failed to retrieve data from Pyload API, trying to login again...");
|
||||
cache.del(`${sessionCacheKey}.${service}`);
|
||||
sessionId = await login(loginUrl, service, widget.username, widget.password);
|
||||
[status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
||||
|
@ -84,9 +91,11 @@ export default async function pyloadProxyHandler(req, res) {
|
|||
|
||||
if (data?.error || status !== 200) {
|
||||
try {
|
||||
return res.status(status).send({error: {message: "HTTP error communicating with Pyload API", data: Buffer.from(data).toString()}});
|
||||
return res.status(status).send({
|
||||
error: { message: "HTTP error communicating with Pyload API", data: Buffer.from(data).toString() },
|
||||
});
|
||||
} catch (e) {
|
||||
return res.status(status).send({error: {message: "HTTP error communicating with Pyload API", data}});
|
||||
return res.status(status).send({ error: { message: "HTTP error communicating with Pyload API", data } });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,8 +104,8 @@ export default async function pyloadProxyHandler(req, res) {
|
|||
}
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
return res.status(500).send({error: {message: `Error communicating with Pyload API: ${e.toString()}`}});
|
||||
return res.status(500).send({ error: { message: `Error communicating with Pyload API: ${e.toString()}` } });
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: 'Invalid proxy service type' });
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue