From 817dffda9c9d9317be34b5c0a90a299e6298c92b Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 11:34:06 +0100 Subject: [PATCH] Pages corrections --- app/api/storage/init/folder/route.ts | 18 ++++++++------- lib/s3.ts | 33 ++++++++++++++++++---------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/app/api/storage/init/folder/route.ts b/app/api/storage/init/folder/route.ts index 93e91a7..d87283c 100644 --- a/app/api/storage/init/folder/route.ts +++ b/app/api/storage/init/folder/route.ts @@ -37,15 +37,14 @@ export async function POST(request: Request) { console.log(`Creating folder ${folderLowercase} for user ${userId}`); try { - // Create the folder path (just a prefix in S3) - const key = `user-${userId}/${folderLowercase}/`; - await putObject(key, '', 'application/x-directory'); - - // Create a placeholder file to ensure the folder exists and is visible + // In S3/MinIO, folders are just prefixes, so we don't need to create an empty object. + // We only create a placeholder file to mark the folder as initialized. const placeholderKey = `user-${userId}/${folderLowercase}/.placeholder`; - await putObject(placeholderKey, 'Folder placeholder', 'text/plain'); + const placeholderContent = `Folder initialized at ${new Date().toISOString()}`; - console.log(`Successfully created folder: ${key}`); + await putObject(placeholderKey, placeholderContent, 'text/plain'); + + console.log(`Successfully created folder: user-${userId}/${folderLowercase}/`); // Return success return NextResponse.json({ @@ -54,7 +53,10 @@ export async function POST(request: Request) { }); } catch (error) { console.error(`Error creating folder ${folderLowercase}:`, error); - return NextResponse.json({ error: 'Failed to create folder' }, { status: 500 }); + return NextResponse.json({ + error: 'Failed to create folder', + details: error instanceof Error ? error.message : String(error) + }, { status: 500 }); } } catch (error) { console.error('Error in folder creation endpoint:', error); diff --git a/lib/s3.ts b/lib/s3.ts index 7c41bcc..80f3d0a 100644 --- a/lib/s3.ts +++ b/lib/s3.ts @@ -43,11 +43,17 @@ export async function putObject( ): Promise<{ key: string; url?: string }> { // Convert string to Buffer consistently, exactly like mission-uploads.ts // This ensures the same encoding and buffer handling that works for mission attachments - const buffer = typeof content === 'string' - ? Buffer.from(content, 'utf-8') - : Buffer.isBuffer(content) - ? content - : Buffer.from(content); + // For empty strings, use an empty buffer instead of Buffer.from('') to avoid MinIO issues + let buffer: Buffer; + if (typeof content === 'string') { + buffer = content.length > 0 + ? Buffer.from(content, 'utf-8') + : Buffer.alloc(0); + } else if (Buffer.isBuffer(content)) { + buffer = content; + } else { + buffer = Buffer.from(content); + } // Use the same pattern as mission-uploads.ts: direct s3Client.send with PutObjectCommand await s3Client.send(new PutObjectCommand({ @@ -148,21 +154,24 @@ export function getPublicUrl(filePath: string, bucket?: string): string { /** * Create standard folder structure for a user + * In S3/MinIO, folders don't need to be explicitly created - they exist as prefixes. + * We only create a placeholder file to mark the folder as initialized. */ export async function createUserFolderStructure(userId: string): Promise { const folders = ['notes', 'diary', 'health', 'contacts']; for (const folder of folders) { try { - // Create folder path (prefix in S3) - const folderKey = `user-${userId}/${folder}/`; - await putObject(folderKey, '', 'application/x-directory'); - - // Create placeholder file to ensure folder is visible + // In S3/MinIO, folders are just prefixes, so we don't need to create an empty object. + // We only create a placeholder file to mark the folder as initialized. const placeholderKey = `user-${userId}/${folder}/.placeholder`; - await putObject(placeholderKey, 'Folder placeholder', 'text/plain'); - console.log(`Created folder: ${folderKey}`); + // Use a simple text content for the placeholder + const placeholderContent = `Folder initialized at ${new Date().toISOString()}`; + + await putObject(placeholderKey, placeholderContent, 'text/plain'); + + console.log(`Created folder: user-${userId}/${folder}/`); } catch (error) { console.error(`Error creating folder ${folder} for user ${userId}:`, error); // Continue with other folders even if one fails