78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { ListBucketsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
|
|
import { s3Client, S3_CONFIG } from '@/lib/s3';
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
// Get session
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const userId = session.user.id;
|
|
const { searchParams } = new URL(request.url);
|
|
const folder = searchParams.get('folder') || 'notes';
|
|
|
|
// Debug information about the configuration
|
|
const config = {
|
|
endpoint: S3_CONFIG.endpoint,
|
|
region: S3_CONFIG.region,
|
|
bucket: S3_CONFIG.bucket,
|
|
hasAccessKey: !!S3_CONFIG.accessKey,
|
|
hasSecretKey: !!S3_CONFIG.secretKey
|
|
};
|
|
|
|
// Try to list buckets to verify credentials
|
|
let bucketList = [];
|
|
try {
|
|
const bucketsResponse = await s3Client.send(new ListBucketsCommand({}));
|
|
bucketList = bucketsResponse.Buckets?.map(b => b.Name) || [];
|
|
} catch (error) {
|
|
return NextResponse.json({
|
|
error: 'Failed to list buckets',
|
|
message: error instanceof Error ? error.message : String(error),
|
|
config
|
|
}, { status: 500 });
|
|
}
|
|
|
|
// Now try to list objects in the folder
|
|
const prefix = `user-${userId}/${folder}/`;
|
|
try {
|
|
const command = new ListObjectsV2Command({
|
|
Bucket: S3_CONFIG.bucket,
|
|
Prefix: prefix,
|
|
Delimiter: '/'
|
|
});
|
|
|
|
const response = await s3Client.send(command);
|
|
|
|
return NextResponse.json({
|
|
config,
|
|
buckets: bucketList,
|
|
prefix,
|
|
objects: response.Contents?.map(item => ({
|
|
key: item.Key,
|
|
size: item.Size,
|
|
lastModified: item.LastModified
|
|
})) || [],
|
|
prefixes: response.CommonPrefixes?.map(p => p.Prefix) || []
|
|
});
|
|
} catch (error) {
|
|
return NextResponse.json({
|
|
error: 'Failed to list objects',
|
|
message: error instanceof Error ? error.message : String(error),
|
|
config,
|
|
buckets: bucketList,
|
|
prefix
|
|
}, { status: 500 });
|
|
}
|
|
} catch (error) {
|
|
return NextResponse.json({
|
|
error: 'Internal server error',
|
|
message: error instanceof Error ? error.message : String(error)
|
|
}, { status: 500 });
|
|
}
|
|
}
|