From 85c3eeceacac61080d1cd54fce9bf24149001e1a Mon Sep 17 00:00:00 2001 From: alma Date: Sat, 24 May 2025 19:46:46 +0200 Subject: [PATCH] W n8n attention vm --- lib/mission-uploads.ts | 160 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 159 insertions(+), 1 deletion(-) diff --git a/lib/mission-uploads.ts b/lib/mission-uploads.ts index 9f6dfa1b..a8880a0f 100644 --- a/lib/mission-uploads.ts +++ b/lib/mission-uploads.ts @@ -1,7 +1,20 @@ /** - * Utilities for mission-related file paths + * Utilities for mission-related file paths and uploads */ +import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; + +// Initialize S3 client for Minio +const s3Client = new S3Client({ + region: 'us-east-1', + endpoint: 'https://dome-api.slm-lab.net', + credentials: { + accessKeyId: '4aBT4CMb7JIMMyUtp4Pl', + secretAccessKey: 'HGn39XhCIlqOjmDVzRK9MED2Fci2rYvDDgbLFElg' + }, + forcePathStyle: true // Required for MinIO +}); + // Generate the mission logo path in Minio export function getMissionLogoPath(userId: string, missionId: string, fileExtension: string): string { // Use a consistent path structure: missions/{missionId}/logo{extension} @@ -45,4 +58,149 @@ export async function deleteMissionLogo(missionId: string, logoPath: string): Pr }); throw error; } +} + +// Upload a mission logo to Minio +export async function uploadMissionLogo(userId: string, missionId: string, file: File): Promise<{ filePath: string }> { + try { + console.log('Starting logo upload:', { + userId, + missionId, + fileName: file.name, + fileSize: file.size, + fileType: file.type + }); + + const fileExtension = file.name.substring(file.name.lastIndexOf('.')); + const filePath = getMissionLogoPath(userId, missionId, fileExtension); + const minioPath = filePath.replace(/^missions\//, ''); // Remove prefix for Minio + + // Convert File to Buffer + const arrayBuffer = await file.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + + console.log('Uploading to Minio:', { + bucket: 'missions', + key: minioPath, + contentType: file.type + }); + + await s3Client.send(new PutObjectCommand({ + Bucket: 'missions', + Key: minioPath, + Body: buffer, + ContentType: file.type, + ACL: 'public-read' + })); + + console.log('Logo upload successful:', { + filePath, + minioPath + }); + + return { filePath }; + } catch (error) { + console.error('Error uploading mission logo:', { + error, + userId, + missionId, + fileName: file.name, + errorType: error instanceof Error ? error.constructor.name : typeof error, + message: error instanceof Error ? error.message : String(error) + }); + throw error; + } +} + +// Upload a mission attachment to Minio +export async function uploadMissionAttachment( + userId: string, + missionId: string, + file: File +): Promise<{ + filename: string; + filePath: string; + fileType: string; + fileSize: number; +}> { + try { + console.log('Starting attachment upload:', { + userId, + missionId, + fileName: file.name, + fileSize: file.size, + fileType: file.type + }); + + const filePath = getMissionAttachmentPath(userId, missionId, file.name); + const minioPath = filePath.replace(/^missions\//, ''); // Remove prefix for Minio + + // Convert File to Buffer + const arrayBuffer = await file.arrayBuffer(); + const buffer = Buffer.from(arrayBuffer); + + console.log('Uploading to Minio:', { + bucket: 'missions', + key: minioPath, + contentType: file.type + }); + + await s3Client.send(new PutObjectCommand({ + Bucket: 'missions', + Key: minioPath, + Body: buffer, + ContentType: file.type, + ACL: 'public-read' + })); + + console.log('Attachment upload successful:', { + filePath, + minioPath + }); + + return { + filename: file.name, + filePath, + fileType: file.type, + fileSize: file.size + }; + } catch (error) { + console.error('Error uploading mission attachment:', { + error, + userId, + missionId, + fileName: file.name, + errorType: error instanceof Error ? error.constructor.name : typeof error, + message: error instanceof Error ? error.message : String(error) + }); + throw error; + } +} + +// Generate a presigned URL for direct upload to Minio (for logo) +export async function generateMissionLogoUploadUrl( + userId: string, + missionId: string, + fileExtension: string +): Promise<{ uploadUrl: string; filePath: string }> { + const filePath = getMissionLogoPath(userId, missionId, fileExtension); + const minioPath = filePath.replace(/^missions\//, ''); + + // TODO: Implement presigned URL generation + // This would require additional AWS SDK functionality + throw new Error('Presigned URL generation not implemented'); +} + +// Generate a presigned URL for direct upload to Minio (for attachment) +export async function generateMissionAttachmentUploadUrl( + userId: string, + missionId: string, + filename: string +): Promise<{ uploadUrl: string; filePath: string }> { + const filePath = getMissionAttachmentPath(userId, missionId, filename); + const minioPath = filePath.replace(/^missions\//, ''); + + // TODO: Implement presigned URL generation + // This would require additional AWS SDK functionality + throw new Error('Presigned URL generation not implemented'); } \ No newline at end of file