From f8056d26ed86e9aa6a720a23118d8d76bc97d285 Mon Sep 17 00:00:00 2001 From: Fabian DEVEL Date: Mon, 21 Sep 2026 16:33:48 +0200 Subject: [PATCH] fix(contact-card): remove cache prefix from VCard filename Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- Ui/Compose/ContactCard/build.gradle.kts | 2 ++ .../contactcard/ContactCardShareExt.kt | 9 ++--- .../contactcard/ContactCardShareExtTest.kt | 34 +++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 Ui/Compose/ContactCard/src/test/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExtTest.kt diff --git a/Ui/Compose/ContactCard/build.gradle.kts b/Ui/Compose/ContactCard/build.gradle.kts index 26006d934..bf61fc134 100644 --- a/Ui/Compose/ContactCard/build.gradle.kts +++ b/Ui/Compose/ContactCard/build.gradle.kts @@ -35,4 +35,6 @@ dependencies { implementation(core.androidx.lifecycle.viewmodel.compose) implementation(core.androidx.lifecycle.runtime.compose) implementation(core.qrose) + + testImplementation(kotlin("test-junit")) } diff --git a/Ui/Compose/ContactCard/src/main/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExt.kt b/Ui/Compose/ContactCard/src/main/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExt.kt index e354d1e6b..dd7110424 100644 --- a/Ui/Compose/ContactCard/src/main/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExt.kt +++ b/Ui/Compose/ContactCard/src/main/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExt.kt @@ -31,17 +31,18 @@ private const val MAX_AVATAR_SIZE = 5 * 1024 * 1024 // 5 MB private val ILLEGAL_FILE_NAME_CHARACTERS = Regex("[\\\\/:*?\"<>|]+") suspend fun Card.createShareFile(context: Context, avatarData: Pair): File = withContext(Dispatchers.IO) { - val fileName = "attachments_cache${firstName}_${lastName}.vcf" - val safeFileName = fileName.replace(ILLEGAL_FILE_NAME_CHARACTERS, "") - val directory = File(context.cacheDir, "attachments_cache").apply { mkdirs() } - File(directory, safeFileName).apply { + File(directory, createVCardFileName(firstName, lastName)).apply { val vCardContent = makeVCardString(avatarBase64 = avatarData.first, avatarMimeType = avatarData.second) writeText(vCardContent) } } +internal fun createVCardFileName(firstName: String, lastName: String): String { + return "${firstName}_${lastName}.vcf".replace(ILLEGAL_FILE_NAME_CHARACTERS, "") +} + suspend fun Card.getAvatarDataOrNull(): Pair { val url = avatarUrl ?: return null to null diff --git a/Ui/Compose/ContactCard/src/test/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExtTest.kt b/Ui/Compose/ContactCard/src/test/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExtTest.kt new file mode 100644 index 000000000..78cccf977 --- /dev/null +++ b/Ui/Compose/ContactCard/src/test/kotlin/com/infomaniak/core/ui/compose/contactcard/ContactCardShareExtTest.kt @@ -0,0 +1,34 @@ +/* + * Infomaniak Core - Android + * Copyright (C) 2026 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.core.ui.compose.contactcard + +import kotlin.test.Test +import kotlin.test.assertEquals + +class ContactCardShareExtTest { + + @Test + fun `VCard file name does not include cache directory name`() { + assertEquals("John_Doe.vcf", createVCardFileName(firstName = "John", lastName = "Doe")) + } + + @Test + fun `VCard file name removes illegal characters`() { + assertEquals("John_Doe.vcf", createVCardFileName(firstName = "Jo/hn", lastName = "D:oe")) + } +}