diff --git a/app/api/nextcloud/files/route.ts b/app/api/nextcloud/files/route.ts
index 6c07cd36..4677aa5b 100644
--- a/app/api/nextcloud/files/route.ts
+++ b/app/api/nextcloud/files/route.ts
@@ -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('');
+}
+
export async function GET(request: Request) {
try {
const { searchParams } = new URL(request.url);
@@ -90,55 +102,48 @@ 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('').slice(1);
+
+ for (const entry of fileEntries) {
+ const href = extractTextContent(entry, 'd:href');
+ if (!href) continue;
- 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;
-
- if (type && !type.getElementsByTagName('d:collection').length) {
- const filename = href.split('/').pop() || '';
-
- // For Contacts folder, return all files
- if (folder === 'Contacts') {
- files.push({
- filename: href,
- basename: filename,
- lastmod,
- size,
- type: 'file',
- etag,
- mime
- });
- }
- // For other folders, only return markdown files
- else if (filename.endsWith('.md')) {
- files.push({
- id: href,
- title: filename.replace('.md', ''),
- lastModified: new Date(lastmod || '').toISOString(),
- size,
- type: 'file',
- mime,
- etag
- });
- }
- }
- }
+ // 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');
+
+ const filename = href.split('/').pop() || '';
+
+ // For Contacts folder, return all files
+ if (folder === 'Contacts') {
+ files.push({
+ filename: href,
+ basename: filename,
+ lastmod,
+ size,
+ type: 'file',
+ etag,
+ mime
+ });
+ }
+ // For other folders, only return markdown files
+ else if (filename.endsWith('.md')) {
+ files.push({
+ id: href,
+ title: filename.replace('.md', ''),
+ lastModified: new Date(lastmod || '').toISOString(),
+ size,
+ type: 'file',
+ mime,
+ etag
+ });
}
}