Removing illegal filename characters also from attachment filename

This commit is contained in:
dankito 2024-11-26 06:14:14 +01:00
parent a078b8bf66
commit b37c4619d2
4 changed files with 16 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import kotlinx.coroutines.*
import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.email.model.EmailAccount
import net.codinux.invoicing.email.model.EmailAttachment import net.codinux.invoicing.email.model.EmailAttachment
import net.codinux.invoicing.email.model.Email import net.codinux.invoicing.email.model.Email
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
import net.codinux.log.logger import net.codinux.log.logger
@ -199,7 +200,7 @@ open class EmailsFetcher(
} }
private fun downloadAttachment(part: Part, status: FetchEmailsStatus) = 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()) } part.inputStream.use { it.copyTo(file.outputStream()) }
} }

View File

@ -21,8 +21,6 @@ open class FetchEmailsOptions(
val DefaultDownloadedAttachmentsWithExtensions = listOf("pdf", "xml") val DefaultDownloadedAttachmentsWithExtensions = listOf("pdf", "xml")
val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() } 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) { fun emailReceived(email: Email) {

View File

@ -4,6 +4,7 @@ import jakarta.mail.BodyPart
import jakarta.mail.Message import jakarta.mail.Message
import jakarta.mail.Part import jakarta.mail.Part
import net.codinux.invoicing.email.model.EmailAccount import net.codinux.invoicing.email.model.EmailAccount
import net.codinux.invoicing.filesystem.FileUtil
import java.io.File import java.io.File
data class FetchEmailsStatus( data class FetchEmailsStatus(
@ -13,7 +14,7 @@ data class FetchEmailsStatus(
) { ) {
val userAttachmentsDownloadDirectory: File by lazy { 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() } File(options.attachmentsDownloadDirectory, userDirName).also { it.mkdirs() }
} }

View File

@ -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("")
}