Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
Issue #206
  • Loading branch information
rsoika committed Feb 19, 2024
1 parent 9719da8 commit 4bce7fc
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void attachPDFMessage(Message message, ItemCollection workitem, String go
new ByteArrayInputStream(htmlMessage.getBytes(StandardCharsets.UTF_8)));
if (pdfContent != null) {
// attache file
String filename = message.getSubject() + ".pdf";
String filename = resolveSubjectToFileName(message) + ".pdf";
FileData fileData = new FileData(filename, pdfContent, "application/pdf", null);
workitem.addFileData(fileData);
}
Expand All @@ -117,9 +117,31 @@ public void attachMessage(Message message, ItemCollection workitem) throws IOExc
logger.fine("...attach message as eml file...");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
String filename = message.getSubject() + ".eml";
String filename = resolveSubjectToFileName(message) + ".eml";
FileData fileData = new FileData(filename, baos.toByteArray(), "message/rfc822", null);
workitem.addFileData(fileData);
}

/**
* Helper method to resolve the subject to a valid filename to be used to store
* .pdf and .eml files.
*
* @param message
* @return
* @throws MessagingException
*/
private String resolveSubjectToFileName(Message message) throws MessagingException {
String subject = "-- no subject --";
if (message.getSubject() != null && !message.getSubject().isEmpty()) {
subject = message.getSubject();
}
// Define a regex pattern for invalid file characters
String invalidCharsRegex = "[\\\\/:*?\"<>|]";
// Replace invalid characters with underscores
subject = subject.replaceAll(invalidCharsRegex, "_");
// remove leading and trailing whitespaces
subject = subject.trim();

return subject;
}
}

0 comments on commit 4bce7fc

Please sign in to comment.