From 4bce7fc821ce63e0b5a043aa46ef011367e64842 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Mon, 19 Feb 2024 14:50:39 +0100 Subject: [PATCH] impl Issue #206 --- .../importer/mail/MailMessageService.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/MailMessageService.java b/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/MailMessageService.java index 3c3a313..26f6332 100644 --- a/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/MailMessageService.java +++ b/imixs-archive-importer/src/main/java/org/imixs/archive/importer/mail/MailMessageService.java @@ -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); } @@ -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; + } }