NeahNew/app/api/storage/status/route.ts
2025-05-04 14:20:40 +02:00

109 lines
3.7 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { S3Client, ListBucketsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
import { createUserFolderStructure, listUserObjects } from '@/lib/s3';
// Import the configured S3 client and bucket name from lib/s3.ts
import { s3Client as configuredS3Client } from '@/lib/s3';
// Get bucket name from environment
const S3_BUCKET_NAME = process.env.MINIO_AWS_S3_UPLOAD_BUCKET_NAME || 'pages';
// Cache for folder lists
const folderCache = new Map<string, { folders: string[], timestamp: number }>();
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const userId = session.user.id;
// Check if we have cached folders for this user
const cachedData = folderCache.get(userId);
if (cachedData && (Date.now() - cachedData.timestamp < CACHE_TTL)) {
console.log(`Returning cached folders for user ${userId}:`, cachedData.folders);
return NextResponse.json({
status: 'ready',
folders: cachedData.folders
});
}
// Check S3 connectivity using the configured client
try {
// Simple check by listing buckets
await configuredS3Client.send(new ListBucketsCommand({}));
} catch (error) {
console.error('S3 connectivity check failed:', error);
return NextResponse.json({
error: 'S3 storage service is not accessible',
status: 'error'
}, { status: 503 });
}
// Direct approach - list all folders under the user prefix
try {
const prefix = `user-${userId}/`;
// List all objects with this prefix to find folders
const command = new ListObjectsV2Command({
Bucket: S3_BUCKET_NAME,
Prefix: prefix,
Delimiter: '/'
});
const response = await configuredS3Client.send(command);
// CommonPrefixes contains the folder paths
const userPrefixes = response.CommonPrefixes || [];
// Extract folder names from prefixes (e.g., "user-123/notes/" → "Notes")
let userFolders = userPrefixes
.map(prefix => prefix.Prefix?.split('/')[1])
.filter(Boolean) as string[];
console.log(`Found ${userFolders.length} folders for user ${userId}:`, userFolders);
// If no folders found, create the standard structure
if (userFolders.length === 0) {
console.log(`No folders found for user ${userId}, creating standard structure`);
await createUserFolderStructure(userId);
userFolders = ['notes', 'diary', 'health', 'contacts'];
}
// Convert to Pascal case for backwards compatibility with NextCloud
const formattedFolders = userFolders.map(folder =>
folder.charAt(0).toUpperCase() + folder.slice(1)
);
console.log(`Returning formatted folders for user ${userId}:`, formattedFolders);
// Update cache
folderCache.set(userId, {
folders: formattedFolders,
timestamp: Date.now()
});
return NextResponse.json({
status: 'ready',
folders: formattedFolders
});
} catch (error) {
console.error('Error fetching user folders:', error);
return NextResponse.json({
error: 'Failed to fetch folders',
status: 'error'
}, { status: 500 });
}
} catch (error) {
console.error('Error in storage status check:', error);
return NextResponse.json({
error: 'Internal server error',
status: 'error'
}, { status: 500 });
}
}