good presidio
This commit is contained in:
@@ -4,11 +4,15 @@ import mammoth from "mammoth";
|
|||||||
import { type File } from "buffer";
|
import { type File } from "buffer";
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
|
console.log("🔍 Début du traitement de la requête");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const formData = await req.formData();
|
const formData = await req.formData();
|
||||||
const file = formData.get("file") as File | null;
|
const file = formData.get("file") as File | null;
|
||||||
|
console.log("📁 Fichier reçu:", file?.name, file?.type);
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
console.log("❌ Aucun fichier reçu");
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Aucun fichier reçu." },
|
{ error: "Aucun fichier reçu." },
|
||||||
{ status: 400 }
|
{ status: 400 }
|
||||||
@@ -17,22 +21,87 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
let fileContent = "";
|
let fileContent = "";
|
||||||
const fileType = file.type;
|
const fileType = file.type;
|
||||||
|
console.log("🔍 Type de fichier:", fileType);
|
||||||
|
|
||||||
if (fileType === "application/pdf") {
|
if (fileType === "application/pdf") {
|
||||||
const buffer = Buffer.from(await file.arrayBuffer());
|
console.log("📄 Traitement PDF en cours...");
|
||||||
const data = await pdf(buffer);
|
try {
|
||||||
fileContent = data.text;
|
const buffer = Buffer.from(await file.arrayBuffer());
|
||||||
|
console.log("📊 Taille du buffer:", buffer.length);
|
||||||
|
|
||||||
|
const data = await pdf(buffer);
|
||||||
|
fileContent = data.text;
|
||||||
|
console.log(
|
||||||
|
"✅ Extraction PDF réussie, longueur du texte:",
|
||||||
|
fileContent.length
|
||||||
|
);
|
||||||
|
} catch (pdfError) {
|
||||||
|
console.error("❌ Erreur PDF:", pdfError);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `Erreur lors du traitement du PDF: ${
|
||||||
|
pdfError instanceof Error ? pdfError.message : "Erreur inconnue"
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
} else if (
|
} else if (
|
||||||
fileType ===
|
fileType ===
|
||||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||||
) {
|
) {
|
||||||
const arrayBuffer = await file.arrayBuffer();
|
console.log("📝 Traitement Word en cours...");
|
||||||
const result = await mammoth.extractRawText({ arrayBuffer });
|
try {
|
||||||
fileContent = result.value;
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
|
const result = await mammoth.extractRawText({ arrayBuffer });
|
||||||
|
fileContent = result.value;
|
||||||
|
console.log(
|
||||||
|
"✅ Extraction Word réussie, longueur du texte:",
|
||||||
|
fileContent.length
|
||||||
|
);
|
||||||
|
} catch (wordError) {
|
||||||
|
console.error("❌ Erreur Word:", wordError);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `Erreur lors du traitement du document Word: ${
|
||||||
|
wordError instanceof Error ? wordError.message : "Erreur inconnue"
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fileContent = await file.text();
|
console.log("📄 Traitement texte en cours...");
|
||||||
|
try {
|
||||||
|
fileContent = await file.text();
|
||||||
|
console.log(
|
||||||
|
"✅ Extraction texte réussie, longueur:",
|
||||||
|
fileContent.length
|
||||||
|
);
|
||||||
|
} catch (textError) {
|
||||||
|
console.error("❌ Erreur texte:", textError);
|
||||||
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `Erreur lors de la lecture du fichier texte: ${
|
||||||
|
textError instanceof Error ? textError.message : "Erreur inconnue"
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Vérification du contenu extrait
|
||||||
|
if (!fileContent || fileContent.trim().length === 0) {
|
||||||
|
console.log("⚠️ Contenu vide détecté");
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Le fichier ne contient pas de texte extractible." },
|
||||||
|
{ status: 400 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("🔍 Contenu extrait, longueur:", fileContent.length);
|
||||||
|
|
||||||
const analyzerConfig = {
|
const analyzerConfig = {
|
||||||
text: fileContent,
|
text: fileContent,
|
||||||
language: "fr",
|
language: "fr",
|
||||||
@@ -107,55 +176,112 @@ export async function POST(req: NextRequest) {
|
|||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log("🔍 Appel à Presidio Analyzer...");
|
||||||
const presidioAnalyzerUrl =
|
const presidioAnalyzerUrl =
|
||||||
"http://ocs00s000ssow8kssossocco.51.68.233.212.sslip.io/analyze";
|
"http://ocs00s000ssow8kssossocco.51.68.233.212.sslip.io/analyze";
|
||||||
|
|
||||||
const analyzeResponse = await fetch(presidioAnalyzerUrl, {
|
const analyzeResponse = await fetch(presidioAnalyzerUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
body: JSON.stringify(analyzerConfig),
|
body: JSON.stringify(analyzerConfig),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log("📊 Statut Analyzer:", analyzeResponse.status);
|
||||||
|
|
||||||
if (!analyzeResponse.ok) {
|
if (!analyzeResponse.ok) {
|
||||||
const errorBody = await analyzeResponse.text();
|
const errorBody = await analyzeResponse.text();
|
||||||
throw new Error(
|
console.error("❌ Erreur Analyzer:", errorBody);
|
||||||
`Erreur de l'analyseur Presidio (${analyzeResponse.status}): ${errorBody}`
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `Erreur de l'analyseur Presidio (${analyzeResponse.status}): ${errorBody}`,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const analyzerResults = await analyzeResponse.json();
|
let analyzerResults;
|
||||||
|
try {
|
||||||
|
analyzerResults = await analyzeResponse.json();
|
||||||
|
console.log("✅ Analyzer réussi, résultats:", analyzerResults.length);
|
||||||
|
} catch (jsonError) {
|
||||||
|
console.error("❌ Erreur parsing JSON Analyzer:", jsonError);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur lors du parsing de la réponse de l'analyseur" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("🔍 Appel à Presidio Anonymizer...");
|
||||||
const presidioAnonymizerUrl =
|
const presidioAnonymizerUrl =
|
||||||
"http://r8gko4kcwwk4sso40cc0gkg8.51.68.233.212.sslip.io/anonymize";
|
"http://r8gko4kcwwk4sso40cc0gkg8.51.68.233.212.sslip.io/anonymize";
|
||||||
|
|
||||||
const anonymizeResponse = await fetch(presidioAnonymizerUrl, {
|
const anonymizeResponse = await fetch(presidioAnonymizerUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
text: fileContent,
|
text: fileContent,
|
||||||
analyzer_results: analyzerResults,
|
analyzer_results: analyzerResults,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
console.log("📊 Statut Anonymizer:", anonymizeResponse.status);
|
||||||
|
|
||||||
if (!anonymizeResponse.ok) {
|
if (!anonymizeResponse.ok) {
|
||||||
const errorBody = await anonymizeResponse.text();
|
const errorBody = await anonymizeResponse.text();
|
||||||
throw new Error(
|
console.error("❌ Erreur Anonymizer:", errorBody);
|
||||||
`Erreur de l'anonymiseur Presidio (${anonymizeResponse.status}): ${errorBody}`
|
return NextResponse.json(
|
||||||
|
{
|
||||||
|
error: `Erreur de l'anonymiseur Presidio (${anonymizeResponse.status}): ${errorBody}`,
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const anonymizerResult = await anonymizeResponse.json();
|
let anonymizerResult;
|
||||||
|
try {
|
||||||
|
anonymizerResult = await anonymizeResponse.json();
|
||||||
|
console.log("✅ Anonymizer réussi");
|
||||||
|
} catch (jsonError) {
|
||||||
|
console.error("❌ Erreur parsing JSON Anonymizer:", jsonError);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur lors du parsing de la réponse de l'anonymiseur" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json({
|
const result = {
|
||||||
anonymizedText: anonymizerResult.text,
|
anonymizedText: anonymizerResult.text,
|
||||||
piiCount: analyzerResults.length,
|
piiCount: analyzerResults.length,
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("✅ Traitement terminé avec succès");
|
||||||
|
return NextResponse.json(result, {
|
||||||
|
status: 200,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
|
console.error("❌ Erreur générale:", err);
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
err instanceof Error
|
err instanceof Error
|
||||||
? err.message
|
? err.message
|
||||||
: "Une erreur inconnue est survenue sur le serveur.";
|
: "Une erreur inconnue est survenue sur le serveur.";
|
||||||
console.error("API Error:", err);
|
|
||||||
return NextResponse.json({ error: errorMessage }, { status: 500 });
|
return NextResponse.json(
|
||||||
|
{ error: errorMessage },
|
||||||
|
{
|
||||||
|
status: 500,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user