import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; // This file serves as an adapter to redirect requests from the old NextCloud // endpoints to the new MinIO S3 endpoints export async function GET(request: Request) { try { // Get session const session = await getServerSession(authOptions); if (!session?.user) { 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); } // 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 }); } } export async function POST(request: Request) { try { // Get session const session = await getServerSession(authOptions); if (!session?.user) { 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 }); } catch (error) { console.error('Error in NextCloud adapter (POST):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); } } export async function PUT(request: Request) { try { // Get session const session = await getServerSession(authOptions); if (!session?.user) { 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 }); } catch (error) { console.error('Error in NextCloud adapter (PUT):', error); return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); } } export async function DELETE(request: Request) { try { // Get session const session = await getServerSession(authOptions); if (!session?.user) { 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 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 }); } }