fix: PK signature offset searching for UDF files in backend

This commit is contained in:
mstfyldz
2026-08-14 13:23:48 +03:00
parent 6dd34fe703
commit 95478c09e8
+54 -30
View File
@@ -405,46 +405,70 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety
if (fileName.endsWith('.udf')) { if (fileName.endsWith('.udf')) {
try { try {
const zip = new AdmZip(file.buffer); let zip: AdmZip | null = null;
const zipEntries = zip.getEntries(); try {
zip = new AdmZip(file.buffer);
} catch (e) {
// PK\x03\x04 imzasını (0x50, 0x4B, 0x03, 0x04) buffer içinde ara (UYAP e-imza başlığı olan dosyalar için)
const pkSignature = Buffer.from([0x50, 0x4b, 0x03, 0x04]);
const pkIndex = file.buffer.indexOf(pkSignature);
if (pkIndex > 0) {
try {
zip = new AdmZip(file.buffer.subarray(pkIndex));
} catch (_) {}
}
}
// 1. Önce XML dosyasını kontrol et (content.xml veya *.xml) if (zip) {
const xmlEntry = zipEntries.find((entry: any) => const zipEntries = zip.getEntries().filter((e: any) => !e.isDirectory);
!entry.isDirectory && (entry.entryName === "content.xml" || entry.entryName.toLowerCase().endsWith(".xml"))
);
// 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf) // 1. Önce XML dosyasını kontrol et (content.xml veya *.xml)
const pdfEntry = zipEntries.find((entry: any) => const xmlEntry = zipEntries.find((entry: any) =>
!entry.isDirectory && entry.entryName.toLowerCase().endsWith(".pdf") entry.entryName === "content.xml" || entry.entryName.toLowerCase().endsWith(".xml")
); );
// Eğer XML bulunmuşsa XML üzerinden devam et // 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf)
if (xmlEntry) { const pdfEntry = zipEntries.find((entry: any) =>
const xmlData = zip.readAsText(xmlEntry); entry.entryName.toLowerCase().endsWith(".pdf")
if (xmlData && xmlData.trim()) { );
const plainText = xmlData.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
if (plainText.length > 20) { // Eğer XML bulunmuşsa XML üzerinden devam et
return plainText; if (xmlEntry) {
const rawXml = zip.readAsText(xmlEntry);
if (rawXml && rawXml.trim()) {
const plainText = rawXml.replace(/<style[\s\S]*?<\/style>/gi, '')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p>/gi, '\n')
.replace(/<[^>]+>/g, " ")
.replace(/&nbsp;/g, " ")
.replace(/\s+/g, " ")
.trim();
if (plainText.length > 20) {
return plainText;
}
}
}
// 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();
} }
} }
} }
// Eğer UDF içerisinden PDF çıktıysa PDF ayrıştırıcı (officeParser) üzerinden devam et // ZIP çözülemediyse düz XML / metin olarak oku
if (pdfEntry) { const rawText = file.buffer.toString('utf8');
const pdfBuffer = pdfEntry.getData(); if (rawText && rawText.trim().length > 20) {
const pdfResult: any = await officeParser.parseOffice(pdfBuffer); const plainText = rawText.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
const pdfText = typeof pdfResult === 'string' ? pdfResult : (pdfResult?.toText ? pdfResult.toText() : String(pdfResult || '')); if (plainText.length > 20) {
if (pdfText && pdfText.trim()) { return plainText;
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ı."); throw new Error("UDF/ZIP arşivinde okunabilir XML veya PDF içeriği bulunamadı.");
} catch (err: any) { } catch (err: any) {
console.error("UDF parse error:", err); console.error("UDF parse error:", err);