import { s3Client, putObject, generatePresignedUrl, S3_CONFIG, deleteObject } from '@/lib/s3'; import { PutObjectCommand } from '@aws-sdk/client-s3'; /** * Utilities for mission-related file uploads using Minio */ // Generate the mission logo path in Minio export function getMissionLogoPath(userId: string, missionId: string, fileExtension: string): string { return `user-${userId}/missions/${missionId}/logo${fileExtension}`; } // Generate the mission attachment path in Minio export function getMissionAttachmentPath(userId: string, missionId: string, filename: string): string { return `user-${userId}/missions/${missionId}/attachments/${filename}`; } // Upload mission logo to Minio export async function uploadMissionLogo( userId: string, missionId: string, file: File ): Promise<{ filePath: string }> { try { // Get file extension const fileExtension = file.name.substring(file.name.lastIndexOf('.')); // Create file path const filePath = getMissionLogoPath(userId, missionId, fileExtension); // Convert file to ArrayBuffer const arrayBuffer = await file.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); // Upload to Minio const command = new PutObjectCommand({ Bucket: S3_CONFIG.bucket, Key: filePath, Body: buffer, ContentType: file.type, }); await s3Client.send(command); return { filePath }; } catch (error) { console.error('Error uploading mission logo:', error); throw new Error('Failed to upload mission logo'); } } // Upload mission attachment to Minio export async function uploadMissionAttachment( userId: string, missionId: string, file: File ): Promise<{ filename: string, filePath: string, fileType: string, fileSize: number }> { try { // Create file path const filePath = getMissionAttachmentPath(userId, missionId, file.name); // Convert file to ArrayBuffer const arrayBuffer = await file.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); // Upload to Minio const command = new PutObjectCommand({ Bucket: S3_CONFIG.bucket, Key: filePath, Body: buffer, ContentType: file.type, }); await s3Client.send(command); return { filename: file.name, filePath, fileType: file.type, fileSize: file.size, }; } catch (error) { console.error('Error uploading mission attachment:', error); throw new Error('Failed to upload mission attachment'); } } // Generate presigned URL for direct browser upload of mission logo export async function generateMissionLogoUploadUrl( userId: string, missionId: string, fileExtension: string, expiresIn = 3600 ): Promise<{ uploadUrl: string, filePath: string }> { try { const filePath = getMissionLogoPath(userId, missionId, fileExtension); const uploadUrl = await generatePresignedUrl(filePath, expiresIn); return { uploadUrl, filePath }; } catch (error) { console.error('Error generating mission logo upload URL:', error); throw new Error('Failed to generate upload URL for mission logo'); } } // Generate presigned URL for direct browser upload of mission attachment export async function generateMissionAttachmentUploadUrl( userId: string, missionId: string, filename: string, expiresIn = 3600 ): Promise<{ uploadUrl: string, filePath: string }> { try { const filePath = getMissionAttachmentPath(userId, missionId, filename); const uploadUrl = await generatePresignedUrl(filePath, expiresIn); return { uploadUrl, filePath }; } catch (error) { console.error('Error generating mission attachment upload URL:', error); throw new Error('Failed to generate upload URL for mission attachment'); } } // Delete mission attachment from Minio export async function deleteMissionAttachment(filePath: string): Promise { try { await deleteObject(filePath); return true; } catch (error) { console.error('Error deleting mission attachment:', error); throw new Error('Failed to delete mission attachment'); } } // Delete mission logo from Minio export async function deleteMissionLogo(filePath: string): Promise { try { await deleteObject(filePath); return true; } catch (error) { console.error('Error deleting mission logo:', error); throw new Error('Failed to delete mission logo'); } }