Human-Library-Endpunkte

This commit is contained in:
Mohammad Zwaib
2026-07-21 23:43:39 +02:00
parent 825b3a371f
commit 65c507f667
17 changed files with 607 additions and 56 deletions
@@ -13,7 +13,8 @@ import java.util.concurrent.ExecutionException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.springframework.beans.factory.annotation.Autowired;
@@ -248,9 +249,15 @@ public class KnowledgeIngestService {
.retrieve()
.bodyToMono(byte[].class)
.block();
if(downloadHref.contains(".docx")) {
return extractText(fileBytes, downloadHref);
}
public String extractText(byte[] fileBytes, String fileName) throws Exception{
String name = (fileName == null) ? "" : fileName.toLowerCase();
if(name.contains(".docx")) {
try(XWPFDocument document = new XWPFDocument(new ByteArrayInputStream(fileBytes))) {
StringBuilder text = new StringBuilder();
@@ -265,14 +272,22 @@ public class KnowledgeIngestService {
throw new RuntimeException("Die DDC-Datei konnte nicht gelesen werden.");
}
}else if(downloadHref.contains(".doc")) {
}else if(name.contains(".doc")) {
try(HWPFDocument document = new HWPFDocument(new ByteArrayInputStream(fileBytes));
WordExtractor extractor = new WordExtractor(document)){
return extractor.getText();
}
}
else if(downloadHref.contains(".txt")){
else if(name.endsWith(".pdf")) {
try(PDDocument doc = PDDocument.load(fileBytes)){
String rawText= new PDFTextStripper().getText(doc);
return rawText;
}
}
else if(name.contains(".txt")){
return decodeBytes(fileBytes);
@@ -293,8 +308,10 @@ public class KnowledgeIngestService {
if (bytes.length >= 3 && (bytes[0]&0xFF)==0xEF && (bytes[1]&0xFF)==0xBB && (bytes[2]&0xFF)==0xBF)
return new String(bytes, 3, bytes.length-3, java.nio.charset.StandardCharsets.UTF_8);
if (bytes.length >= 2 && (bytes[0]&0xFF)==0xFF && (bytes[1]&0xFF)==0xFE)
return new String(bytes, 2, bytes.length-2, java.nio.charset.StandardCharsets.UTF_16LE);
if (bytes.length >= 2 && (bytes[0]&0xFF)==0xFE && (bytes[1]&0xFF)==0xFF)
return new String(bytes, 2, bytes.length-2, java.nio.charset.StandardCharsets.UTF_16BE);