Removing illegal filename characters also from attachment filename
This commit is contained in:
parent
a078b8bf66
commit
b37c4619d2
|
@ -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()) }
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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() }
|
||||
}
|
||||
|
|
|
@ -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("")
|
||||
|
||||
}
|
Loading…
Reference in New Issue