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) { export async function GET(request: Request) {
try { try {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
@ -90,55 +102,48 @@ export async function GET(request: Request) {
} }
const text = await response.text(); const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, 'text/xml');
const files: any[] = []; const files: any[] = [];
const responses = xmlDoc.getElementsByTagName('d:response');
for (let i = 0; i < responses.length; i++) { // Split the response into individual file entries
const response = responses[i]; const fileEntries = text.split('<d:response>').slice(1);
const href = response.getElementsByTagName('d:href')[0]?.textContent;
const propstat = response.getElementsByTagName('d:propstat')[0];
if (href && propstat) { for (const entry of fileEntries) {
const prop = propstat.getElementsByTagName('d:prop')[0]; const href = extractTextContent(entry, 'd:href');
if (prop) { if (!href) continue;
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;
if (type && !type.getElementsByTagName('d:collection').length) { // Skip if it's a collection (directory)
const filename = href.split('/').pop() || ''; if (isCollection(entry)) continue;
// For Contacts folder, return all files const lastmod = extractTextContent(entry, 'd:getlastmodified');
if (folder === 'Contacts') { const size = extractTextContent(entry, 'd:getcontentlength');
files.push({ const mime = extractTextContent(entry, 'd:getcontenttype');
filename: href, const etag = extractTextContent(entry, 'd:getetag');
basename: filename,
lastmod, const filename = href.split('/').pop() || '';
size,
type: 'file', // For Contacts folder, return all files
etag, if (folder === 'Contacts') {
mime files.push({
}); filename: href,
} basename: filename,
// For other folders, only return markdown files lastmod,
else if (filename.endsWith('.md')) { size,
files.push({ type: 'file',
id: href, etag,
title: filename.replace('.md', ''), mime
lastModified: new Date(lastmod || '').toISOString(), });
size, }
type: 'file', // For other folders, only return markdown files
mime, else if (filename.endsWith('.md')) {
etag files.push({
}); id: href,
} title: filename.replace('.md', ''),
} lastModified: new Date(lastmod || '').toISOString(),
} size,
type: 'file',
mime,
etag
});
} }
} }