diff --git a/app/api/nextcloud/status/route.ts b/app/api/nextcloud/status/route.ts index fd7e4f4d..17cb17ce 100644 --- a/app/api/nextcloud/status/route.ts +++ b/app/api/nextcloud/status/route.ts @@ -8,6 +8,31 @@ async function sleep(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); } +async function parseXMLResponse(response: Response): Promise { + const text = await response.text(); + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(text, 'text/xml'); + + // Convert XML to a simple object + const result: any = {}; + const root = xmlDoc.documentElement; + + if (root && root.nodeName === 'ocs') { + const data = root.getElementsByTagName('data')[0]; + if (data) { + const children = data.childNodes; + for (let i = 0; i < children.length; i++) { + const child = children[i]; + if (child.nodeType === 1) { // Element node + result[child.nodeName] = child.textContent; + } + } + } + } + + return result; +} + async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) { // First, try to get the user's WebDAV password const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, { @@ -22,8 +47,8 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi return null; } - const userInfo = await userInfoResponse.json(); - const webdavPassword = userInfo.ocs.data?.webdav_password; + const userInfo = await parseXMLResponse(userInfoResponse); + const webdavPassword = userInfo.webdav_password; if (!webdavPassword) { // If no WebDAV password exists, create one @@ -58,8 +83,8 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi return null; } - const newUserInfo = await newUserInfoResponse.json(); - return newUserInfo.ocs.data?.webdav_password; + const newUserInfo = await parseXMLResponse(newUserInfoResponse); + return newUserInfo.webdav_password; } return webdavPassword;