This commit is contained in:
alma 2025-05-04 14:40:49 +02:00
parent 8884081071
commit dc4283a002
5 changed files with 14 additions and 11 deletions

View File

@ -73,7 +73,7 @@ export async function POST(request: Request) {
const normalizedFolder = folder.toLowerCase(); const normalizedFolder = folder.toLowerCase();
// Create the full key (path) for the S3 object // Create the full key (path) for the S3 object
const key = `user-${userId}/${normalizedFolder}/${title}${title.endsWith('.md') ? '' : '.md'}`; const key = `pages/user-${userId}/${normalizedFolder}/${title}${title.endsWith('.md') ? '' : '.md'}`;
console.log('Creating file in S3:', { key, contentLength: content.length }); console.log('Creating file in S3:', { key, contentLength: content.length });
@ -103,7 +103,7 @@ export async function PUT(request: Request) {
if (id) { if (id) {
// Ensure the user can only access their own files // Ensure the user can only access their own files
if (!id.startsWith(`user-${userId}/`)) { if (!id.includes(`user-${userId}/`)) {
return NextResponse.json({ error: 'Unauthorized access to file' }, { status: 403 }); return NextResponse.json({ error: 'Unauthorized access to file' }, { status: 403 });
} }
key = id; key = id;
@ -113,7 +113,7 @@ export async function PUT(request: Request) {
return NextResponse.json({ error: 'Missing required fields', received: { title: !!title, folder: !!folder } }, { status: 400 }); return NextResponse.json({ error: 'Missing required fields', received: { title: !!title, folder: !!folder } }, { status: 400 });
} }
const normalizedFolder = folder.toLowerCase(); const normalizedFolder = folder.toLowerCase();
key = `user-${userId}/${normalizedFolder}/${title}${title.endsWith('.md') ? '' : '.md'}`; key = `pages/user-${userId}/${normalizedFolder}/${title}${title.endsWith('.md') ? '' : '.md'}`;
} }
console.log('Updating file in S3:', { key, contentLength: content?.length }); console.log('Updating file in S3:', { key, contentLength: content?.length });
@ -144,7 +144,7 @@ export async function DELETE(request: Request) {
} }
// Ensure the user can only delete their own files // Ensure the user can only delete their own files
if (!id.startsWith(`user-${userId}/`)) { if (!id.includes(`user-${userId}/`)) {
return NextResponse.json({ error: 'Unauthorized access to file' }, { status: 403 }); return NextResponse.json({ error: 'Unauthorized access to file' }, { status: 403 });
} }

View File

@ -38,11 +38,11 @@ export async function POST(request: Request) {
try { try {
// Create the folder path (just a prefix in S3) // Create the folder path (just a prefix in S3)
const key = `user-${userId}/${folderLowercase}/`; const key = `pages/user-${userId}/${folderLowercase}/`;
await putObject(key, '', 'application/x-directory'); await putObject(key, '', 'application/x-directory');
// Create a placeholder file to ensure the folder exists and is visible // Create a placeholder file to ensure the folder exists and is visible
const placeholderKey = `user-${userId}/${folderLowercase}/.placeholder`; const placeholderKey = `pages/user-${userId}/${folderLowercase}/.placeholder`;
await putObject(placeholderKey, 'Folder placeholder', 'text/plain'); await putObject(placeholderKey, 'Folder placeholder', 'text/plain');
console.log(`Successfully created folder: ${key}`); console.log(`Successfully created folder: ${key}`);

View File

@ -46,7 +46,7 @@ export async function GET() {
// List folders in the user's path // List folders in the user's path
try { try {
// Using the user prefix to list all folders // Using the user prefix to list all folders
const prefix = `user-${userId}/`; const prefix = `pages/user-${userId}/`;
const command = new ListObjectsV2Command({ const command = new ListObjectsV2Command({
Bucket: S3_CONFIG.bucket, Bucket: S3_CONFIG.bucket,
Prefix: prefix, Prefix: prefix,

View File

@ -296,7 +296,7 @@ export default function CarnetPage() {
setIsSaving(true); setIsSaving(true);
// Construct API payload with lowercase folder name // Construct API payload with lowercase folder name
const payload = { const payload = {
id: note.id, id: note.id || `pages/user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${note.title}${note.title.endsWith('.md') ? '' : '.md'}`,
title: note.title, title: note.title,
content: note.content, content: note.content,
folder: selectedFolder.toLowerCase(), // Use lowercase for storage consistency folder: selectedFolder.toLowerCase(), // Use lowercase for storage consistency

View File

@ -68,7 +68,10 @@ console.log('S3 Configuration:', {
// List objects in a "folder" for a specific user // List objects in a "folder" for a specific user
export async function listUserObjects(userId: string, folder: string) { export async function listUserObjects(userId: string, folder: string) {
try { try {
const prefix = `user-${userId}/${folder}/`; // Updated to include 'pages/' prefix in the path
const prefix = `pages/user-${userId}/${folder}/`;
console.log(`Listing objects with prefix: ${prefix}`);
const command = new ListObjectsV2Command({ const command = new ListObjectsV2Command({
Bucket: S3_CONFIG.bucket, Bucket: S3_CONFIG.bucket,
Prefix: prefix, Prefix: prefix,
@ -169,12 +172,12 @@ export async function createUserFolderStructure(userId: string) {
for (const folder of folders) { for (const folder of folders) {
try { try {
// Create the folder path (just a prefix in S3) // Create the folder path (just a prefix in S3)
const key = `user-${userId}/${folder}/`; const key = `pages/user-${userId}/${folder}/`;
console.log(`Creating folder: ${key}`); console.log(`Creating folder: ${key}`);
await putObject(key, '', 'application/x-directory'); await putObject(key, '', 'application/x-directory');
// Create a placeholder file to ensure the folder exists and is visible // Create a placeholder file to ensure the folder exists and is visible
const placeholderKey = `user-${userId}/${folder}/.placeholder`; const placeholderKey = `pages/user-${userId}/${folder}/.placeholder`;
await putObject(placeholderKey, 'Folder placeholder', 'text/plain'); await putObject(placeholderKey, 'Folder placeholder', 'text/plain');
results.push(folder); results.push(folder);