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 { authOptions } from "@/app/api/auth/options";
import { prisma } from '@/lib/prisma';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
// 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';
import { uploadMissionAttachment } from '@/lib/mission-uploads';
// Helper function to check if user can manage files (creator or gardien)
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 });
}
// Construct the S3 key
// Files are stored in MinIO without the "missions/" prefix
const s3Key = path ? `${missionId}/${path}/${file.name}` : `${missionId}/${file.name}`;
const filePath = `missions/${s3Key}`; // Full path for database
// Use the existing uploadMissionAttachment function from mission-uploads.ts
// This ensures we use the exact same S3 client configuration and logic
console.log(`[POST /api/missions/${missionId}/files/upload] Uploading file:`, {
fileName: file.name,
fileSize: file.size,
fileType: file.type,
path: path
});
// Convert File to Buffer
const arrayBuffer = await file.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
// Upload to S3
await missionsS3Client.send(new PutObjectCommand({
Bucket: MISSIONS_BUCKET,
Key: s3Key,
Body: buffer,
ContentType: file.type || 'application/octet-stream',
ACL: 'public-read'
}));
// Upload using the proven function from mission-uploads.ts
const uploadResult = await uploadMissionAttachment(userId, missionId, file);
console.log(`[POST /api/missions/${missionId}/files/upload] Upload successful:`, {
filePath: uploadResult.filePath
});
// Create attachment record in database
const attachment = await prisma.attachment.create({
data: {
filename: file.name,
filePath: filePath,
fileType: file.type || 'application/octet-stream',
fileSize: file.size,
filename: uploadResult.filename,
filePath: uploadResult.filePath,
fileType: uploadResult.fileType,
fileSize: uploadResult.fileSize,
missionId: missionId,
uploaderId: userId
}
@ -101,10 +86,10 @@ export async function POST(
success: true,
file: {
type: 'file',
name: file.name,
path: filePath,
key: filePath,
size: file.size,
name: uploadResult.filename,
path: uploadResult.filePath,
key: uploadResult.filePath,
size: uploadResult.fileSize,
lastModified: new Date().toISOString()
}
});