diff --git a/app/api/nextcloud/files/route.ts b/app/api/nextcloud/files/route.ts index 3ccc0ecf..9e4c2dff 100644 --- a/app/api/nextcloud/files/route.ts +++ b/app/api/nextcloud/files/route.ts @@ -47,28 +47,34 @@ export async function GET(request: Request) { const client = createClient({ username: credentials.username, password: credentials.password, - baseURL: normalizedBaseURL, + baseURL: `${normalizedBaseURL}/remote.php/dav/files/${credentials.username}/Private`, + authType: 'basic', }); - // Construct the folder path with leading slash - const folderPath = `/remote.php/dav/files/${credentials.username}/Private/${folder}`; + // Construct the folder path without leading slash + const folderPath = folder; console.log('Full folder path:', folderPath); - // List files in the specified folder - const files = await client.getDirectoryContents(folderPath); - console.log('Found files:', files.length); + try { + // List files in the specified folder + const files = await client.getDirectoryContents(folderPath); + console.log('Found files:', files.length); - // Filter for .md files and format the response - const markdownFiles = files - .filter((file: any) => file.basename.endsWith('.md')) - .map((file: any) => ({ - id: file.filename, - title: file.basename.replace('.md', ''), - lastModified: new Date(file.lastmod).toISOString(), - size: file.size, - })); + // Filter for .md files and format the response + const markdownFiles = files + .filter((file: any) => file.basename.endsWith('.md')) + .map((file: any) => ({ + id: file.filename, + title: file.basename.replace('.md', ''), + lastModified: new Date(file.lastmod).toISOString(), + size: file.size, + })); - return NextResponse.json(markdownFiles); + return NextResponse.json(markdownFiles); + } catch (error) { + console.error('Error listing directory contents:', error); + return NextResponse.json({ error: 'Failed to list directory contents' }, { status: 500 }); + } } catch (error) { console.error('Error fetching files:', error); return NextResponse.json({ error: 'Failed to fetch files' }, { status: 500 });