Skip to content

Commit

Permalink
fix inline CID replacement by not loading attachments a second time, …
Browse files Browse the repository at this point in the history
…and general CID improvement
  • Loading branch information
StevenMassaro committed Apr 1, 2024
1 parent bd19a1e commit 4f31761
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.massaro</groupId>
<artifactId>Email</artifactId>
<version>2.9.8</version>
<version>2.9.9</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/email/endpoint/AttachmentEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ public ResponseEntity<Resource> get(@RequestParam("id") long id) {
if (resourceResponseEntity != null) {
return resourceResponseEntity;
}

resourceResponseEntity = checkCid(id);
if (resourceResponseEntity != null) {
return resourceResponseEntity;
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "no message found with the corresponding attachment ID");
}

Expand All @@ -54,16 +49,4 @@ private ResponseEntity<Resource> checkAttachments(long id) {
}
return null;
}

private ResponseEntity<Resource> checkCid(long id) {
Optional<Message> messageWithAttachment = messageService.list().stream()
.filter(m -> m.getCidMap() != null && !m.getCidMap().isEmpty() && m.getCidMap().entrySet().stream().anyMatch(a -> a.getValue().getId() == id)).findFirst();
if (messageWithAttachment.isPresent()) {
Map.Entry<String, Attachment> attachment = messageWithAttachment.get().getCidMap().entrySet().stream().filter(a -> a.getValue().getId() == id).findFirst().orElse(null);
if (attachment != null) {
return attachment.getValue().toResponseEntity();
}
}
return null;
}
}
10 changes: 5 additions & 5 deletions src/main/java/email/endpoint/BodyEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public ResponseEntity<String> getBody(@RequestParam("id") long id) throws Except
}

// replace all cid (Content-Id) identifiers with a link to the attachment endpoint
for (Map.Entry<String, Attachment> cidMapEntry : message.getCidMap().entrySet()) {
Attachment attachment = cidMapEntry.getValue();
String contentId = cidMapEntry.getKey();

body = body.replace("cid:" + contentId, "./attachment?id=" + attachment.getId());
for (Attachment attachment : message.getAttachments()) {
String contentId = attachment.getContentId();
if (StringUtils.isNotEmpty(contentId)) {
body = body.replace("cid:" + contentId, "./attachment?id=" + attachment.getId());
}
}

// add open in new tab to all links
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/email/model/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Attachment {
private String contentType;
@JsonIgnore
private byte[] file;
private String contentId;

public Attachment(String name, String contentType, byte[] file) {
this.id = MessageService.attachmentIdSequence.incrementAndGet();
Expand All @@ -52,7 +53,7 @@ public static Attachment fromDataSource(DataSource dataSource) throws IOExceptio
String contentType = dataSource.getContentType();
byte[] file = IOUtils.toByteArray(dataSource.getInputStream());
if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(contentType) && file != null) {
return new Attachment(dataSource.getName(), dataSource.getContentType(), file);
return new Attachment(name, contentType, file);
}
return null;
}
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/email/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.mail.util.MimeMessageParser;
import org.springframework.beans.factory.annotation.Value;

import javax.activation.DataSource;
import javax.mail.Flags;
Expand Down Expand Up @@ -51,10 +50,6 @@ public class Message {
private List<BodyPart> bodyParts = new ArrayList<>();
private boolean readInd;
private List<Attachment> attachments = new ArrayList<>();
/**
* Map of Content-ID's to their respective attachments.
*/
private Map<String, Attachment> cidMap = new HashMap<>();

public Message() {

Expand All @@ -74,7 +69,7 @@ public Message(javax.mail.Message message,
mimeMessageParser.parse();
setAttachments(mimeMessageParser);
setBodyParts(mimeMessageParser);
setCidMap(mimeMessageParser);
setCidsOnAttachments(mimeMessageParser);
} catch (Exception e) {
log.warn("Failed to parse message using commons email parser, resorting to fallback method (which is less mature)", e);
try {
Expand Down Expand Up @@ -108,14 +103,13 @@ public Message(javax.mail.Message message,
this.readInd = determineReadInd(message);
}

private void setCidMap(MimeMessageParser mimeMessageParser) throws IOException {
private void setCidsOnAttachments(MimeMessageParser mimeMessageParser) {
Collection<String> contentIds = mimeMessageParser.getContentIds();
for (String contentId : contentIds) {
DataSource attachmentDs = mimeMessageParser.findAttachmentByCid(contentId);
Attachment attachment = Attachment.fromDataSource(attachmentDs);
if (attachment != null) {
this.cidMap.put(contentId, attachment);
}
this.attachments.stream()
.filter(a -> a.getName().equals(attachmentDs.getName()) && a.getContentType().equals(attachmentDs.getContentType()))
.findFirst().ifPresent(attachment -> attachment.setContentId(contentId));
}
}

Expand Down

0 comments on commit 4f31761

Please sign in to comment.