Skip to content

Commit

Permalink
improved adding attachments for templates endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Apr 24, 2018
1 parent c761954 commit 6b9f759
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</developers>

<properties>
<postmark.version>1.1.2</postmark.version>
<postmark.version>1.1.3</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ public void addAttachment(String name, byte[] content, String contentType) {
/**
* Add attachments as hash. To add it, attachment needs to look like this:
*
* HashMap<String,String> attachment = new HashMap<>();
* attachment.put("Name", "filename.txt");
* attachment.put("Content", Base64.getEncoder().encodeToString("test content".getBytes()));
* attachment.put("ContentType", "application/text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ public void setInlineCss(Boolean inlineCss) {
this.inlineCss = inlineCss;
}

public List<Map<String, String>> getAttachments() {
return attachments;
}

public void setAttachments(List<Map<String, String>> attachments) {
this.attachments = attachments;
}

public String getFrom() {
return from;
}
Expand Down Expand Up @@ -184,27 +176,72 @@ public void clearHeaders() {
}
}

public List<Map<String, String>> getAttachments() {
return attachments;
}

public void setAttachments(List<Map<String, String>> attachments) {
this.attachments = attachments;
}

/**
* Add attachments from file path
*
* @param path file path
*/
public void addAttachment(String path) throws IOException {
addAttachment(new File(path).getName(), readFileContent(path), readFileContentType(path));
}

/**
* Add attachments by file details
*
* @param filename filename to show up in email
* @param content file content
* @param contentType file content type
*/
public void addAttachment(String filename, String content, String contentType) {
addAttachment(filename, content.getBytes(),contentType);
}

/**
* Add attachments by file details
*
* @param name filename to show up in email
* @param content file content
* @param contentType file content type
*/
public void addAttachment(String name, byte[] content, String contentType) {
Map<String, String> attachment = new HashMap<>();
attachment.put("Name", new File(path).getName());
attachment.put("Content", readFileContent(path));
attachment.put("ContentType", readFileContentType(path));
attachment.put("Name", name);
attachment.put("Content", Base64.getEncoder().encodeToString(content));
attachment.put("ContentType", contentType);

addAttachment(attachment);
}

/**
* Add attachments as hash. To add it, attachment needs to look like this:
*
* attachment.put("Name", "filename.txt");
* attachment.put("Content", Base64.getEncoder().encodeToString("test content".getBytes()));
* attachment.put("ContentType", "application/text");
*
* @param attachment hash map of attachment.
*/
public void addAttachment(Map<String, String> attachment) {
if (this.attachments == null) {
this.attachments = new ArrayList<>();
}
attachments.add(attachment);
}

public void addAttachments(List<Map<String, String>> attachments) {
attachments.forEach(this::addAttachment);
}

private String readFileContent(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path));
return Base64.getEncoder().encodeToString(bytes);
private byte[] readFileContent(String path) throws IOException {
return Files.readAllBytes(Paths.get(path));
}

private String readFileContentType(String path) {
Expand Down

0 comments on commit 6b9f759

Please sign in to comment.