Implemented option downloadOnlyPlainTextOrHtmlMessageBody

This commit is contained in:
dankito 2024-11-28 01:40:20 +01:00
parent b28403025c
commit d976f848de
3 changed files with 23 additions and 4 deletions

View File

@ -154,6 +154,7 @@ open class EmailsFetcher(
} }
val sender = message.from?.firstOrNull()?.let { map(it) } val sender = message.from?.firstOrNull()?.let { map(it) }
val plainTextBody = getPlainTextBody(messageBodyParts, status)
val email = Email( val email = Email(
sender, message.subject ?: "", map(message.sentDate ?: message.receivedDate), sender, message.subject ?: "", map(message.sentDate ?: message.receivedDate),
@ -162,7 +163,7 @@ open class EmailsFetcher(
status.folder.getUID(message), status.folder.getUID(message),
parts.any { it.mediaType == "application/pgp-encrypted" }, parts.any { it.mediaType == "application/pgp-encrypted" },
imapMessage?.contentLanguage?.firstOrNull(), imapMessage?.contentLanguage?.firstOrNull(),
getPlainTextBody(messageBodyParts, status), getHtmlBody(messageBodyParts, status), plainTextBody, getHtmlBody(messageBodyParts, status, plainTextBody),
attachments attachments
) )
@ -277,8 +278,13 @@ open class EmailsFetcher(
protected open fun getPlainTextBody(parts: Collection<MessagePart>, status: FetchEmailsStatus) = protected open fun getPlainTextBody(parts: Collection<MessagePart>, status: FetchEmailsStatus) =
if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/plain", status) else null if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/plain", status) else null
protected open fun getHtmlBody(parts: Collection<MessagePart>, status: FetchEmailsStatus) = protected open fun getHtmlBody(parts: Collection<MessagePart>, status: FetchEmailsStatus, plainTextBody: String?) =
if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/html", status) else null // 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<MessagePart>, mediaType: String, status: FetchEmailsStatus): String? = try { protected open fun getBodyWithMediaType(parts: Collection<MessagePart>, mediaType: String, status: FetchEmailsStatus): String? = try {
val partsForMediaType = parts.filter { it.mediaType == mediaType } val partsForMediaType = parts.filter { it.mediaType == mediaType }

View File

@ -10,6 +10,12 @@ open class FetchEmailsOptions(
val lastRetrievedMessageId: Long? = null, val lastRetrievedMessageId: Long? = null,
val downloadMessageBody: Boolean = false, 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. * Set the extension (without the dot) of files that should be downloaded.
*/ */

View File

@ -8,6 +8,8 @@ open class ListenForNewMailsOptions(
val stopListening: AtomicBoolean = AtomicBoolean(false), val stopListening: AtomicBoolean = AtomicBoolean(false),
downloadMessageBody: Boolean = false, downloadMessageBody: Boolean = false,
downloadOnlyPlainTextOrHtmlMessageBody: Boolean = false,
downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions, downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory,
@ -16,4 +18,9 @@ open class ListenForNewMailsOptions(
onError: ((FetchEmailsError) -> Unit)? = null, onError: ((FetchEmailsError) -> Unit)? = null,
onEmailReceived: (Email) -> Unit onEmailReceived: (Email) -> Unit
) : FetchEmailsOptions(null, downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived) ) : FetchEmailsOptions(
null,
downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody,
downloadAttachmentsWithExtensions, attachmentsDownloadDirectory,
emailFolderName, connectTimeoutSeconds, onError, onEmailReceived
)