version nice

This commit is contained in:
nBiqoz
2025-07-26 21:39:49 +02:00
parent aa19bb82a0
commit 5ba4fdc450
16 changed files with 1462 additions and 517 deletions

View File

@@ -19,15 +19,16 @@ export async function POST(req: NextRequest) {
let fileContent = "";
const fileType = file.type;
// --- LOGIQUE D'EXTRACTION DE TEXTE (INCHANGÉE) ---
// --- LOGIQUE D'EXTRACTION DE TEXTE ---
if (fileType === "application/pdf") {
console.log("📄 Traitement PDF en cours...");
try {
const buffer = Buffer.from(await file.arrayBuffer());
const data = await pdf(buffer);
fileContent = data.text;
fileContent = data.text || "";
console.log("✅ Extraction PDF réussie, longueur:", fileContent.length);
} catch (pdfError) {
console.error("❌ Erreur PDF:", pdfError);
return NextResponse.json(
{
error: `Erreur traitement PDF: ${
@@ -45,12 +46,13 @@ export async function POST(req: NextRequest) {
try {
const arrayBuffer = await file.arrayBuffer();
const result = await mammoth.extractRawText({ arrayBuffer });
fileContent = result.value;
fileContent = result.value || "";
console.log(
"✅ Extraction Word réussie, longueur:",
fileContent.length
);
} catch (wordError) {
console.error("❌ Erreur Word:", wordError);
return NextResponse.json(
{
error: `Erreur traitement Word: ${
@@ -69,6 +71,7 @@ export async function POST(req: NextRequest) {
fileContent.length
);
} catch (textError) {
console.error("❌ Erreur texte:", textError);
return NextResponse.json(
{
error: `Erreur lecture texte: ${
@@ -88,12 +91,19 @@ export async function POST(req: NextRequest) {
);
}
// Vérifier si c'est juste pour l'extraction de texte (lecture simple)
const isSimpleExtraction =
req.headers.get("x-simple-extraction") === "true";
if (isSimpleExtraction) {
// Retourner juste le texte extrait
return NextResponse.json({ text: fileContent }, { status: 200 });
}
// ==========================================================
// CONFIGURATION PRESIDIO ANALYZER (SIMPLIFIÉE)
// CONFIGURATION PRESIDIO ANALYZER (pour l'anonymisation complète)
// ==========================================================
// Toute la configuration (recognizers, allow_list, etc.) est maintenant dans le default.yaml du service.
// L'API a juste besoin d'envoyer le texte et la langue.
const analyzerConfig = {
text: fileContent,
language: "fr",
@@ -104,70 +114,72 @@ export async function POST(req: NextRequest) {
const presidioAnalyzerUrl =
"http://analyzer.151.80.20.211.sslip.io/analyze";
const analyzeResponse = await fetch(presidioAnalyzerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(analyzerConfig),
});
try {
const analyzeResponse = await fetch(presidioAnalyzerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(analyzerConfig),
});
console.log("📊 Statut Analyzer:", analyzeResponse.status);
if (!analyzeResponse.ok) {
const errorBody = await analyzeResponse.text();
return NextResponse.json(
{ error: `Erreur Analyzer: ${errorBody}` },
{ status: 500 }
);
console.log("📊 Statut Analyzer:", analyzeResponse.status);
if (!analyzeResponse.ok) {
const errorBody = await analyzeResponse.text();
console.error("❌ Erreur Analyzer:", errorBody);
// Fallback: retourner juste le texte si Presidio n'est pas disponible
return NextResponse.json({ text: fileContent }, { status: 200 });
}
const analyzerResults = await analyzeResponse.json();
console.log("✅ Analyzer a trouvé", analyzerResults.length, "entités.");
// =========================================================================
// CONFIGURATION PRESIDIO ANONYMIZER
// =========================================================================
const anonymizerConfig = {
text: fileContent,
analyzer_results: analyzerResults,
};
console.log("🔍 Appel à Presidio Anonymizer...");
const presidioAnonymizerUrl =
"http://anonymizer.151.80.20.211.sslip.io/anonymize";
const anonymizeResponse = await fetch(presidioAnonymizerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(anonymizerConfig),
});
console.log("📊 Statut Anonymizer:", anonymizeResponse.status);
if (!anonymizeResponse.ok) {
const errorBody = await anonymizeResponse.text();
console.error("❌ Erreur Anonymizer:", errorBody);
// Fallback: retourner juste le texte si Presidio n'est pas disponible
return NextResponse.json({ text: fileContent }, { status: 200 });
}
const anonymizerResult = await anonymizeResponse.json();
console.log("✅ Anonymisation réussie.");
const result = {
text: fileContent,
anonymizedText: anonymizerResult.text,
piiCount: analyzerResults.length,
};
return NextResponse.json(result, { status: 200 });
} catch (presidioError) {
console.error("❌ Erreur Presidio:", presidioError);
// Fallback: retourner juste le texte extrait
return NextResponse.json({ text: fileContent }, { status: 200 });
}
const analyzerResults = await analyzeResponse.json();
console.log("✅ Analyzer a trouvé", analyzerResults.length, "entités.");
// =========================================================================
// CONFIGURATION PRESIDIO ANONYMIZER
// =========================================================================
// L'Anonymizer lira la config d'anonymisation du default.yaml de l'Analyzer
// ou vous pouvez définir des transformations spécifiques ici si besoin.
// Pour commencer, on envoie juste les résultats de l'analyse.
const anonymizerConfig = {
text: fileContent,
analyzer_results: analyzerResults,
};
console.log("🔍 Appel à Presidio Anonymizer...");
const presidioAnonymizerUrl =
"http://anonymizer.151.80.20.211.sslip.io/anonymize";
const anonymizeResponse = await fetch(presidioAnonymizerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(anonymizerConfig),
});
console.log("📊 Statut Anonymizer:", anonymizeResponse.status);
if (!anonymizeResponse.ok) {
const errorBody = await anonymizeResponse.text();
return NextResponse.json(
{ error: `Erreur Anonymizer: ${errorBody}` },
{ status: 500 }
);
}
const anonymizerResult = await anonymizeResponse.json();
console.log("✅ Anonymisation réussie.");
const result = {
anonymizedText: anonymizerResult.text,
piiCount: analyzerResults.length,
};
return NextResponse.json(result, { status: 200 });
} catch (err: unknown) {
console.error("❌ Erreur générale:", err);
return NextResponse.json(