Skip to content

Commit

Permalink
Merge pull request #1519 from theshaun/main
Browse files Browse the repository at this point in the history
QNAP plugin, fix key expiry issue
  • Loading branch information
shamoon authored May 19, 2023
2 parents 4aeb388 + a9d3873 commit 6046f79
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/widgets/qnap/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,23 @@ async function apiCall(widget, endpoint, service) {
return { status, contentType, data: null, responseHeaders };
}

const dataDecoded = xml2json(data.toString(), { compact: true });
return { status, contentType, data: JSON.parse(dataDecoded.toString()), responseHeaders };
let dataDecoded = JSON.parse(xml2json(data.toString(), { compact: true }).toString());

if (dataDecoded.QDocRoot.authPassed._cdata === '0') {
logger.error("QNAP API rejected the request, attempting to obtain new session token");
key = await login(widget, service);
apiUrl = new URL(formatApiCall(`${endpoint}&sid=${key}`, widget));
[status, contentType, data, responseHeaders] = await httpProxy(apiUrl);

if (status !== 200) {
logger.error("Error getting data from QNAP: %s status %d. Data: %s", apiUrl, status, data);
return { status, contentType, data: null, responseHeaders };
}

dataDecoded = JSON.parse(xml2json(data.toString(), { compact: true }).toString());
}

return { status, contentType, data: dataDecoded, responseHeaders };
}

export default async function qnapProxyHandler(req, res) {
Expand Down

0 comments on commit 6046f79

Please sign in to comment.