diff --git a/app/api/storage/files/route.ts b/app/api/storage/files/route.ts index 9cb57876..5b519ed1 100644 --- a/app/api/storage/files/route.ts +++ b/app/api/storage/files/route.ts @@ -73,7 +73,7 @@ export async function POST(request: Request) { const normalizedFolder = folder.toLowerCase(); // 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 }); @@ -103,7 +103,7 @@ export async function PUT(request: Request) { if (id) { // 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 }); } 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 }); } 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 }); @@ -144,7 +144,7 @@ export async function DELETE(request: Request) { } // 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 }); } diff --git a/app/api/storage/init/folder/route.ts b/app/api/storage/init/folder/route.ts index d7840766..16fdb4f2 100644 --- a/app/api/storage/init/folder/route.ts +++ b/app/api/storage/init/folder/route.ts @@ -38,11 +38,11 @@ export async function POST(request: Request) { try { // 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'); // 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'); console.log(`Successfully created folder: ${key}`); diff --git a/app/api/storage/status/route.ts b/app/api/storage/status/route.ts index 05099bf9..c62c3910 100644 --- a/app/api/storage/status/route.ts +++ b/app/api/storage/status/route.ts @@ -46,7 +46,7 @@ export async function GET() { // List folders in the user's path try { // Using the user prefix to list all folders - const prefix = `user-${userId}/`; + const prefix = `pages/user-${userId}/`; const command = new ListObjectsV2Command({ Bucket: S3_CONFIG.bucket, Prefix: prefix, diff --git a/app/pages/page.tsx b/app/pages/page.tsx index 158b575c..fa9adf4c 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -296,7 +296,7 @@ export default function CarnetPage() { setIsSaving(true); // Construct API payload with lowercase folder name 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, content: note.content, folder: selectedFolder.toLowerCase(), // Use lowercase for storage consistency diff --git a/lib/s3.ts b/lib/s3.ts index fbd98b2d..084b3e41 100644 --- a/lib/s3.ts +++ b/lib/s3.ts @@ -68,7 +68,10 @@ console.log('S3 Configuration:', { // List objects in a "folder" for a specific user export async function listUserObjects(userId: string, folder: string) { 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({ Bucket: S3_CONFIG.bucket, Prefix: prefix, @@ -169,12 +172,12 @@ export async function createUserFolderStructure(userId: string) { for (const folder of folders) { try { // 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}`); await putObject(key, '', 'application/x-directory'); // 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'); results.push(folder);