From b37c4619d2e48ffc9c302cedbc98629d524f1a4a Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 26 Nov 2024 06:14:14 +0100 Subject: [PATCH] Removing illegal filename characters also from attachment filename --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 3 ++- .../codinux/invoicing/email/FetchEmailsOptions.kt | 2 -- .../net/codinux/invoicing/email/FetchEmailsStatus.kt | 3 ++- .../net/codinux/invoicing/filesystem/FileUtil.kt | 12 ++++++++++++ 4 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FileUtil.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 3779c9d..1ffe7d9 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 @@ -12,6 +12,7 @@ import kotlinx.coroutines.* import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.email.model.EmailAttachment import net.codinux.invoicing.email.model.Email +import net.codinux.invoicing.filesystem.FileUtil import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.reader.EInvoiceReader import net.codinux.log.logger @@ -199,7 +200,7 @@ open class EmailsFetcher( } private fun downloadAttachment(part: Part, status: FetchEmailsStatus) = - File(status.userAttachmentsDownloadDirectory, part.fileName).also { file -> + File(status.userAttachmentsDownloadDirectory, FileUtil.removeIllegalFileCharacters(part.fileName)).also { file -> part.inputStream.use { it.copyTo(file.outputStream()) } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt index bb62191..d066091 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt @@ -21,8 +21,6 @@ open class FetchEmailsOptions( val DefaultDownloadedAttachmentsWithExtensions = listOf("pdf", "xml") val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() } - - val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') // TODO: make generic for attachment filename } fun emailReceived(email: Email) { diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt index 439e2d5..9b3c1f0 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt @@ -4,6 +4,7 @@ import jakarta.mail.BodyPart import jakarta.mail.Message import jakarta.mail.Part import net.codinux.invoicing.email.model.EmailAccount +import net.codinux.invoicing.filesystem.FileUtil import java.io.File data class FetchEmailsStatus( @@ -13,7 +14,7 @@ data class FetchEmailsStatus( ) { val userAttachmentsDownloadDirectory: File by lazy { - val userDirName = account.username.map { if (it in FetchEmailsOptions.IllegalFileCharacters || it.code < 32) '_' else it }.joinToString("") + val userDirName = FileUtil.removeIllegalFileCharacters(account.username) File(options.attachmentsDownloadDirectory, userDirName).also { it.mkdirs() } } diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FileUtil.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FileUtil.kt new file mode 100644 index 0000000..791cfa3 --- /dev/null +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/filesystem/FileUtil.kt @@ -0,0 +1,12 @@ +package net.codinux.invoicing.filesystem + +object FileUtil { + + private val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') + + + fun removeIllegalFileCharacters(name: String, replacementChar: Char = '_') = name + .map { if (it in IllegalFileCharacters || it.code < 32) replacementChar else it } + .joinToString("") + +} \ No newline at end of file