Implemented option downloadOnlyPlainTextOrHtmlMessageBody
This commit is contained in:
parent
b28403025c
commit
d976f848de
|
@ -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<MessagePart>, status: FetchEmailsStatus) =
|
||||
if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/plain", status) else null
|
||||
|
||||
protected open fun getHtmlBody(parts: Collection<MessagePart>, status: FetchEmailsStatus) =
|
||||
if (status.options.downloadMessageBody) getBodyWithMediaType(parts, "text/html", status) else null
|
||||
protected open fun getHtmlBody(parts: Collection<MessagePart>, 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<MessagePart>, mediaType: String, status: FetchEmailsStatus): String? = try {
|
||||
val partsForMediaType = parts.filter { it.mediaType == mediaType }
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -8,6 +8,8 @@ open class ListenForNewMailsOptions(
|
|||
val stopListening: AtomicBoolean = AtomicBoolean(false),
|
||||
|
||||
downloadMessageBody: Boolean = false,
|
||||
downloadOnlyPlainTextOrHtmlMessageBody: Boolean = false,
|
||||
|
||||
downloadAttachmentsWithExtensions: List<String> = 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)
|
||||
) : FetchEmailsOptions(
|
||||
null,
|
||||
downloadMessageBody, downloadOnlyPlainTextOrHtmlMessageBody,
|
||||
downloadAttachmentsWithExtensions, attachmentsDownloadDirectory,
|
||||
emailFolderName, connectTimeoutSeconds, onError, onEmailReceived
|
||||
)
|
Loading…
Reference in New Issue