Skip to content

Commit

Permalink
more robust parsing of inline images. issue #146
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Oct 22, 2024
1 parent 71a524b commit e197323
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import static java.util.Optional.ofNullable;
import static javax.print.DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
import static org.apache.commons.lang3.StringUtils.isBlank;
import org.apache.log4j.Logger;
//import static org.apache.tika.mime.MediaType.TEXT_HTML;


class MimeMessageObject {

private static final Logger log=Logger.getLogger(MimeMessageObject.class.getName());

private String entry;
private ContentType contentType;
Expand Down Expand Up @@ -137,8 +140,17 @@ private Map<String, MimeMessageObject> getInlinedImage(Part part) throws Excepti
walkMimeStructure(part, 0, (p, level) -> {
String[] header = p.getHeader(CONTENT_ID);
if (p.isMimeType(IMAGE_TYPE) && nonNull(header)) {
String imageBase64 = Base64.getEncoder().encodeToString(IOUtils.toByteArray((BASE64DecoderStream) p.getContent()));
result.put(header[0], new MimeMessageObject(imageBase64, new ContentType(p.getContentType())));
if (p.getContent() != null && !(p.getContent() instanceof BASE64DecoderStream)) {
log.warn("inline image has content of instance " + p.getContent().getClass().getName());
} else {
try {
String imageBase64 = Base64.getEncoder().encodeToString(IOUtils.toByteArray((BASE64DecoderStream) p.getContent()));
result.put(header[0], new MimeMessageObject(imageBase64, new ContentType(p.getContentType())));
} catch (Throwable t) {
log.error("unable to get content of inline image - skipping", t);
}
}

}
});
return result;
Expand Down

0 comments on commit e197323

Please sign in to comment.