Skip to content

Commit bd320f1

Browse files
committed
#70: Add heuristic to pick larger body parts
1 parent d1f797f commit bd320f1

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

src/main/java/mimeparser/MimeMessageParser.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,31 @@ public void walkMimeCallback(Part p, int level) throws Exception {
135135
return;
136136
}
137137

138-
String stringContent = getStringContent(p);
139-
boolean isAttachment = Part.ATTACHMENT.equalsIgnoreCase(p.getDisposition());
138+
// ignore attachments
139+
if (Part.ATTACHMENT.equalsIgnoreCase(p.getDisposition())) {
140+
return;
141+
}
140142

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

145-
// use text/plain entries only when we found nothing before
146-
if (result.getEntry().isEmpty() || p.isMimeType("text/html")) {
147-
result.setEntry(stringContent);
148-
result.setContentType(new ContentType(p.getContentType()));
148+
// ignore empty parts
149+
String stringContent = getStringContent(p);
150+
if (Strings.nullToEmpty(stringContent).trim().isEmpty()) {
151+
return;
149152
}
153+
154+
// ignore parts of same type and smaller size
155+
boolean partAndResultHaveSameContentType = result.getContentType().match(p.getContentType());
156+
boolean partContentIsSmallerThanResultContent = stringContent.length() < result.getEntry().length();
157+
if (partAndResultHaveSameContentType && partContentIsSmallerThanResultContent) {
158+
return;
159+
}
160+
161+
result.setEntry(stringContent);
162+
result.setContentType(new ContentType(p.getContentType()));
150163
}
151164
});
152165

0 commit comments

Comments
 (0)