Pages corrections pages missions

This commit is contained in:
alma 2026-01-16 14:49:47 +01:00
parent e7c6623fb3
commit 634266756a

View File

@ -2,20 +2,7 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth'; import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options"; import { authOptions } from "@/app/api/auth/options";
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; import { uploadMissionAttachment } from '@/lib/mission-uploads';
// Use the exact same S3 client configuration as mission-uploads.ts
const missionsS3Client = new S3Client({
region: 'us-east-1',
endpoint: 'https://dome-api.slm-lab.net',
credentials: {
accessKeyId: process.env.MINIO_ACCESS_KEY || '4aBT4CMb7JIMMyUtp4Pl',
secretAccessKey: process.env.MINIO_SECRET_KEY || 'HGn39XhCIlqOjmDVzRK9MED2Fci2rYvDDgbLFElg'
},
forcePathStyle: true
});
const MISSIONS_BUCKET = 'missions';
// Helper function to check if user can manage files (creator or gardien) // Helper function to check if user can manage files (creator or gardien)
async function checkCanManage(userId: string, missionId: string): Promise<boolean> { async function checkCanManage(userId: string, missionId: string): Promise<boolean> {
@ -67,31 +54,29 @@ export async function POST(
return NextResponse.json({ error: 'File is required' }, { status: 400 }); return NextResponse.json({ error: 'File is required' }, { status: 400 });
} }
// Construct the S3 key // Use the existing uploadMissionAttachment function from mission-uploads.ts
// Files are stored in MinIO without the "missions/" prefix // This ensures we use the exact same S3 client configuration and logic
const s3Key = path ? `${missionId}/${path}/${file.name}` : `${missionId}/${file.name}`; console.log(`[POST /api/missions/${missionId}/files/upload] Uploading file:`, {
const filePath = `missions/${s3Key}`; // Full path for database fileName: file.name,
fileSize: file.size,
fileType: file.type,
path: path
});
// Convert File to Buffer // Upload using the proven function from mission-uploads.ts
const arrayBuffer = await file.arrayBuffer(); const uploadResult = await uploadMissionAttachment(userId, missionId, file);
const buffer = Buffer.from(arrayBuffer);
console.log(`[POST /api/missions/${missionId}/files/upload] Upload successful:`, {
// Upload to S3 filePath: uploadResult.filePath
await missionsS3Client.send(new PutObjectCommand({ });
Bucket: MISSIONS_BUCKET,
Key: s3Key,
Body: buffer,
ContentType: file.type || 'application/octet-stream',
ACL: 'public-read'
}));
// Create attachment record in database // Create attachment record in database
const attachment = await prisma.attachment.create({ const attachment = await prisma.attachment.create({
data: { data: {
filename: file.name, filename: uploadResult.filename,
filePath: filePath, filePath: uploadResult.filePath,
fileType: file.type || 'application/octet-stream', fileType: uploadResult.fileType,
fileSize: file.size, fileSize: uploadResult.fileSize,
missionId: missionId, missionId: missionId,
uploaderId: userId uploaderId: userId
} }
@ -101,10 +86,10 @@ export async function POST(
success: true, success: true,
file: { file: {
type: 'file', type: 'file',
name: file.name, name: uploadResult.filename,
path: filePath, path: uploadResult.filePath,
key: filePath, key: uploadResult.filePath,
size: file.size, size: uploadResult.fileSize,
lastModified: new Date().toISOString() lastModified: new Date().toISOString()
} }
}); });