Implemented configuring which attachments should be downloaded
This commit is contained in:
parent
d70a748ad0
commit
b3f6f2dbc3
|
@ -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
|
||||
|
|
|
@ -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<String> = 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)
|
||||
}
|
||||
|
|
|
@ -7,8 +7,9 @@ open class ListenForNewMailsOptions(
|
|||
val stopListening: AtomicBoolean = AtomicBoolean(false),
|
||||
|
||||
downloadMessageBody: Boolean = false,
|
||||
downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
|
||||
emailFolderName: String = "INBOX",
|
||||
|
||||
onError: ((FetchEmailsError) -> Unit)? = null,
|
||||
onEmailReceived: (Email) -> Unit
|
||||
) : FetchEmailsOptions(downloadMessageBody, emailFolderName, onError, onEmailReceived)
|
||||
) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, emailFolderName, onError, onEmailReceived)
|
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue