Skip to content

Commit

Permalink
pyload widget compatibility with pyload-ng
Browse files Browse the repository at this point in the history
Closes #517
  • Loading branch information
shamoon committed Nov 10, 2022
1 parent eff2f1a commit 3c0b185
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/widgets/pyload/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 3c0b185

Please sign in to comment.