From d07f48394afeaa4f97b1c55deaee3007f0a47b71 Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 6 May 2025 12:46:06 +0200 Subject: [PATCH] missions s3 --- components/missions/missions-admin-panel.tsx | 30 +++++++++++- lib/mission-uploads.ts | 51 +++++++++++++++++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/components/missions/missions-admin-panel.tsx b/components/missions/missions-admin-panel.tsx index 6a603a31..0e2f86f8 100644 --- a/components/missions/missions-admin-panel.tsx +++ b/components/missions/missions-admin-panel.tsx @@ -437,7 +437,20 @@ export function MissionsAdminPanel() { }); if (!logoResponse.ok) { - console.error('Failed to upload logo:', await logoResponse.json()); + console.error('Failed to upload logo - Status:', logoResponse.status, logoResponse.statusText); + try { + const errorJson = await logoResponse.json().catch(() => null); + if (errorJson) { + console.error('Error details:', errorJson); + } else { + console.error('No JSON error details available - Response may be empty'); + // Try to get error details from headers + const headers = Object.fromEntries(logoResponse.headers.entries()); + console.error('Response headers:', headers); + } + } catch (parseError) { + console.error('Error parsing response:', parseError); + } // Continue with mission creation even if logo upload fails } else { console.log('Logo uploaded successfully'); @@ -461,7 +474,20 @@ export function MissionsAdminPanel() { }); if (!attachmentResponse.ok) { - console.error(`Failed to upload attachment ${file.name}:`, await attachmentResponse.json()); + console.error(`Failed to upload attachment ${file.name} - Status:`, attachmentResponse.status, attachmentResponse.statusText); + try { + const errorJson = await attachmentResponse.json().catch(() => null); + if (errorJson) { + console.error('Error details:', errorJson); + } else { + console.error('No JSON error details available - Response may be empty'); + // Try to get error details from headers + const headers = Object.fromEntries(attachmentResponse.headers.entries()); + console.error('Response headers:', headers); + } + } catch (parseError) { + console.error('Error parsing response:', parseError); + } return false; } else { console.log(`Attachment ${file.name} uploaded successfully`); diff --git a/lib/mission-uploads.ts b/lib/mission-uploads.ts index b6eb5338..53223695 100644 --- a/lib/mission-uploads.ts +++ b/lib/mission-uploads.ts @@ -50,19 +50,37 @@ export async function uploadMissionLogo( hasSecretKey: !!S3_CONFIG.secretKey || 'MISSING!' }); + // Log the full path being used + console.log('FULL S3 PATH:', `${S3_CONFIG.endpoint}/${S3_CONFIG.missionsBucket}/${filePath}`); + const command = new PutObjectCommand({ Bucket: S3_CONFIG.missionsBucket, Key: filePath, Body: buffer, ContentType: file.type, + // Add ACL for public read access + ACL: 'public-read', }); console.log('Sending upload command to S3/Minio...'); + console.log('Command details:', { + Bucket: command.input.Bucket, + Key: command.input.Key, + ContentType: command.input.ContentType, + ACL: command.input.ACL, + ContentLength: buffer.length + }); + try { const result = await s3Client.send(command); console.log('Upload successful, result:', result); } catch (uploadError) { console.error('S3 upload error details:', uploadError); + console.error('Error name:', (uploadError as any).name); + console.error('Error message:', (uploadError as any).message); + if ((uploadError as any).$metadata) { + console.error('Error metadata:', (uploadError as any).$metadata); + } throw uploadError; } @@ -86,12 +104,21 @@ export async function uploadMissionAttachment( fileSize: number }> { try { + console.log('=== Starting attachment upload process ==='); + console.log('Upload params:', { userId, missionId, fileName: file.name, fileSize: file.size, fileType: file.type }); + // Create file path const filePath = getMissionAttachmentPath(userId, missionId, file.name); + console.log('Generated file path:', filePath); // Convert file to ArrayBuffer + console.log('Converting file to buffer...'); const arrayBuffer = await file.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); + console.log('Buffer created, size:', buffer.length); + + // Log the full path being used + console.log('FULL S3 PATH:', `${S3_CONFIG.endpoint}/${S3_CONFIG.missionsBucket}/${filePath}`); // Upload to Minio using missions bucket const command = new PutObjectCommand({ @@ -99,9 +126,31 @@ export async function uploadMissionAttachment( Key: filePath, Body: buffer, ContentType: file.type, + // Add ACL for public read access + ACL: 'public-read', }); - await s3Client.send(command); + console.log('Sending upload command to S3/Minio...'); + console.log('Command details:', { + Bucket: command.input.Bucket, + Key: command.input.Key, + ContentType: command.input.ContentType, + ACL: command.input.ACL, + ContentLength: buffer.length + }); + + try { + const result = await s3Client.send(command); + console.log('Upload successful, result:', result); + } catch (uploadError) { + console.error('S3 upload error details:', uploadError); + console.error('Error name:', (uploadError as any).name); + console.error('Error message:', (uploadError as any).message); + if ((uploadError as any).$metadata) { + console.error('Error metadata:', (uploadError as any).$metadata); + } + throw uploadError; + } return { filename: file.name,