Page logic 2

This commit is contained in:
alma 2025-04-21 11:43:40 +02:00
parent 9fbeb08462
commit 207d4938e6

View File

@ -42,6 +42,18 @@ const createWebDAVClient = async (userId: string) => {
};
};
// Helper function to extract text content from XML
function extractTextContent(xml: string, tag: string): string | null {
const regex = new RegExp(`<${tag}[^>]*>(.*?)</${tag}>`, 's');
const match = xml.match(regex);
return match ? match[1].trim() : null;
}
// Helper function to check if a response is a collection
function isCollection(xml: string): boolean {
return xml.includes('<d:collection/>');
}
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
@ -90,27 +102,23 @@ export async function GET(request: Request) {
}
const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, 'text/xml');
const files: any[] = [];
const responses = xmlDoc.getElementsByTagName('d:response');
for (let i = 0; i < responses.length; i++) {
const response = responses[i];
const href = response.getElementsByTagName('d:href')[0]?.textContent;
const propstat = response.getElementsByTagName('d:propstat')[0];
// Split the response into individual file entries
const fileEntries = text.split('<d:response>').slice(1);
if (href && propstat) {
const prop = propstat.getElementsByTagName('d:prop')[0];
if (prop) {
const type = prop.getElementsByTagName('d:resourcetype')[0];
const lastmod = prop.getElementsByTagName('d:getlastmodified')[0]?.textContent;
const size = prop.getElementsByTagName('d:getcontentlength')[0]?.textContent;
const mime = prop.getElementsByTagName('d:getcontenttype')[0]?.textContent;
const etag = prop.getElementsByTagName('d:getetag')[0]?.textContent;
for (const entry of fileEntries) {
const href = extractTextContent(entry, 'd:href');
if (!href) continue;
// Skip if it's a collection (directory)
if (isCollection(entry)) continue;
const lastmod = extractTextContent(entry, 'd:getlastmodified');
const size = extractTextContent(entry, 'd:getcontentlength');
const mime = extractTextContent(entry, 'd:getcontenttype');
const etag = extractTextContent(entry, 'd:getetag');
if (type && !type.getElementsByTagName('d:collection').length) {
const filename = href.split('/').pop() || '';
// For Contacts folder, return all files
@ -138,9 +146,6 @@ export async function GET(request: Request) {
});
}
}
}
}
}
return NextResponse.json(files);
} catch (error) {