carnet api

This commit is contained in:
alma 2025-04-20 14:28:00 +02:00
parent 576b9e7fc9
commit 9b039cb6fd

View File

@ -10,10 +10,18 @@ async function sleep(ms: number) {
async function parseXMLResponse(response: Response): Promise<any> {
const text = await response.text();
console.log('XML Response:', text); // Debug log
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, 'text/xml');
// Convert XML to a simple object
// Check for parsing errors
const parserError = xmlDoc.getElementsByTagName('parsererror');
if (parserError.length > 0) {
console.error('XML Parsing Error:', parserError[0].textContent);
throw new Error('Failed to parse XML response');
}
const result: any = {};
const root = xmlDoc.documentElement;
@ -34,6 +42,7 @@ async function parseXMLResponse(response: Response): Promise<any> {
}
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) {
try {
// First, try to get the user's WebDAV password
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
headers: {
@ -44,14 +53,19 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
if (!userInfoResponse.ok) {
console.error('Failed to get user info:', await userInfoResponse.text());
return null;
throw new Error(`Failed to get user info: ${userInfoResponse.status} ${userInfoResponse.statusText}`);
}
const userInfo = await parseXMLResponse(userInfoResponse);
console.log('User Info:', userInfo); // Debug log
const webdavPassword = userInfo.webdav_password;
if (!webdavPassword) {
// If no WebDAV password exists, create one
const randomPassword = Math.random().toString(36).slice(-8);
console.log('Creating new WebDAV password:', randomPassword); // Debug log
const createPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
method: 'PUT',
headers: {
@ -61,13 +75,13 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
},
body: new URLSearchParams({
key: 'webdav_password',
value: Math.random().toString(36).slice(-8), // Generate a random password
value: randomPassword,
}).toString(),
});
if (!createPasswordResponse.ok) {
console.error('Failed to create WebDAV password:', await createPasswordResponse.text());
return null;
throw new Error(`Failed to create WebDAV password: ${createPasswordResponse.status} ${createPasswordResponse.statusText}`);
}
// Get the new WebDAV password
@ -80,14 +94,24 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
if (!newUserInfoResponse.ok) {
console.error('Failed to get new user info:', await newUserInfoResponse.text());
return null;
throw new Error(`Failed to get new user info: ${newUserInfoResponse.status} ${newUserInfoResponse.statusText}`);
}
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
console.log('New User Info:', newUserInfo); // Debug log
if (!newUserInfo.webdav_password) {
throw new Error('WebDAV password was not set after creation');
}
return newUserInfo.webdav_password;
}
return webdavPassword;
} catch (error) {
console.error('Error in getWebDAVCredentials:', error);
throw error;
}
}
export async function GET() {