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 {
|
try {
|
||||||
const zip = new AdmZip(file.buffer);
|
const zip = new AdmZip(file.buffer);
|
||||||
const zipEntries = zip.getEntries();
|
const zipEntries = zip.getEntries();
|
||||||
const contentEntry = zipEntries.find((entry: any) => entry.entryName === "content.xml" || entry.entryName.endsWith(".xml"));
|
|
||||||
if (!contentEntry) {
|
// 1. Önce XML dosyasını kontrol et (content.xml veya *.xml)
|
||||||
throw new Error("No XML content found inside UDF/ZIP");
|
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
|
// Eğer UDF içerisinden PDF çıktıysa PDF ayrıştırıcı (officeParser) üzerinden devam et
|
||||||
const plainText = xmlData.replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
if (pdfEntry) {
|
||||||
return plainText;
|
const pdfBuffer = pdfEntry.getData();
|
||||||
} catch (err) {
|
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);
|
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'}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,15 +60,15 @@ SADECE anahtar kelimeleri aralarına boşluk koyarak yaz. Noktalama işareti, a
|
|||||||
return res.status(mcpResponse.status).json({ error: 'Arama servisi hata döndürdü', details: errorData });
|
return res.status(mcpResponse.status).json({ error: 'Arama servisi hata döndürdü', details: errorData });
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await mcpResponse.json();
|
let data = await mcpResponse.json();
|
||||||
|
|
||||||
// Hukuki niyet ve sonuç (örn. beraat, bozma) odaklı akıllı yeniden sıralama ve puanlama
|
// Hukuki niyet ve sonuç (örn. beraat, bozma) odaklı akıllı yeniden sıralama ve puanlama
|
||||||
let rawItems: any[] = data.decisions || data.results || data.data || data.hits || (Array.isArray(data) ? data : []);
|
let rawItems: any[] = data.decisions || data.results || data.data || data.hits || (Array.isArray(data) ? data : []);
|
||||||
|
|
||||||
if (Array.isArray(rawItems) && rawItems.length > 0) {
|
if (Array.isArray(rawItems) && rawItems.length > 0) {
|
||||||
const searchTerms = phrase.toLowerCase().split(/\s+/).filter(Boolean);
|
const searchTerms = phrase.toLowerCase().split(/\s+/).filter((Boolean) as (value: string) => boolean);
|
||||||
const isBeraatSearch = searchTerms.some(t => t.includes('beraat'));
|
const isBeraatSearch = searchTerms.some((t: string) => t.includes('beraat'));
|
||||||
const isBozmaSearch = searchTerms.some(t => t.includes('bozma') || t.includes('bozulması'));
|
const isBozmaSearch = searchTerms.some((t: string) => t.includes('bozma') || t.includes('bozulması'));
|
||||||
|
|
||||||
const scoredItems = rawItems.map((item: any) => {
|
const scoredItems = rawItems.map((item: any) => {
|
||||||
const fullText = (
|
const fullText = (
|
||||||
|
|||||||
Reference in New Issue
Block a user