new interactive
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import pdf from "pdf-parse"; // ✅ Import correct
|
||||
import mammoth from "mammoth";
|
||||
import { PresidioAnalyzerResult } from "@/app/config/entityLabels";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
console.log("🔍 Début du traitement de la requête");
|
||||
@@ -8,6 +9,9 @@ export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File | null;
|
||||
const category = (formData.get("category") as string) || "pii"; // Récupérer la catégorie
|
||||
|
||||
console.log("📊 Catégorie sélectionnée:", category);
|
||||
// ✅ Validation améliorée du fichier
|
||||
if (!file) {
|
||||
return NextResponse.json(
|
||||
@@ -169,9 +173,11 @@ export async function POST(req: NextRequest) {
|
||||
const analyzerConfig = {
|
||||
text: fileContent,
|
||||
language: "fr",
|
||||
mode: category, // Ajouter le mode basé sur la catégorie
|
||||
};
|
||||
|
||||
console.log("🔍 Appel à Presidio Analyzer...");
|
||||
console.log("📊 Configuration:", analyzerConfig);
|
||||
|
||||
// ✅ Définir l'URL AVANT de l'utiliser
|
||||
const presidioAnalyzerUrl =
|
||||
@@ -209,6 +215,7 @@ export async function POST(req: NextRequest) {
|
||||
const anonymizerConfig = {
|
||||
text: fileContent,
|
||||
analyzer_results: analyzerResults,
|
||||
mode: category, // Ajouter le mode pour l'anonymizer aussi
|
||||
};
|
||||
|
||||
console.log("🔍 Appel à Presidio Anonymizer...");
|
||||
@@ -236,77 +243,33 @@ export async function POST(req: NextRequest) {
|
||||
const anonymizerResult = await anonymizeResponse.json();
|
||||
console.log("✅ Anonymisation réussie.");
|
||||
|
||||
// 🔧 NOUVELLE FONCTION SIMPLIFIÉE pour extraire les valeurs de remplacement
|
||||
// Ajouter cette interface au début du fichier
|
||||
interface AnalyzerResult {
|
||||
entity_type: string;
|
||||
start: number;
|
||||
end: number;
|
||||
score: number;
|
||||
}
|
||||
|
||||
// Puis modifier la fonction
|
||||
const extractReplacementValues = (
|
||||
originalText: string,
|
||||
anonymizedText: string,
|
||||
analyzerResults: AnalyzerResult[]
|
||||
) => {
|
||||
const replacementMap: Record<string, string> = {};
|
||||
|
||||
// Trier les entités par position
|
||||
const sortedResults = [...analyzerResults].sort(
|
||||
(a, b) => a.start - b.start
|
||||
);
|
||||
|
||||
// Pour chaque entité, trouver son remplacement dans le texte anonymisé
|
||||
let searchOffset = 0;
|
||||
|
||||
sortedResults.forEach((result) => {
|
||||
const originalValue = originalText.substring(
|
||||
result.start,
|
||||
result.end
|
||||
);
|
||||
|
||||
// Chercher le prochain remplacement [XXX] après la position courante
|
||||
const replacementPattern = /\[[^\]]+\]/g;
|
||||
replacementPattern.lastIndex = searchOffset;
|
||||
const match = replacementPattern.exec(anonymizedText);
|
||||
|
||||
if (match) {
|
||||
replacementMap[originalValue] = match[0];
|
||||
searchOffset = match.index + match[0].length;
|
||||
console.log(
|
||||
`✅ Mapping positionnel: "${originalValue}" -> "${match[0]}"`
|
||||
);
|
||||
} else {
|
||||
// Fallback
|
||||
const fallbackValue = `[${result.entity_type.toUpperCase()}]`;
|
||||
replacementMap[originalValue] = fallbackValue;
|
||||
console.log(
|
||||
`⚠️ Fallback: "${originalValue}" -> "${fallbackValue}"`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("🔧 Mapping final:", replacementMap);
|
||||
return replacementMap;
|
||||
};
|
||||
|
||||
const replacementValues = extractReplacementValues(
|
||||
fileContent,
|
||||
anonymizerResult.anonymized_text,
|
||||
analyzerResults
|
||||
// 🎯 SOLUTION SIMPLIFIÉE : Utiliser directement le texte anonymisé de Presidio
|
||||
console.log(
|
||||
"✅ Texte anonymisé reçu de Presidio:",
|
||||
anonymizerResult.anonymized_text
|
||||
);
|
||||
|
||||
// 🔍 AJOUT D'UN LOG POUR DÉBOGUER
|
||||
console.log("🔧 Valeurs de remplacement extraites:", replacementValues);
|
||||
// Créer un mapping simple basé sur les entités détectées
|
||||
const replacementValues: Record<string, string> = {};
|
||||
|
||||
analyzerResults.forEach((result: PresidioAnalyzerResult) => {
|
||||
const originalValue = fileContent.substring(result.start, result.end);
|
||||
const replacementValue = result.entity_type; // ✅ CORRECTION : Utiliser entity_type au lieu de [ENTITY_TYPE]
|
||||
replacementValues[originalValue] = replacementValue;
|
||||
|
||||
console.log(
|
||||
`📝 Mapping créé: "${originalValue}" -> "${replacementValue}"`
|
||||
);
|
||||
});
|
||||
|
||||
const result = {
|
||||
text: fileContent,
|
||||
anonymizedText: anonymizerResult.anonymized_text,
|
||||
text: fileContent, // Texte original pour référence
|
||||
anonymizedText: anonymizerResult.anonymized_text, // Texte déjà anonymisé par Presidio
|
||||
piiCount: analyzerResults.length,
|
||||
analyzerResults: analyzerResults,
|
||||
replacementValues: replacementValues, // Utiliser les nouvelles valeurs
|
||||
replacementValues: replacementValues,
|
||||
// 🎯 NOUVEAU : Indiquer qu'on utilise directement le texte de Presidio
|
||||
usePresidioText: true,
|
||||
};
|
||||
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
|
||||
Reference in New Issue
Block a user