feat: add CDATA section parser for UDF 1.8 template XML format
This commit is contained in:
@@ -451,10 +451,11 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
|
|||||||
if (zip) {
|
if (zip) {
|
||||||
const zipEntries = zip.getEntries().filter((e: any) => !e.isDirectory);
|
const zipEntries = zip.getEntries().filter((e: any) => !e.isDirectory);
|
||||||
|
|
||||||
// 1. XML dosyalarını kontrol et (content.xml veya *.xml)
|
// 1. XML dosyalarını kontrol et (content.xml, *.xml veya template XML)
|
||||||
const xmlEntries = zipEntries.filter((entry: any) =>
|
const xmlEntries = zipEntries.filter((entry: any) => {
|
||||||
entry.entryName === "content.xml" || entry.entryName.toLowerCase().endsWith(".xml")
|
const name = entry.entryName.toLowerCase();
|
||||||
);
|
return name.includes('content') || name.endsWith('.xml') || name.includes('template');
|
||||||
|
});
|
||||||
|
|
||||||
// 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf)
|
// 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf)
|
||||||
const pdfEntry = zipEntries.find((entry: any) =>
|
const pdfEntry = zipEntries.find((entry: any) =>
|
||||||
@@ -480,7 +481,24 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (rawXml && rawXml.trim()) {
|
if (rawXml && rawXml.trim()) {
|
||||||
const plainText = rawXml.replace(/<style[\s\S]*?<\/style>/gi, '')
|
// A. UYAP CDATA bloğu kontrolü (<![CDATA[ ... ]]>): UYAP format 1.8 şablonlarında asıl metin buradadır
|
||||||
|
const cdataMatches = rawXml.match(/<!\[CDATA\[([\s\S]*?)\]\]>/gi);
|
||||||
|
if (cdataMatches && cdataMatches.length > 0) {
|
||||||
|
const cdataText = cdataMatches
|
||||||
|
.map(m => m.replace(/^<!\[CDATA\[/i, '').replace(/\]\]>$/, '').trim())
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('\n\n');
|
||||||
|
|
||||||
|
const cleanedCdata = sanitizePostgresText(cdataText);
|
||||||
|
if (cleanedCdata.length > 10) {
|
||||||
|
return cleanedCdata;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// B. Genel XML etiket temizliği
|
||||||
|
const plainText = rawXml
|
||||||
|
.replace(/<!\[CDATA\[[\s\S]*?\]\]>/gi, (m) => m.replace(/^<!\[CDATA\[/i, '').replace(/\]\]>$/, ''))
|
||||||
|
.replace(/<style[\s\S]*?<\/style>/gi, '')
|
||||||
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
.replace(/<script[\s\S]*?<\/script>/gi, '')
|
||||||
.replace(/<br\s*\/?>/gi, '\n')
|
.replace(/<br\s*\/?>/gi, '\n')
|
||||||
.replace(/<\/p>/gi, '\n')
|
.replace(/<\/p>/gi, '\n')
|
||||||
@@ -490,7 +508,6 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
|
|||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
const cleaned = sanitizePostgresText(plainText);
|
const cleaned = sanitizePostgresText(plainText);
|
||||||
// Sadece tek başına "content.xml" veya "content.xmlPK" olan kısa çöp dizgileri süz (Gerçek metinleri kesinlikle engelleme)
|
|
||||||
const isExactZipHeader = /^content\.xml(PK)?$/i.test(cleaned.trim()) && cleaned.length < 35;
|
const isExactZipHeader = /^content\.xml(PK)?$/i.test(cleaned.trim()) && cleaned.length < 35;
|
||||||
if (cleaned.length > 5 && !cleaned.startsWith('PK') && !isExactZipHeader) {
|
if (cleaned.length > 5 && !cleaned.startsWith('PK') && !isExactZipHeader) {
|
||||||
return cleaned;
|
return cleaned;
|
||||||
|
|||||||
Reference in New Issue
Block a user