From d976f848dec61c37d012dc2474becb86ad2484aa Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 28 Nov 2024 01:40:20 +0100 Subject: [PATCH] Implemented option downloadOnlyPlainTextOrHtmlMessageBody --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 12 +++++++++--- .../codinux/invoicing/email/FetchEmailsOptions.kt | 6 ++++++ .../invoicing/email/ListenForNewMailsOptions.kt | 9 ++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index 3fed5da..e26e7b7 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -154,6 +154,7 @@ open class EmailsFetcher( } val sender = message.from?.firstOrNull()?.let { map(it) } + val plainTextBody = getPlainTextBody(messageBodyParts, status) val email = Email( sender, message.subject ?: "", map(message.sentDate ?: message.receivedDate), @@ -162,7 +163,7 @@ open class EmailsFetcher( status.folder.getUID(message), parts.any { it.mediaType == "application/pgp-encrypted" }, imapMessage?.contentLanguage?.firstOrNull(), - getPlainTextBody(messageBodyParts, status), getHtmlBody(messageBodyParts, status), + plainTextBody, getHtmlBody(messageBodyParts, status, plainTextBody), attachments ) @@ -277,8 +278,13 @@ open class EmailsFetcher( protected open fun getPlainTextBody(parts: Collection, status: FetchEmailsStatus) = if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/plain", status) else null - protected open fun getHtmlBody(parts: Collection, status: FetchEmailsStatus) = - if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/html", status) else null + protected open fun getHtmlBody(parts: Collection, status: FetchEmailsStatus, plainTextBody: String?) = + // in case of downloadOnlyPlainTextOrHtmlMessageBody == true, download html body only if there's no plain text body + if (status.options.downloadMessageBody && (status.options.downloadOnlyPlainTextOrHtmlMessageBody == false || plainTextBody == null)) { + getBodyWithMediaType(parts, "text/html", status) + } else { + null + } protected open fun getBodyWithMediaType(parts: Collection, mediaType: String, status: FetchEmailsStatus): String? = try { val partsForMediaType = parts.filter { it.mediaType == mediaType } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt index ceee833..9e83312 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt @@ -10,6 +10,12 @@ open class FetchEmailsOptions( val lastRetrievedMessageId: Long? = null, val downloadMessageBody: Boolean = false, + /** + * If set to true and message contains a plain text message body, then only the plain text message body is downloaded + * and the HTML message body ignored / not downloaded. Reduces process time about 50 % (if no attachments get downloaded). + */ + val downloadOnlyPlainTextOrHtmlMessageBody: Boolean = false, + /** * Set the extension (without the dot) of files that should be downloaded. */ diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt index 93b63db..6ba31f5 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt @@ -8,6 +8,8 @@ open class ListenForNewMailsOptions( val stopListening: AtomicBoolean = AtomicBoolean(false), downloadMessageBody: Boolean = false, + downloadOnlyPlainTextOrHtmlMessageBody: Boolean = false, + downloadAttachmentsWithExtensions: List = DefaultDownloadedAttachmentsWithExtensions, attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, @@ -16,4 +18,9 @@ open class ListenForNewMailsOptions( onError: ((FetchEmailsError) -> Unit)? = null, onEmailReceived: (Email) -> Unit -) : FetchEmailsOptions(null, downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived) \ No newline at end of file +) : FetchEmailsOptions( + null, + downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody, + downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, + emailFolderName, connectTimeoutSeconds, onError, onEmailReceived +) \ No newline at end of file