71 lines
2.2 KiB
TypeScript
71 lines
2.2 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getAdminAuth, getAdminStorage } from '@/lib/firebase/admin';
|
|
|
|
export async function GET(
|
|
request: Request,
|
|
{ params }: { params: Promise<{ projectId: string }> }
|
|
) {
|
|
try {
|
|
const { projectId } = await params;
|
|
|
|
// Authentication (skip in development if no auth header)
|
|
const authHeader = request.headers.get('Authorization');
|
|
const isDevelopment = process.env.NODE_ENV === 'development';
|
|
|
|
if (!isDevelopment || authHeader?.startsWith('Bearer ')) {
|
|
if (!authHeader?.startsWith('Bearer ')) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const token = authHeader.substring(7);
|
|
const auth = getAdminAuth();
|
|
const decoded = await auth.verifyIdToken(token);
|
|
|
|
if (!decoded?.uid) {
|
|
return NextResponse.json({ error: 'Invalid token' }, { status: 401 });
|
|
}
|
|
}
|
|
|
|
// List files in Firebase Storage for this project
|
|
let fileList = [];
|
|
|
|
try {
|
|
const storage = getAdminStorage();
|
|
const bucket = storage.bucket();
|
|
const [files] = await bucket.getFiles({
|
|
prefix: `projects/${projectId}/`,
|
|
maxResults: 100,
|
|
});
|
|
|
|
fileList = files.map(file => ({
|
|
name: file.name.split('/').pop() || file.name,
|
|
fullPath: file.name,
|
|
size: file.metadata.size,
|
|
contentType: file.metadata.contentType,
|
|
timeCreated: file.metadata.timeCreated,
|
|
updated: file.metadata.updated,
|
|
}));
|
|
|
|
console.log('[API /storage/files] Found', fileList.length, 'files');
|
|
} catch (storageError) {
|
|
console.error('[API /storage/files] Firebase Storage query failed:', storageError);
|
|
console.error('[API /storage/files] This is likely due to missing Firebase Admin credentials');
|
|
// Return empty array instead of failing
|
|
fileList = [];
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
files: fileList,
|
|
count: fileList.length,
|
|
});
|
|
} catch (error) {
|
|
console.error('[API] Error fetching storage files:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch storage files' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|