From 634266756ab715e467ad4003e858c0c4c77cfed9 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 14:49:47 +0100 Subject: [PATCH] Pages corrections pages missions --- .../[missionId]/files/upload/route.ts | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/app/api/missions/[missionId]/files/upload/route.ts b/app/api/missions/[missionId]/files/upload/route.ts index 74edfd2..ce1ec57 100644 --- a/app/api/missions/[missionId]/files/upload/route.ts +++ b/app/api/missions/[missionId]/files/upload/route.ts @@ -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 { @@ -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() } });