Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private String configureInlineImagesForInternal(String body) {
- if(body.equals("")) {
- return "<p></p>";
- }
- JerryParser jerry = Jerry.jerry();
- //Jerry uses Lagarto behind the scenes, so we get the LagartoDOMBuilder to configure it
- LagartoDOMBuilder domBuilder = (LagartoDOMBuilder) jerry.getDOMBuilder();
- //We want to parse HTML, not XML, et al.
- domBuilder.enableHtmlMode();
- //By default, Lagarto pretends to handle IE conditional comments, but we want it to ignore
- //them and treat them as normal comments so the HTML roundtrips the parse
- domBuilder.setConditionalCommentExpression(null);
- domBuilder.setIgnoreComments(false);
- //With Lagarto configured, resume the regularly scheduled Jerry Springer show
- Jerry htmlBody = jerry.parse(body);
- Jerry imgs = htmlBody.find("img");
- imgs.forEach(img -> {
- String source = img.attr("src");
- if(source.startsWith("data") && source.contains("base64")) {
- /* case of byte data */
- String name = "cid:" + UUID.randomUUID();
- byte[] contentBytes = DatatypeConverter.parseBase64Binary(source.split(",")[1]);
- byte[] checksum = DigestUtils.sha256(contentBytes);
- String attachmentBase64 = DatatypeConverter.printHexBinary(checksum);
- File file = fileService.findFileByMD5(attachmentBase64);
- if (file == null) {
- FileModel fileModel = new FileModel();
- fileModel.setData(contentBytes);
- fileModel.setName(name);
- try {
- file = fileService.createFile(fileModel);
- } catch (IOException e) {
- logger.error("Unable to create file from mail attachment.", e);
- }
- }
- String base = "https://";
- if(config.isNotProduction()) {
- // no ssl on dev
- base = "http://";
- }
- /* set <img> src to file URL */
- img.attr("src", base + config.getHostname() + "/files/" + file.getId() + "/" + name);
- }
- });
- return htmlBody.html();
- }
Add Comment
Please, Sign In to add comment