good presidio

This commit is contained in:
nBiqoz
2025-06-18 13:45:23 +02:00
parent 4c0622e47e
commit 8c7a19e881
8 changed files with 898 additions and 469 deletions

View File

@@ -0,0 +1,161 @@
import { NextResponse, type NextRequest } from "next/server";
import pdf from "pdf-parse";
import mammoth from "mammoth";
import { type File } from "buffer";
export async function POST(req: NextRequest) {
try {
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) {
return NextResponse.json(
{ error: "Aucun fichier reçu." },
{ status: 400 }
);
}
let fileContent = "";
const fileType = file.type;
if (fileType === "application/pdf") {
const buffer = Buffer.from(await file.arrayBuffer());
const data = await pdf(buffer);
fileContent = data.text;
} else if (
fileType ===
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
) {
const arrayBuffer = await file.arrayBuffer();
const result = await mammoth.extractRawText({ arrayBuffer });
fileContent = result.value;
} else {
fileContent = await file.text();
}
const analyzerConfig = {
text: fileContent,
language: "fr",
ad_hoc_recognizers: [
{
name: "BelgianNRNRecognizer",
supported_entity: "BE_NATIONAL_REGISTER_NUMBER",
supported_language: "fr",
patterns: [
{
name: "NRN_Pattern",
regex:
"\\b(?:[0-9]{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01]))-?\\d{3}\\.?\\d{2}\\b",
score: 1.0,
},
],
context: ["registre national", "nrn", "niss"],
},
{
name: "BelgianEnterpriseRecognizer",
supported_entity: "BE_ENTERPRISE_NUMBER",
supported_language: "fr",
patterns: [
{
name: "BTW_Pattern",
regex: "\\bBE\\s?0\\d{3}\\.\\d{3}\\.\\d{3}\\b",
score: 0.95,
},
],
context: ["entreprise", "btw", "tva"],
},
{
name: "IBANRecognizer",
supported_entity: "IBAN",
supported_language: "fr",
patterns: [
{
name: "IBAN_Pattern",
regex: "\\b[A-Z]{2}\\d{2}\\s?(?:\\d{4}\\s?){4,7}\\d{1,4}\\b",
score: 0.95,
},
],
context: ["iban", "compte", "bancaire"],
},
{
name: "PhoneRecognizer",
supported_entity: "PHONE_NUMBER",
supported_language: "fr",
patterns: [
{
name: "Phone_Pattern",
regex:
"\\b(?:(?:\\+|00)(?:32|33|352)|0)\\s?[1-9](?:[\\s.-]?\\d{2}){3,4}\\b",
score: 0.8,
},
],
context: ["téléphone", "tel", "mobile", "gsm"],
},
{
name: "EmailRecognizer",
supported_entity: "EMAIL_ADDRESS",
supported_language: "fr",
patterns: [
{
name: "Email_Pattern",
regex: "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b",
score: 1.0,
},
],
context: ["email", "courriel", "adresse électronique"],
},
],
};
const presidioAnalyzerUrl =
"http://ocs00s000ssow8kssossocco.51.68.233.212.sslip.io/analyze";
const analyzeResponse = await fetch(presidioAnalyzerUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(analyzerConfig),
});
if (!analyzeResponse.ok) {
const errorBody = await analyzeResponse.text();
throw new Error(
`Erreur de l'analyseur Presidio (${analyzeResponse.status}): ${errorBody}`
);
}
const analyzerResults = await analyzeResponse.json();
const presidioAnonymizerUrl =
"http://r8gko4kcwwk4sso40cc0gkg8.51.68.233.212.sslip.io/anonymize";
const anonymizeResponse = await fetch(presidioAnonymizerUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: fileContent,
analyzer_results: analyzerResults,
}),
});
if (!anonymizeResponse.ok) {
const errorBody = await anonymizeResponse.text();
throw new Error(
`Erreur de l'anonymiseur Presidio (${anonymizeResponse.status}): ${errorBody}`
);
}
const anonymizerResult = await anonymizeResponse.json();
return NextResponse.json({
anonymizedText: anonymizerResult.text,
piiCount: analyzerResults.length,
});
} catch (err: unknown) {
const errorMessage =
err instanceof Error
? err.message
: "Une erreur inconnue est survenue sur le serveur.";
console.error("API Error:", err);
return NextResponse.json({ error: errorMessage }, { status: 500 });
}
}