From b28403025c79122cfbea4fc997b7a50a7126431b Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 28 Nov 2024 01:33:31 +0100 Subject: [PATCH] Added extension, size, contentType and disposition to EmailAttachment --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 14 +++++++++----- .../invoicing/email/model/ContentDisposition.kt | 11 +++++++++++ .../invoicing/email/model/EmailAttachment.kt | 6 +++++- 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/ContentDisposition.kt 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 4f35bb9..3fed5da 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 @@ -6,10 +6,7 @@ import jakarta.mail.event.MessageCountEvent import jakarta.mail.internet.InternetAddress import jakarta.mail.internet.MimeUtility import kotlinx.coroutines.* -import net.codinux.invoicing.email.model.Email -import net.codinux.invoicing.email.model.EmailAccount -import net.codinux.invoicing.email.model.EmailAddress -import net.codinux.invoicing.email.model.EmailAttachment +import net.codinux.invoicing.email.model.* import net.codinux.invoicing.filesystem.FileUtil import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.reader.EInvoiceReader @@ -198,7 +195,7 @@ open class EmailsFetcher( if (extension !in status.options.downloadAttachmentsWithExtensions) null else downloadAttachment(part, status) - return EmailAttachment(part.fileName, messagePart.mediaType, invoiceAndFile?.first, file) + return EmailAttachment(part.fileName, extension, part.size.takeIf { it > 0 }, mapDisposition(part), messagePart.mediaType, part.contentType, invoiceAndFile?.first, file) } } catch (e: Throwable) { log.error(e) { "Could not check attachment '${messagePart.part.fileName}' (${messagePart.mediaType}) for eInvoice" } @@ -208,6 +205,13 @@ open class EmailsFetcher( return null } + private fun mapDisposition(part: Part) = when (part.disposition?.lowercase()) { + "inline" -> ContentDisposition.Inline + "attachment" -> ContentDisposition.Attachment + null -> ContentDisposition.Body + else -> ContentDisposition.Unknown + } + protected open fun tryToReadEInvoice(part: Part, extension: String, mediaType: String?, status: FetchEmailsStatus): Pair? = try { if (extension == "pdf" || mediaType == "application/pdf" || mediaType == "application/octet-stream") { val file = downloadAttachment(part, status) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/ContentDisposition.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/ContentDisposition.kt new file mode 100644 index 0000000..86a67d8 --- /dev/null +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/model/ContentDisposition.kt @@ -0,0 +1,11 @@ +package net.codinux.invoicing.email.model + +enum class ContentDisposition { + Body, + + Inline, + + Attachment, + + Unknown +} \ 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 80db8cb..e21169e 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 @@ -4,13 +4,17 @@ import net.codinux.invoicing.model.Invoice import java.io.File class EmailAttachment( - val filename: String, + val filename: String?, + val extension: String?, + val size: Int?, + val disposition: ContentDisposition, /** * Attachment's media type like "application/xml", "application/pdf", ... * * Should always be non-null, but can theoretically be null. */ val mediaType: String?, + val contentType: String?, val invoice: Invoice? = null, val file: File? = null ) {