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? {
|
protected open fun findAttachment(messagePart: MessagePart, status: FetchEmailsStatus): EmailAttachment? {
|
||||||
try {
|
try {
|
||||||
val part = messagePart.part
|
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)) {
|
if (invoice != null || Part.ATTACHMENT.equals(part.description, ignoreCase = true)) {
|
||||||
val filename = File(part.fileName)
|
val file = if (extension !in status.options.downloadAttachmentsWithExtensions) null
|
||||||
val file = File.createTempFile(filename.nameWithoutExtension, "." + filename.extension).also { file ->
|
else {
|
||||||
part.inputStream.use { it.copyTo(file.outputStream()) }
|
File.createTempFile(filename.nameWithoutExtension, "." + extension).also { file ->
|
||||||
file.deleteOnExit()
|
part.inputStream.use { it.copyTo(file.outputStream()) }
|
||||||
}
|
file.deleteOnExit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return EmailAttachment(part.fileName, messagePart.mediaType, invoice, file)
|
return EmailAttachment(part.fileName, messagePart.mediaType, invoice, file)
|
||||||
}
|
}
|
||||||
|
@ -165,12 +170,10 @@ open class EmailsFetcher(
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
protected open fun tryToReadEInvoice(part: Part, mediaType: String?, status: FetchEmailsStatus): Invoice? = try {
|
protected open fun tryToReadEInvoice(part: Part, extension: String, mediaType: String?, status: FetchEmailsStatus): Invoice? = try {
|
||||||
val filename = part.fileName?.lowercase() ?: ""
|
if (extension == "pdf" || mediaType == "application/pdf" || mediaType == "application/octet-stream") {
|
||||||
|
|
||||||
if (filename.endsWith(".pdf") || mediaType == "application/pdf" || mediaType == "application/octet-stream") {
|
|
||||||
eInvoiceReader.extractFromPdf(part.inputStream)
|
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)
|
eInvoiceReader.extractFromXml(part.inputStream)
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
|
|
|
@ -4,11 +4,19 @@ import net.codinux.invoicing.email.model.Email
|
||||||
|
|
||||||
open class FetchEmailsOptions(
|
open class FetchEmailsOptions(
|
||||||
val downloadMessageBody: Boolean = false,
|
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 emailFolderName: String = "INBOX",
|
||||||
|
|
||||||
val onError: ((FetchEmailsError) -> Unit)? = null,
|
val onError: ((FetchEmailsError) -> Unit)? = null,
|
||||||
val onEmailReceived: ((Email) -> Unit)? = null
|
val onEmailReceived: ((Email) -> Unit)? = null
|
||||||
) {
|
) {
|
||||||
|
companion object {
|
||||||
|
val DefaultDownloadedAttachmentsWithExtensions = listOf("pdf", "xml")
|
||||||
|
}
|
||||||
|
|
||||||
fun emailReceived(email: Email) {
|
fun emailReceived(email: Email) {
|
||||||
onEmailReceived?.invoke(email)
|
onEmailReceived?.invoke(email)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,9 @@ open class ListenForNewMailsOptions(
|
||||||
val stopListening: AtomicBoolean = AtomicBoolean(false),
|
val stopListening: AtomicBoolean = AtomicBoolean(false),
|
||||||
|
|
||||||
downloadMessageBody: Boolean = false,
|
downloadMessageBody: Boolean = false,
|
||||||
|
downloadAttachmentsWithExtensions: List<String> = DefaultDownloadedAttachmentsWithExtensions,
|
||||||
emailFolderName: String = "INBOX",
|
emailFolderName: String = "INBOX",
|
||||||
|
|
||||||
onError: ((FetchEmailsError) -> Unit)? = null,
|
onError: ((FetchEmailsError) -> Unit)? = null,
|
||||||
onEmailReceived: (Email) -> Unit
|
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.
|
* Should always be non-null, but can theoretically be null.
|
||||||
*/
|
*/
|
||||||
val mediaType: String?,
|
val mediaType: String?,
|
||||||
val invoice: Invoice?,
|
val invoice: Invoice? = null,
|
||||||
val file: File
|
val file: File? = null
|
||||||
) {
|
) {
|
||||||
override fun toString() = "$filename: $invoice"
|
override fun toString() = "$filename: $invoice"
|
||||||
}
|
}
|
Loading…
Reference in New Issue