From b3f6f2dbc381753b93a575fc866f7e0fbcb3d94a Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 26 Nov 2024 03:56:58 +0100 Subject: [PATCH] Implemented configuring which attachments should be downloaded --- .../codinux/invoicing/email/EmailsFetcher.kt | 25 +++++++++++-------- .../invoicing/email/FetchEmailsOptions.kt | 8 ++++++ .../email/ListenForNewMailsOptions.kt | 3 ++- .../invoicing/email/model/EmailAttachment.kt | 4 +-- 4 files changed, 26 insertions(+), 14 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 27757d5..6484e1e 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 @@ -146,14 +146,19 @@ open class EmailsFetcher( protected open fun findAttachment(messagePart: MessagePart, status: FetchEmailsStatus): EmailAttachment? { try { val part = messagePart.part - val invoice = tryToReadEInvoice(part, messagePart.mediaType, status) + val filename = File(part.fileName) + val extension = filename.extension + + val invoice = tryToReadEInvoice(part, extension, messagePart.mediaType, status) if (invoice != null || Part.ATTACHMENT.equals(part.description, ignoreCase = true)) { - val filename = File(part.fileName) - val file = File.createTempFile(filename.nameWithoutExtension, "." + filename.extension).also { file -> - part.inputStream.use { it.copyTo(file.outputStream()) } - file.deleteOnExit() - } + val file = if (extension !in status.options.downloadAttachmentsWithExtensions) null + else { + File.createTempFile(filename.nameWithoutExtension, "." + extension).also { file -> + part.inputStream.use { it.copyTo(file.outputStream()) } + file.deleteOnExit() + } + } return EmailAttachment(part.fileName, messagePart.mediaType, invoice, file) } @@ -165,12 +170,10 @@ open class EmailsFetcher( return null } - protected open fun tryToReadEInvoice(part: Part, mediaType: String?, status: FetchEmailsStatus): Invoice? = try { - val filename = part.fileName?.lowercase() ?: "" - - if (filename.endsWith(".pdf") || mediaType == "application/pdf" || mediaType == "application/octet-stream") { + protected open fun tryToReadEInvoice(part: Part, extension: String, mediaType: String?, status: FetchEmailsStatus): Invoice? = try { + if (extension == "pdf" || mediaType == "application/pdf" || mediaType == "application/octet-stream") { eInvoiceReader.extractFromPdf(part.inputStream) - } else if (filename.endsWith(".xml") || mediaType == "application/xml" || mediaType == "text/xml") { + } else if (extension == "xml" || mediaType == "application/xml" || mediaType == "text/xml") { eInvoiceReader.extractFromXml(part.inputStream) } else { null 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 182eeac..d9bb88e 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 @@ -4,11 +4,19 @@ import net.codinux.invoicing.email.model.Email open class FetchEmailsOptions( val downloadMessageBody: Boolean = false, + /** + * Set the extension (without the dot) of files that should be downloaded. + */ + val downloadAttachmentsWithExtensions: List = DefaultDownloadedAttachmentsWithExtensions, val emailFolderName: String = "INBOX", val onError: ((FetchEmailsError) -> Unit)? = null, val onEmailReceived: ((Email) -> Unit)? = null ) { + companion object { + val DefaultDownloadedAttachmentsWithExtensions = listOf("pdf", "xml") + } + fun emailReceived(email: Email) { onEmailReceived?.invoke(email) } 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 e70930a..812e507 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 @@ -7,8 +7,9 @@ open class ListenForNewMailsOptions( val stopListening: AtomicBoolean = AtomicBoolean(false), downloadMessageBody: Boolean = false, + downloadAttachmentsWithExtensions: List = DefaultDownloadedAttachmentsWithExtensions, emailFolderName: String = "INBOX", onError: ((FetchEmailsError) -> Unit)? = null, onEmailReceived: (Email) -> Unit -) : FetchEmailsOptions(downloadMessageBody, emailFolderName, onError, onEmailReceived) \ No newline at end of file +) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, emailFolderName, onError, onEmailReceived) \ No newline at end of file diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/EmailAttachment.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/EmailAttachment.kt index a89a012..80db8cb 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/EmailAttachment.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/EmailAttachment.kt @@ -11,8 +11,8 @@ class EmailAttachment( * Should always be non-null, but can theoretically be null. */ val mediaType: String?, - val invoice: Invoice?, - val file: File + val invoice: Invoice? = null, + val file: File? = null ) { override fun toString() = "$filename: $invoice" } \ No newline at end of file