NeahNew/lib/mission-uploads.ts
2025-05-06 10:51:17 +02:00

179 lines
5.4 KiB
TypeScript

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 {
console.log('=== Starting logo upload process ===');
console.log('Upload params:', { userId, missionId, fileName: file.name, fileSize: file.size, fileType: file.type });
// Get file extension
const fileExtension = file.name.substring(file.name.lastIndexOf('.'));
console.log('File extension:', fileExtension);
// Create file path
const filePath = getMissionLogoPath(userId, missionId, fileExtension);
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);
// Upload to Minio
console.log('Creating S3 command with bucket:', S3_CONFIG.bucket);
console.log('S3 config:', {
endpoint: S3_CONFIG.endpoint || 'MISSING!',
region: S3_CONFIG.region || 'MISSING!',
bucket: S3_CONFIG.bucket || 'MISSING!',
hasAccessKey: !!S3_CONFIG.accessKey || 'MISSING!',
hasSecretKey: !!S3_CONFIG.secretKey || 'MISSING!'
});
const command = new PutObjectCommand({
Bucket: S3_CONFIG.bucket,
Key: filePath,
Body: buffer,
ContentType: file.type,
});
console.log('Sending upload command to S3/Minio...');
try {
const result = await s3Client.send(command);
console.log('Upload successful, result:', result);
} catch (uploadError) {
console.error('S3 upload error details:', uploadError);
throw uploadError;
}
console.log('Upload complete, returning file path:', filePath);
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<boolean> {
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<boolean> {
try {
await deleteObject(filePath);
return true;
} catch (error) {
console.error('Error deleting mission logo:', error);
throw new Error('Failed to delete mission logo');
}
}