139 lines
No EOL
4.3 KiB
TypeScript
139 lines
No EOL
4.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
import { randomUUID } from "crypto";
|
|
import { createSbServiceRole } from "@/lib/supabaseServer";
|
|
|
|
const s3Client = new S3Client({
|
|
region: process.env.AWS_REGION || "eu-west-3",
|
|
credentials: {
|
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID || "",
|
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "",
|
|
},
|
|
});
|
|
|
|
const BUCKET_NAME = (process.env.AWS_S3_BUCKET || "odentas-docs").trim();
|
|
|
|
// Types de fichiers autorisés et leurs tailles max
|
|
const ALLOWED_FILE_TYPES = {
|
|
'image/jpeg': 10 * 1024 * 1024, // 10MB
|
|
'image/jpg': 10 * 1024 * 1024, // 10MB
|
|
'image/png': 10 * 1024 * 1024, // 10MB
|
|
'application/pdf': 10 * 1024 * 1024 // 10MB
|
|
};
|
|
|
|
const FILE_TYPE_NAMES = {
|
|
piece_identite: "piece-identite",
|
|
attestation_secu: "attestation-secu",
|
|
rib: "rib",
|
|
medecine_travail: "medecine-travail",
|
|
autre: "autre"
|
|
};
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const formData = await req.formData();
|
|
const file = formData.get('file') as File;
|
|
const token = formData.get('token') as string; // Changé de 'matricule' à 'token'
|
|
const type = formData.get('type') as string;
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ error: "Aucun fichier fourni" }, { status: 400 });
|
|
}
|
|
|
|
if (!token) {
|
|
return NextResponse.json({ error: "Token d'accès manquant" }, { status: 400 });
|
|
}
|
|
|
|
if (!type || !FILE_TYPE_NAMES[type as keyof typeof FILE_TYPE_NAMES]) {
|
|
return NextResponse.json({ error: "Type de fichier invalide" }, { status: 400 });
|
|
}
|
|
|
|
// Vérifier le token et récupérer les infos du salarié
|
|
const supabase = createSbServiceRole();
|
|
const { data: tokenData, error: tokenError } = await supabase
|
|
.from('auto_declaration_tokens')
|
|
.select(`
|
|
salarie_id,
|
|
expires_at,
|
|
used,
|
|
salaries (
|
|
code_salarie
|
|
)
|
|
`)
|
|
.eq('token', token)
|
|
.single();
|
|
|
|
if (tokenError || !tokenData) {
|
|
return NextResponse.json({ error: "Token d'accès invalide" }, { status: 403 });
|
|
}
|
|
|
|
// Vérifier l'expiration du token
|
|
if (new Date() > new Date(tokenData.expires_at)) {
|
|
return NextResponse.json({ error: "Token d'accès expiré" }, { status: 403 });
|
|
}
|
|
|
|
const matricule = (tokenData.salaries as any)?.code_salarie;
|
|
if (!matricule) {
|
|
return NextResponse.json({ error: "Matricule du salarié non trouvé" }, { status: 404 });
|
|
}
|
|
|
|
// Vérifier le type de fichier
|
|
if (!ALLOWED_FILE_TYPES[file.type as keyof typeof ALLOWED_FILE_TYPES]) {
|
|
return NextResponse.json({
|
|
error: "Type de fichier non autorisé. Seuls les fichiers PDF, JPG et PNG sont acceptés"
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// Vérifier la taille du fichier
|
|
const maxSize = ALLOWED_FILE_TYPES[file.type as keyof typeof ALLOWED_FILE_TYPES];
|
|
if (file.size > maxSize) {
|
|
return NextResponse.json({
|
|
error: `Fichier trop volumineux. Taille maximum autorisée : ${maxSize / (1024 * 1024)}MB`
|
|
}, { status: 400 });
|
|
}
|
|
|
|
// Générer un nom de fichier unique
|
|
const fileExtension = file.name.split('.').pop() || '';
|
|
const fileName = `${FILE_TYPE_NAMES[type as keyof typeof FILE_TYPE_NAMES]}-${Date.now()}.${fileExtension}`;
|
|
const s3Key = `justif-salaries/${matricule}/${fileName}`;
|
|
|
|
// Convertir le fichier en buffer
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
|
|
// Upload vers S3
|
|
const uploadCommand = new PutObjectCommand({
|
|
Bucket: BUCKET_NAME,
|
|
Key: s3Key,
|
|
Body: buffer,
|
|
ContentType: file.type,
|
|
Metadata: {
|
|
'matricule': matricule,
|
|
'type': type,
|
|
'original-filename': file.name,
|
|
'uploaded-at': new Date().toISOString()
|
|
}
|
|
});
|
|
|
|
await s3Client.send(uploadCommand);
|
|
|
|
// Construire l'URL du fichier
|
|
const fileUrl = `https://${BUCKET_NAME}.s3.eu-west-3.amazonaws.com/${s3Key}`;
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
key: s3Key,
|
|
url: fileUrl,
|
|
filename: fileName,
|
|
originalName: file.name,
|
|
type: type,
|
|
matricule: matricule
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error("Erreur upload S3:", error);
|
|
return NextResponse.json(
|
|
{ error: "Erreur lors du téléchargement du fichier" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |