Added extension, size, contentType and disposition to EmailAttachment

This commit is contained in:
dankito 2024-11-28 01:33:31 +01:00
parent 1d7c07e7d6
commit b28403025c
3 changed files with 25 additions and 6 deletions

View File

@ -6,10 +6,7 @@ import jakarta.mail.event.MessageCountEvent
import jakarta.mail.internet.InternetAddress import jakarta.mail.internet.InternetAddress
import jakarta.mail.internet.MimeUtility import jakarta.mail.internet.MimeUtility
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.codinux.invoicing.email.model.Email import net.codinux.invoicing.email.model.*
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.filesystem.FileUtil import net.codinux.invoicing.filesystem.FileUtil
import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.model.Invoice
import net.codinux.invoicing.reader.EInvoiceReader import net.codinux.invoicing.reader.EInvoiceReader
@ -198,7 +195,7 @@ open class EmailsFetcher(
if (extension !in status.options.downloadAttachmentsWithExtensions) null if (extension !in status.options.downloadAttachmentsWithExtensions) null
else downloadAttachment(part, status) 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) { } catch (e: Throwable) {
log.error(e) { "Could not check attachment '${messagePart.part.fileName}' (${messagePart.mediaType}) for eInvoice" } log.error(e) { "Could not check attachment '${messagePart.part.fileName}' (${messagePart.mediaType}) for eInvoice" }
@ -208,6 +205,13 @@ open class EmailsFetcher(
return null 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<Invoice, File>? = try { protected open fun tryToReadEInvoice(part: Part, extension: String, mediaType: String?, status: FetchEmailsStatus): Pair<Invoice, File>? = try {
if (extension == "pdf" || mediaType == "application/pdf" || mediaType == "application/octet-stream") { if (extension == "pdf" || mediaType == "application/pdf" || mediaType == "application/octet-stream") {
val file = downloadAttachment(part, status) val file = downloadAttachment(part, status)

View File

@ -0,0 +1,11 @@
package net.codinux.invoicing.email.model
enum class ContentDisposition {
Body,
Inline,
Attachment,
Unknown
}

View File

@ -4,13 +4,17 @@ import net.codinux.invoicing.model.Invoice
import java.io.File import java.io.File
class EmailAttachment( 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", ... * Attachment's media type like "application/xml", "application/pdf", ...
* *
* Should always be non-null, but can theoretically be null. * Should always be non-null, but can theoretically be null.
*/ */
val mediaType: String?, val mediaType: String?,
val contentType: String?,
val invoice: Invoice? = null, val invoice: Invoice? = null,
val file: File? = null val file: File? = null
) { ) {