diff --git a/src/controllers/document.controller.ts b/src/controllers/document.controller.ts index ddb2b74..9a0b62e 100644 --- a/src/controllers/document.controller.ts +++ b/src/controllers/document.controller.ts @@ -405,46 +405,70 @@ export async function extractTextFromUploadedFile(file: { buffer: Buffer; mimety if (fileName.endsWith('.udf')) { try { - const zip = new AdmZip(file.buffer); - const zipEntries = zip.getEntries(); - - // 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")) - ); + let zip: AdmZip | null = null; + 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 (_) {} + } + } - // 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") - ); + if (zip) { + const zipEntries = zip.getEntries().filter((e: any) => !e.isDirectory); + + // 1. Önce XML dosyasını kontrol et (content.xml veya *.xml) + const xmlEntry = zipEntries.find((entry: any) => + entry.entryName === "content.xml" || entry.entryName.toLowerCase().endsWith(".xml") + ); - // 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; + // 2. İçerisinde gömülü PDF dosyası var mı kontrol et (*.pdf) + const pdfEntry = zipEntries.find((entry: any) => + entry.entryName.toLowerCase().endsWith(".pdf") + ); + + // Eğer XML bulunmuşsa XML üzerinden devam et + if (xmlEntry) { + const rawXml = zip.readAsText(xmlEntry); + if (rawXml && rawXml.trim()) { + const plainText = rawXml.replace(//gi, '') + .replace(//gi, '\n') + .replace(/<\/p>/gi, '\n') + .replace(/<[^>]+>/g, " ") + .replace(/ /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 - 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(); + // ZIP çözülemediyse düz XML / metin olarak oku + const rawText = file.buffer.toString('utf8'); + if (rawText && rawText.trim().length > 20) { + const plainText = rawText.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim(); + if (plainText.length > 20) { + return plainText; } } - // 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);