From d28940adc5524d5c1bca7ae570e97616237cb06d Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 20 Apr 2025 15:23:20 +0200 Subject: [PATCH] carnet api nc --- app/api/nextcloud/status/route.ts | 46 +++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/app/api/nextcloud/status/route.ts b/app/api/nextcloud/status/route.ts index a4d76e0d..260bba7d 100644 --- a/app/api/nextcloud/status/route.ts +++ b/app/api/nextcloud/status/route.ts @@ -51,27 +51,38 @@ async function parseXMLResponse(response: Response): Promise { } async function createFolder(nextcloudUrl: string, username: string, password: string, folderPath: string) { - const response = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/${encodeURIComponent(folderPath)}`, { - method: 'MKCOL', - headers: { - 'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`, - }, - }); + try { + const response = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/${encodeURIComponent(folderPath)}`, { + method: 'MKCOL', + headers: { + 'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`, + }, + }); - if (!response.ok && response.status !== 405) { // 405 means folder already exists - throw new Error(`Failed to create folder ${folderPath}: ${response.status} ${response.statusText}`); + if (!response.ok && response.status !== 405) { // 405 means folder already exists + const errorText = await response.text(); + console.error(`Failed to create folder ${folderPath}:`, { + status: response.status, + statusText: response.statusText, + error: errorText + }); + throw new Error(`Failed to create folder ${folderPath}: ${response.status} ${response.statusText}`); + } + } catch (error) { + console.error(`Error creating folder ${folderPath}:`, error); + throw error; } } async function ensureFolderStructure(nextcloudUrl: string, username: string, password: string) { try { - // Create Private folder if it doesn't exist + // First, ensure the Private folder exists await createFolder(nextcloudUrl, username, password, 'Private'); - // Create Diary folder if it doesn't exist + // Then create Diary folder inside Private await createFolder(nextcloudUrl, username, password, 'Private/Diary'); - // Create Health folder if it doesn't exist + // Finally create Health folder inside Private await createFolder(nextcloudUrl, username, password, 'Private/Health'); } catch (error) { console.error('Error creating folder structure:', error); @@ -99,12 +110,17 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi }); if (verifyResponse.ok) { - // Ensure required folders exist - await ensureFolderStructure(nextcloudUrl, username, credentials.password); - return credentials.password; + try { + // Ensure required folders exist + await ensureFolderStructure(nextcloudUrl, username, credentials.password); + return credentials.password; + } catch (folderError) { + console.error('Failed to create folders with existing credentials:', folderError); + // If folder creation fails, we'll try with new credentials + } } - // If verification failed, delete the old credentials + // If verification failed or folder creation failed, delete the old credentials await prisma.webDAVCredentials.delete({ where: { userId }, });