From e1973234b5bfca5cb153cb418f9a9161445dd92a Mon Sep 17 00:00:00 2001 From: j-dimension Date: Tue, 22 Oct 2024 22:09:42 +0200 Subject: [PATCH] more robust parsing of inline images. issue #146 --- .../client/utils/convert/MimeMessageObject.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java index fa1ea6a39..344c81142 100644 --- a/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java +++ b/j-lawyer-client/src/com/jdimension/jlawyer/client/utils/convert/MimeMessageObject.java @@ -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; @@ -137,8 +140,17 @@ private Map 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;