From dab73b433c8b5a5fab6da36952387292fd8872e2 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 4 May 2025 13:54:23 +0200 Subject: [PATCH] pages s3 --- app/api/nextcloud/files/content/route.ts | 29 ++----- app/api/nextcloud/files/route.ts | 97 ++++++++---------------- app/api/nextcloud/init/route.ts | 18 ++--- app/api/nextcloud/status/route.ts | 25 +++--- app/api/storage/init/route.ts | 2 +- 5 files changed, 58 insertions(+), 113 deletions(-) diff --git a/app/api/nextcloud/files/content/route.ts b/app/api/nextcloud/files/content/route.ts index e1c98da9..bd573acb 100644 --- a/app/api/nextcloud/files/content/route.ts +++ b/app/api/nextcloud/files/content/route.ts @@ -13,29 +13,14 @@ export async function GET(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get query parameters - const { searchParams } = new URL(request.url); - const path = searchParams.get('path'); - const id = searchParams.get('id'); - - // Create a new URL for the storage API with the same parameters - const newUrl = new URL('/api/storage/files/content', request.url); - if (path) { - newUrl.searchParams.set('path', path); + // Forward to storage handler + try { + const { GET: storageContentHandler } = await import('@/app/api/storage/files/content/route'); + return await storageContentHandler(request); + } catch (error) { + console.error('Error calling storage content handler:', error); + return NextResponse.json({ error: 'Failed to get file content' }, { status: 500 }); } - if (id) { - newUrl.searchParams.set('id', id); - } - - // Forward the request to the new endpoint - const response = await fetch(newUrl, { - headers: { - 'Cookie': request.headers.get('cookie') || '' - } - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); } catch (error) { console.error('Error in NextCloud content adapter:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); diff --git a/app/api/nextcloud/files/route.ts b/app/api/nextcloud/files/route.ts index 8e356e8a..124d8da8 100644 --- a/app/api/nextcloud/files/route.ts +++ b/app/api/nextcloud/files/route.ts @@ -13,25 +13,14 @@ export async function GET(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get query parameters - const { searchParams } = new URL(request.url); - const folder = searchParams.get('folder'); - - // Create a new URL for the storage API with the same parameters - const newUrl = new URL('/api/storage/files', request.url); - if (folder) { - newUrl.searchParams.set('folder', folder); + // Get query parameters - we'll pass the same request to the storage handler + try { + const { GET: storageFilesHandler } = await import('@/app/api/storage/files/route'); + return await storageFilesHandler(request); + } catch (error) { + console.error('Error calling storage files handler:', error); + return NextResponse.json({ error: 'Failed to list files' }, { status: 500 }); } - - // Forward the request to the new endpoint - const response = await fetch(newUrl, { - headers: { - 'Cookie': request.headers.get('cookie') || '' - } - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); } catch (error) { console.error('Error in NextCloud adapter (GET):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); @@ -46,21 +35,14 @@ export async function POST(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get request body - const body = await request.json(); - - // Forward the request to the new endpoint - const response = await fetch('/api/storage/files', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Cookie': request.headers.get('cookie') || '' - }, - body: JSON.stringify(body) - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); + // Forward to storage handler + try { + const { POST: storageFilesHandler } = await import('@/app/api/storage/files/route'); + return await storageFilesHandler(request); + } catch (error) { + console.error('Error calling storage files handler:', error); + return NextResponse.json({ error: 'Failed to create file' }, { status: 500 }); + } } catch (error) { console.error('Error in NextCloud adapter (POST):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); @@ -75,21 +57,14 @@ export async function PUT(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get request body - const body = await request.json(); - - // Forward the request to the new endpoint - const response = await fetch('/api/storage/files', { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - 'Cookie': request.headers.get('cookie') || '' - }, - body: JSON.stringify(body) - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); + // Forward to storage handler + try { + const { PUT: storageFilesHandler } = await import('@/app/api/storage/files/route'); + return await storageFilesHandler(request); + } catch (error) { + console.error('Error calling storage files handler:', error); + return NextResponse.json({ error: 'Failed to update file' }, { status: 500 }); + } } catch (error) { console.error('Error in NextCloud adapter (PUT):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); @@ -104,26 +79,14 @@ export async function DELETE(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get query parameters - const { searchParams } = new URL(request.url); - const id = searchParams.get('id'); - - // Create a new URL for the storage API with the same parameters - const newUrl = new URL('/api/storage/files', request.url); - if (id) { - newUrl.searchParams.set('id', id); + // Forward to storage handler + try { + const { DELETE: storageFilesHandler } = await import('@/app/api/storage/files/route'); + return await storageFilesHandler(request); + } catch (error) { + console.error('Error calling storage files handler:', error); + return NextResponse.json({ error: 'Failed to delete file' }, { status: 500 }); } - - // Forward the request to the new endpoint - const response = await fetch(newUrl, { - method: 'DELETE', - headers: { - 'Cookie': request.headers.get('cookie') || '' - } - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); } catch (error) { console.error('Error in NextCloud adapter (DELETE):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); diff --git a/app/api/nextcloud/init/route.ts b/app/api/nextcloud/init/route.ts index 43f904e8..4633df6b 100644 --- a/app/api/nextcloud/init/route.ts +++ b/app/api/nextcloud/init/route.ts @@ -13,16 +13,14 @@ export async function POST(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Forward the request to the new endpoint - const response = await fetch('/api/storage/init', { - method: 'POST', - headers: { - 'Cookie': request.headers.get('cookie') || '' - } - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); + // Forward to storage handler + try { + const { POST: storageInitHandler } = await import('@/app/api/storage/init/route'); + return await storageInitHandler(); + } catch (error) { + console.error('Error calling storage init handler:', error); + return NextResponse.json({ error: 'Failed to initialize storage' }, { status: 500 }); + } } catch (error) { console.error('Error in NextCloud init adapter:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); diff --git a/app/api/nextcloud/status/route.ts b/app/api/nextcloud/status/route.ts index 5d93b730..e6322d16 100644 --- a/app/api/nextcloud/status/route.ts +++ b/app/api/nextcloud/status/route.ts @@ -13,19 +13,18 @@ export async function GET(request: Request) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } - // Get the base URL from the request - const { protocol, host } = new URL(request.url); - const baseUrl = `${protocol}//${host}`; - - // Forward the request to the new endpoint - const response = await fetch(`${baseUrl}/api/storage/status`, { - headers: { - 'Cookie': request.headers.get('cookie') || '' - } - }); - - // Return the response from the new endpoint - return NextResponse.json(await response.json(), { status: response.status }); + // Instead of making an HTTP request, directly call the handler function + // for the storage status endpoint + try { + const { GET: storageStatusHandler } = await import('@/app/api/storage/status/route'); + const storageResponse = await storageStatusHandler(); + + // Return the response + return storageResponse; + } catch (error) { + console.error('Error calling storage status handler:', error); + return NextResponse.json({ error: 'Failed to get storage status' }, { status: 500 }); + } } catch (error) { console.error('Error in NextCloud status adapter:', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); diff --git a/app/api/storage/init/route.ts b/app/api/storage/init/route.ts index d628bc6f..b07f156e 100644 --- a/app/api/storage/init/route.ts +++ b/app/api/storage/init/route.ts @@ -3,7 +3,7 @@ import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { createUserFolderStructure } from '@/lib/s3'; -export async function POST() { +export async function POST(request: Request) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) {