Pages corrections

This commit is contained in:
alma 2026-01-16 11:34:06 +01:00
parent b8078a261f
commit 817dffda9c
2 changed files with 31 additions and 20 deletions

View File

@ -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);

View File

@ -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'
// 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.isBuffer(content)
? content
: Buffer.from(content);
: 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<void> {
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