Skip to content

Commit

Permalink
fix: normalize asset file name when exporting to external storage (#2161
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MohamadJaara authored Sep 1, 2023
1 parent 89b7526 commit 231b06f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/wire/android/util/FileManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import android.content.Context
import android.net.Uri
import com.wire.android.appLogger
import com.wire.android.ui.home.conversations.model.AssetBundle
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.android.util.dispatchers.DefaultDispatcherProvider
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.AttachmentType
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.withContext
import okio.Path
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/kotlin/com/wire/android/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ fun ContentResolver.copyFile(destinationUri: Uri, sourcePath: Path) {
}

private fun Context.saveFileDataToMediaFolder(assetName: String, downloadedDataPath: Path, fileSize: Long, mimeType: String): Uri? {
val normalizedFileName = assetName.normalizeFileName()

val resolver = contentResolver
val directory = Environment.getExternalStoragePublicDirectory(
when {
Expand All @@ -171,7 +173,7 @@ private fun Context.saveFileDataToMediaFolder(assetName: String, downloadedDataP
)
directory.mkdirs()
val contentValues = ContentValues().apply {
val availableAssetName = findFirstUniqueName(directory, assetName.ifEmpty { ATTACHMENT_FILENAME })
val availableAssetName = findFirstUniqueName(directory, normalizedFileName.ifEmpty { ATTACHMENT_FILENAME })
put(DISPLAY_NAME, availableAssetName)
put(MIME_TYPE, mimeType)
put(SIZE, fileSize)
Expand All @@ -189,7 +191,7 @@ private fun Context.saveFileDataToMediaFolder(assetName: String, downloadedDataP
val insertedUri = resolver.insert(externalContentUri, contentValues) ?: run {
val authority = getProviderAuthority()
// we need to find the next available name with copy counter by ourselves before copying
val availableAssetName = findFirstUniqueName(directory, assetName.ifEmpty { ATTACHMENT_FILENAME })
val availableAssetName = findFirstUniqueName(directory, normalizedFileName.ifEmpty { ATTACHMENT_FILENAME })
val destinationFile = File(directory, availableAssetName)
FileProvider.getUriForFile(this, authority, destinationFile)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/com/wire/android/util/StringUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ fun String.toTitleCase(delimiter: String = " ", separator: String = " "): String
}

fun String.capitalizeFirstLetter(): String = lowercase().replaceFirstChar(Char::titlecaseChar)

fun String.normalizeFileName(): String = this.replace("/", "")
8 changes: 8 additions & 0 deletions app/src/test/kotlin/com/wire/android/util/StringUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ class StringUtilTest {
val actual = input.toTitleCase()
assert(expected == actual)
}

@Test
fun givenString_whenNormalizingAsFileName_thenAllSlashesAreRemoved() {
val input = "this/is/a/test"
val expected = "thisisatest"
val actual = input.normalizeFileName()
assert(expected == actual)
}
}

0 comments on commit 231b06f

Please sign in to comment.