Skip to content

Commit

Permalink
#70: Add heuristic to pick larger body parts
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrussler committed Jun 15, 2024
1 parent d1f797f commit bd320f1
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/mimeparser/MimeMessageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,31 @@ public void walkMimeCallback(Part p, int level) throws Exception {
return;
}

String stringContent = getStringContent(p);
boolean isAttachment = Part.ATTACHMENT.equalsIgnoreCase(p.getDisposition());
// ignore attachments
if (Part.ATTACHMENT.equalsIgnoreCase(p.getDisposition())) {
return;
}

if (Strings.nullToEmpty(stringContent).trim().isEmpty() || isAttachment) {
// ignore text/plain part if we already found a text/html part
if (result.getContentType().match("text/html") && p.isMimeType("text/plain")) {
return;
}

// use text/plain entries only when we found nothing before
if (result.getEntry().isEmpty() || p.isMimeType("text/html")) {
result.setEntry(stringContent);
result.setContentType(new ContentType(p.getContentType()));
// ignore empty parts
String stringContent = getStringContent(p);
if (Strings.nullToEmpty(stringContent).trim().isEmpty()) {
return;
}

// ignore parts of same type and smaller size
boolean partAndResultHaveSameContentType = result.getContentType().match(p.getContentType());
boolean partContentIsSmallerThanResultContent = stringContent.length() < result.getEntry().length();
if (partAndResultHaveSameContentType && partContentIsSmallerThanResultContent) {
return;
}

result.setEntry(stringContent);
result.setContentType(new ContentType(p.getContentType()));
}
});

Expand Down

0 comments on commit bd320f1

Please sign in to comment.