feat: enhance UDF file extraction pipeline to unzip container and handle content.xml or embedded PDF files
This commit is contained in:
@@ -407,17 +407,48 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
|
||||
try {
|
||||
const zip = new AdmZip(file.buffer);
|
||||
const zipEntries = zip.getEntries();
|
||||
const contentEntry = zipEntries.find((entry: any) => entry.entryName === "content.xml" || entry.entryName.endsWith(".xml"));
|
||||
if (!contentEntry) {
|
||||
throw new Error("No XML content found inside UDF/ZIP");
|
||||
|
||||
// 1. Önce XML dosyasını kontrol et (content.xml veya *.xml)
|
||||
const xmlEntry = zipEntries.find((entry: any) =>
|
||||
!entry.isDirectory && (entry.entryName === "content.xml" || entry.entryName.toLowerCase().endsWith(".xml"))
|
||||
);
|
||||
|
||||
// 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf)
|
||||
const pdfEntry = zipEntries.find((entry: any) =>
|
||||
!entry.isDirectory && entry.entryName.toLowerCase().endsWith(".pdf")
|
||||
);
|
||||
|
||||
// Eğer XML bulunmuşsa XML üzerinden devam et
|
||||
if (xmlEntry) {
|
||||
const xmlData = zip.readAsText(xmlEntry);
|
||||
if (xmlData && xmlData.trim()) {
|
||||
const plainText = xmlData.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
||||
if (plainText.length > 20) {
|
||||
return plainText;
|
||||
}
|
||||
}
|
||||
}
|
||||
const xmlData = zip.readAsText(contentEntry);
|
||||
// Remove XML tags and replace with space, then replace multiple spaces with single space
|
||||
const plainText = xmlData.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
||||
return plainText;
|
||||
} catch (err) {
|
||||
|
||||
// Eğer UDF içerisinden PDF çıktıysa PDF ayrıştırıcı (officeParser) üzerinden devam et
|
||||
if (pdfEntry) {
|
||||
const pdfBuffer = pdfEntry.getData();
|
||||
const pdfResult: any = await officeParser.parseOffice(pdfBuffer);
|
||||
const pdfText = typeof pdfResult === 'string' ? pdfResult : (pdfResult?.toText ? pdfResult.toText() : String(pdfResult || ''));
|
||||
if (pdfText && pdfText.trim()) {
|
||||
return pdfText.trim();
|
||||
}
|
||||
}
|
||||
|
||||
// Yedek durum: Son çare XML metnini döndür
|
||||
if (xmlEntry) {
|
||||
const xmlData = zip.readAsText(xmlEntry);
|
||||
return xmlData.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
throw new Error("UDF/ZIP arşivinde okunabilir XML veya PDF içeriği bulunamadı.");
|
||||
} catch (err: any) {
|
||||
console.error("UDF parse error:", err);
|
||||
throw new Error("UDF dosyası okunamadı veya bozuk.");
|
||||
throw new Error(`UDF dosyası ayrıştırılamadı: ${err.message || 'Bozuk arşiv'}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user