Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Ui/Compose/ContactCard/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ dependencies {
implementation(core.androidx.lifecycle.viewmodel.compose)
implementation(core.androidx.lifecycle.runtime.compose)
implementation(core.qrose)

testImplementation(kotlin("test-junit"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?, String?>): 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<String?, String?> {
val url = avatarUrl ?: return null to null

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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"))
}
}
Loading