From 3c0b18570e3737549d96b1fe04cded54fc97f7af Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 10 Nov 2022 13:16:10 -0800 Subject: [PATCH] pyload widget compatibility with pyload-ng Closes https://github.com/benphelps/homepage/issues/517 --- src/widgets/pyload/proxy.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/widgets/pyload/proxy.js b/src/widgets/pyload/proxy.js index 86989ad05..80c83586e 100644 --- a/src/widgets/pyload/proxy.js +++ b/src/widgets/pyload/proxy.js @@ -9,6 +9,7 @@ import { httpProxy } from 'utils/proxy/http'; const proxyName = 'pyloadProxyHandler'; const logger = createLogger(proxyName); const sessionCacheKey = `${proxyName}__sessionId`; +const isNgCacheKey = `${proxyName}__isNg`; async function fetchFromPyloadAPI(url, sessionId, params) { const options = { @@ -23,17 +24,32 @@ async function fetchFromPyloadAPI(url, sessionId, params) { }, }; + // see https://github.com/benphelps/homepage/issues/517 + const isNg = cache.get(isNgCacheKey); + if (isNg && !params) { + delete options.body; + options.headers.Cookie = cache.get(sessionCacheKey); + } + // eslint-disable-next-line no-unused-vars - const [status, contentType, data] = await httpProxy(url, options); - return [status, JSON.parse(Buffer.from(data).toString())]; + const [status, contentType, data, responseHeaders] = await httpProxy(url, options); + return [status, JSON.parse(Buffer.from(data).toString()), responseHeaders]; } async function login(loginUrl, username, password = '') { - const [status, sessionId] = await fetchFromPyloadAPI(loginUrl, null, { username, password }); + const [status, sessionId, responseHeaders] = 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); + // Support pyload-ng, see https://github.com/benphelps/homepage/issues/517 + if (responseHeaders['set-cookie']?.join().includes('pyload_session')) { + cache.put(isNgCacheKey, true); + const sessionCookie = responseHeaders['set-cookie'][0]; + cache.put(sessionCacheKey, sessionCookie); + } else { + cache.put(sessionCacheKey, sessionId); + } + return sessionId; } }