Skip to content

Commit 974f53b

Browse files
committed
decouple file type detector tika from the model
1 parent 0aa3b50 commit 974f53b

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.postmarkapp.postmark.client.data;
2+
3+
import org.apache.tika.Tika;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
public class FileDetails {
10+
public static String getFileName(String path) {
11+
return new File(path).getName();
12+
}
13+
public static byte[] getFileContent(String path) throws IOException {
14+
return Files.readAllBytes(Paths.get(path));
15+
}
16+
public static String getFileContentType(String path) throws IOException {
17+
return new Tika().detect(new File(path));
18+
}
19+
}

src/main/java/com/postmarkapp/postmark/client/data/model/message/BaseMessage.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.postmarkapp.postmark.client.data.model.message;
22

3-
import java.io.File;
3+
import com.postmarkapp.postmark.client.data.FileDetails;
44
import java.io.IOException;
5-
import java.nio.file.Files;
6-
import java.nio.file.Paths;
75
import java.util.*;
8-
import org.apache.tika.Tika;
96

107
/**
118
* Base email message object
@@ -238,7 +235,8 @@ public void setAttachments(List<Map<String, String>> attachments) {
238235
* @throws IOException
239236
*/
240237
public void addAttachment(String path) throws IOException {
241-
addAttachment(new File(path).getName(), readFileContent(path), readFileContentType(path));
238+
addAttachment(FileDetails.getFileName(path), FileDetails.getFileContent(path),
239+
FileDetails.getFileContentType(path));
242240
}
243241

244242
/**
@@ -248,7 +246,8 @@ public void addAttachment(String path) throws IOException {
248246
* @param contentId file content id, like "cid:image.jpg", very usefull for inline images
249247
*/
250248
public void addAttachment(String path, String contentId) throws IOException {
251-
addAttachment(new File(path).getName(), readFileContent(path), readFileContentType(path), contentId);
249+
addAttachment(FileDetails.getFileName(path), FileDetails.getFileContent(path),
250+
FileDetails.getFileContentType(path), contentId);
252251
}
253252

254253
/**
@@ -328,14 +327,6 @@ public void addAttachments(List<Map<String, String>> attachments) {
328327
attachments.forEach(this::addAttachment);
329328
}
330329

331-
private byte[] readFileContent(String path) throws IOException {
332-
return Files.readAllBytes(Paths.get(path));
333-
}
334-
335-
private String readFileContentType(String path) throws IOException {
336-
return new Tika().detect(new File(path));
337-
}
338-
339330
/**
340331
* Converts list of recipients which contains full name next to email address to a string.
341332
* By passing something like Map of: key -> John Smith, value -> [email protected]

src/main/java/com/postmarkapp/postmark/client/data/model/templates/BaseTemplatedMessage.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.postmarkapp.postmark.client.data.model.templates;
22

3+
import com.postmarkapp.postmark.client.data.FileDetails;
34
import com.postmarkapp.postmark.client.data.model.message.Header;
4-
import org.apache.tika.Tika;
55

6-
import java.io.File;
76
import java.io.IOException;
8-
import java.nio.file.Files;
9-
import java.nio.file.Paths;
107
import java.util.*;
118

129
/**
@@ -222,7 +219,8 @@ public void setAttachments(List<Map<String, String>> attachments) {
222219
* @param path file path
223220
*/
224221
public void addAttachment(String path) throws IOException {
225-
addAttachment(new File(path).getName(), readFileContent(path), readFileContentType(path));
222+
addAttachment(FileDetails.getFileName(path), FileDetails.getFileContent(path),
223+
FileDetails.getFileContentType(path));
226224
}
227225

228226
/**
@@ -232,7 +230,8 @@ public void addAttachment(String path) throws IOException {
232230
* @param contentId file content id, like "cid:image.jpg", very usefull for inline images
233231
*/
234232
public void addAttachment(String path, String contentId) throws IOException {
235-
addAttachment(new File(path).getName(), readFileContent(path), readFileContentType(path), contentId);
233+
addAttachment(FileDetails.getFileName(path), FileDetails.getFileContent(path),
234+
FileDetails.getFileContentType(path), contentId);
236235
}
237236

238237
/**
@@ -312,14 +311,6 @@ public void addAttachments(List<Map<String, String>> attachments) {
312311
attachments.forEach(this::addAttachment);
313312
}
314313

315-
private byte[] readFileContent(String path) throws IOException {
316-
return Files.readAllBytes(Paths.get(path));
317-
}
318-
319-
private String readFileContentType(String path) throws IOException {
320-
return new Tika().detect(new File(path));
321-
}
322-
323314
/**
324315
* This helper method allows setting list of recipients which will contain full name next to email address.
325316
* By passing something like Map of: key -> John Smith, value -> [email protected]

src/test/java/unit/data/MessageTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
import java.io.File;
1010
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.Paths;
1113
import java.util.ArrayList;
14+
import java.util.Base64;
1215
import java.util.HashMap;
1316
import java.util.List;
1417

@@ -188,13 +191,15 @@ void attachmentException() throws IOException {
188191

189192
@Test
190193
void addPdfAttachment() throws IOException {
194+
String path = getDefaultFilePath() + "/test.pdf";
191195
Message message = new Message("[email protected]","[email protected]","Hello world", "Hello world");
192-
message.addAttachment(getDefaultFilePath() + "/test.pdf" );
196+
message.addAttachment(path);
197+
String base64message = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(path)));
193198

194199
HashMap<String,String> attachment = (HashMap<String, String>) message.getAttachments().get(0);
195200
assertEquals(attachment.get("ContentType"), "application/pdf");
196-
assertEquals(attachment.get("Name"), new File(getDefaultFilePath() + "/test.pdf").getName());
197-
assertNotNull(attachment.get("Content"));
201+
assertEquals(attachment.get("Name"), new File(path).getName());
202+
assertEquals(attachment.get("Content"), base64message);
198203
assertEquals(attachment.get("ContentId"), null);
199204
}
200205

@@ -203,11 +208,12 @@ void addImageAttachment() throws IOException {
203208
String file = getDefaultFilePath() + "/test.jpg";
204209
Message message = new Message("[email protected]","[email protected]","Hello world", "Hello world");
205210
message.addAttachment(file);
211+
String base64message = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(file)));
206212

207213
HashMap<String,String> attachment = (HashMap<String, String>) message.getAttachments().get(0);
208214
assertEquals(attachment.get("ContentType"), "image/jpeg");
209215
assertEquals(attachment.get("Name"), new File(file).getName());
210-
assertNotNull(attachment.get("Content"));
216+
assertEquals(attachment.get("Content"), base64message);
211217
assertEquals(attachment.get("ContentId"), null);
212218
}
213219

@@ -230,11 +236,12 @@ void addAttachmentWithContentId() throws IOException {
230236
String file = getDefaultFilePath() + "/test.pdf";
231237
Message message = new Message("[email protected]","[email protected]","Hello world", "Hello world");
232238
message.addAttachment(file, contentId);
239+
String base64message = Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(file)));
233240

234241
HashMap<String,String> attachment = (HashMap<String, String>) message.getAttachments().get(0);
235242
assertEquals(attachment.get("ContentType"), "application/pdf");
236243
assertEquals(attachment.get("Name"), new File(file).getName());
237-
assertNotNull(attachment.get("Content"));
244+
assertEquals(attachment.get("Content"), base64message);
238245
assertEquals(attachment.get("ContentId"), contentId);
239246
}
240247

0 commit comments

Comments
 (0)