fix: filter out ZIP header metadata strings like content.xml and content.xmlPK from UDF text extractor

This commit is contained in:
Mustafa Yildiz
2026-08-15 11:03:26 +03:00
parent 9229c82806
commit 389bbe33ef
+16 -6
View File
@@ -490,7 +490,9 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
.trim(); .trim();
const cleaned = sanitizePostgresText(plainText); const cleaned = sanitizePostgresText(plainText);
if (cleaned.length > 5 && !cleaned.startsWith('PK')) { // Akıllı filtreleme: content.xml, content.xmlPK gibi ZIP başlık isimlerini metin sanma
const isZipNoise = /^content\.xml/i.test(cleaned) || cleaned.includes('content.xmlPK') || cleaned.includes('documentproperties.xml');
if (cleaned.length > 5 && !cleaned.startsWith('PK') && !isZipNoise) {
return cleaned; return cleaned;
} }
} }
@@ -507,7 +509,7 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
} }
} }
// 3. ZIP çözülemediyse ham buffer içinden okunabilir Türkçe metin bloklarını çek // 3. ZIP çözülemediyse ham buffer içinden okunabilir Türkçe metin bloklarını çek (İkili / ZIP başlık verisini süz)
let rawText = ''; let rawText = '';
try { rawText = iconv.decode(file.buffer, 'iso-8859-9'); } catch (_) { rawText = file.buffer.toString('utf8'); } try { rawText = iconv.decode(file.buffer, 'iso-8859-9'); } catch (_) { rawText = file.buffer.toString('utf8'); }
@@ -515,19 +517,27 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
if (textMatches && textMatches.length > 0) { if (textMatches && textMatches.length > 0) {
const cleanExtracted = textMatches const cleanExtracted = textMatches
.map(m => m.trim()) .map(m => m.trim())
.filter(m => m.length > 8 && !m.includes('documentproperties') && !m.includes('sign.sgn') && !m.startsWith('PK')) .filter(m => {
const lower = m.toLowerCase();
return m.length > 8 &&
!lower.includes('documentproperties') &&
!lower.includes('content.xml') &&
!lower.includes('sign.sgn') &&
!lower.includes('manifest.xml') &&
!m.startsWith('PK');
})
.join('\n'); .join('\n');
const cleaned = sanitizePostgresText(cleanExtracted); const cleaned = sanitizePostgresText(cleanExtracted);
if (cleaned.length > 10) { if (cleaned.length > 10 && !cleaned.toLowerCase().includes('content.xml')) {
return cleaned; return cleaned;
} }
} }
return `[UDF Belgesi: ${file.originalname} (İçerik e-imzalı kilitli veya taranmış görsellidir)]`; return `[UDF Belgesi: ${file.originalname} (İçerik e-imzalı kilitli veya metinsizdir)]`;
} catch (err: any) { } catch (err: any) {
console.error("UDF parse error:", err); console.error("UDF parse error:", err);
return `[UDF Belgesi: ${file.originalname} - Ek metin olarak eklendi]`; return `[UDF Belgesi: ${file.originalname} - Şablon olarak klendi]`;
} }
} }