carnet api nc

This commit is contained in:
alma 2025-04-20 14:41:20 +02:00
parent fb59a0e160
commit 1acecd3678

View File

@ -82,11 +82,15 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
throw new Error(`Failed to set password: ${setPasswordResponse.status} ${setPasswordResponse.statusText}`);
}
// Verify the password was set by trying to authenticate
// Verify the password was set by trying to authenticate with PROPFIND
const verifyResponse = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/`, {
method: 'PROPFIND',
headers: {
'Authorization': `Basic ${Buffer.from(`${username}:${newPassword}`).toString('base64')}`,
'Depth': '1',
'Content-Type': 'application/xml',
},
body: '<?xml version="1.0" encoding="UTF-8"?><d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/></d:prop></d:propfind>',
});
if (!verifyResponse.ok) {
@ -156,11 +160,13 @@ export async function GET() {
console.log('Requesting WebDAV URL:', webdavUrl);
const foldersResponse = await fetch(webdavUrl, {
method: 'PROPFIND',
headers: {
'Authorization': `Basic ${Buffer.from(`${nextcloudUsername}:${webdavPassword}`).toString('base64')}`,
'Depth': '1',
'Content-Type': 'application/xml',
},
body: '<?xml version="1.0" encoding="UTF-8"?><d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/><d:displayname/></d:prop></d:propfind>',
});
if (foldersResponse.status === 429) {
@ -192,9 +198,13 @@ export async function GET() {
const isCollection = resourceType?.getElementsByTagName('d:collection').length > 0;
if (isCollection) {
const displayName = response.getElementsByTagName('d:displayname')[0]?.textContent;
if (displayName && displayName !== nextcloudUsername) {
folders.push(displayName);
const href = response.getElementsByTagName('d:href')[0]?.textContent;
if (href) {
// Extract folder name from href
const folderName = decodeURIComponent(href.split('/').filter(Boolean).pop() || '');
if (folderName && folderName !== nextcloudUsername) {
folders.push(folderName);
}
}
}
}