Compare commits

..

No commits in common. "ddd6e01f1fedf1715e592a7ee63c73a69187920f" and "058fb62849c63f00223e5dfcf7ed3ee357782e6b" have entirely different histories.

7 changed files with 19 additions and 126 deletions

View File

@ -1,49 +0,0 @@
package net.codinux.invoicing.filesystem
import net.codinux.invoicing.model.Invoice
import net.codinux.invoicing.reader.EInvoiceReader
import net.codinux.log.logger
import java.nio.file.Path
import kotlin.io.path.*
class FilesystemInvoiceReader(
private val eInvoiceReader: EInvoiceReader = EInvoiceReader()
) {
private val log by logger()
fun readAllInvoicesOfDirectory(directory: Path, recursive: Boolean = false) =
readInvoicesFromFiles(collectFiles(directory, recursive))
private fun collectFiles(directory: Path, recursive: Boolean): List<Path> = buildList {
directory.listDirectoryEntries().forEach { child ->
if (child.isRegularFile()) {
add(child)
} else if (recursive && child.isDirectory()) {
addAll(collectFiles(child, recursive))
}
}
}
fun readInvoicesFromFiles(vararg files: Path) =
readInvoicesFromFiles(files.toList())
fun readInvoicesFromFiles(files: List<Path>): List<InvoiceOnFilesystem> =
files.mapNotNull { file -> readInvoiceFromFile(file)?.let { InvoiceOnFilesystem(file, it) } }
fun readInvoiceFromFile(file: Path): Invoice? = try {
val extension = file.extension.lowercase()
if (extension == "pdf") {
eInvoiceReader.extractFromPdf(file.inputStream())
} else if (extension == "xml") {
eInvoiceReader.readFromXml(file.inputStream())
} else {
null
}
} catch (e: Throwable) {
log.debug(e) { "Could not extract invoices from $file" }
null
}
}

View File

@ -1,12 +0,0 @@
package net.codinux.invoicing.filesystem
import net.codinux.invoicing.model.Invoice
import java.nio.file.Path
import kotlin.io.path.name
class InvoiceOnFilesystem(
val file: Path,
val invoice: Invoice
) {
override fun toString() = "${file.name} $invoice"
}

View File

@ -17,6 +17,10 @@ class MailReader(
private val eInvoiceReader: EInvoiceReader = EInvoiceReader()
) {
private val extractionErrorMessages = mutableSetOf<String?>()
private val extractionErrors = mutableSetOf<Throwable>()
private val log by logger()
@ -101,20 +105,23 @@ class MailReader(
return null
}
private fun tryToReadEInvoice(part: BodyPart): Invoice? = try {
private fun tryToReadEInvoice(part: BodyPart): Invoice? {
val filename = part.fileName.lowercase()
val contentType = part.contentType.lowercase()
if (filename.endsWith(".pdf") || contentType.startsWith("application/pdf") || contentType.startsWith("application/octet-stream")) {
eInvoiceReader.extractFromPdf(part.inputStream)
return if (filename.endsWith(".pdf") || contentType.startsWith("application/pdf") || contentType.startsWith("application/octet-stream")) {
try {
eInvoiceReader.extractFromPdf(part.inputStream)
} catch (e: Throwable) {
extractionErrorMessages.add(e.message)
extractionErrors.add(e)
null
}
} else if (filename.endsWith(".xml") || contentType.startsWith("application/xml") || contentType.startsWith("text/xml")) {
eInvoiceReader.readFromXml(part.inputStream)
} else {
null
}
} catch (e: Throwable) {
log.debug(e) { "Could not extract invoices from ${part.fileName}" }
null
}
// TODO: same code as in MustangMapper

View File

@ -1,41 +0,0 @@
package net.codinux.invoicing.filesystem
import assertk.assertThat
import assertk.assertions.hasSize
import net.codinux.invoicing.model.Invoice
import net.codinux.invoicing.test.InvoiceAsserter
import net.codinux.invoicing.test.TestUtils
import java.nio.file.Path
import kotlin.test.Test
class FilesystemInvoiceReaderTest {
private val underTest = FilesystemInvoiceReader()
@Test
fun readAllInvoicesOfDirectory() {
val testDirectory = getTestFile("XRechnung.xml").parent
val result = underTest.readAllInvoicesOfDirectory(testDirectory)
assertThat(result).hasSize(3)
}
@Test
fun readInvoiceFromFile() {
val xRechnung = getTestFile("XRechnung.xml")
val result = underTest.readInvoiceFromFile(xRechnung)
assertInvoice(result)
}
private fun getTestFile(filename: String): Path =
TestUtils.getTestFile(filename).toPath()
private fun assertInvoice(invoice: Invoice?) {
InvoiceAsserter.assertInvoice(invoice)
}
}

View File

@ -2,7 +2,7 @@ package net.codinux.invoicing.reader
import net.codinux.invoicing.model.Invoice
import net.codinux.invoicing.test.InvoiceAsserter
import net.codinux.invoicing.test.TestUtils
import java.io.InputStream
import kotlin.test.Test
class EInvoiceReaderTest {
@ -32,7 +32,8 @@ class EInvoiceReaderTest {
}
private fun getTestFile(filename: String) = TestUtils.getTestFileAsStream(filename)
private fun getTestFile(filename: String): InputStream =
this.javaClass.classLoader.getResourceAsStream("files/$filename")!!
private fun assertInvoice(invoice: Invoice?) {
InvoiceAsserter.assertInvoice(invoice)

View File

@ -1,14 +0,0 @@
package net.codinux.invoicing.test
import java.io.File
import java.io.InputStream
object TestUtils {
fun getTestFileAsStream(filename: String): InputStream =
this.javaClass.classLoader.getResourceAsStream("files/$filename")!!
fun getTestFile(filename: String): File =
File(this.javaClass.classLoader.getResource("files/$filename")!!.toURI())
}

View File

@ -2,7 +2,7 @@ package net.codinux.invoicing.validation
import assertk.assertThat
import assertk.assertions.*
import net.codinux.invoicing.test.TestUtils
import java.io.File
import kotlin.test.Test
class EInvoiceValidatorTest {
@ -54,6 +54,7 @@ class EInvoiceValidatorTest {
}
private fun getTestFile(filename: String) = TestUtils.getTestFile(filename)
private fun getTestFile(filename: String): File =
File(this.javaClass.classLoader.getResource("files/$filename")!!.toURI())
}