From 8f8b582ab35ded46ea75b38b487150d23d8139b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Saleniuk?= <30429749+saleniuk@users.noreply.github.com> Date: Thu, 23 May 2024 09:24:05 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20recreate=20cache=20directories=20in=20ca?= =?UTF-8?q?se=20cache=20is=20cleared=20[WPB-7368]=20=F0=9F=8D=92=20(#3016)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yamil Medina --- .../sendmessage/SendMessageViewModel.kt | 11 +++++------ .../conversations/usecase/HandleUriAssetUseCase.kt | 11 +++++------ .../sharing/ImportMediaAuthenticatedViewModel.kt | 2 +- .../kotlin/com/wire/android/util/FileManager.kt | 14 ++++++-------- kalium | 2 +- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt index 7205fd09f69..f0c3f6d6c59 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/conversations/sendmessage/SendMessageViewModel.kt @@ -36,6 +36,7 @@ import com.wire.android.ui.home.conversations.usecase.HandleUriAssetUseCase import com.wire.android.ui.home.messagecomposer.model.ComposableMessageBundle import com.wire.android.ui.home.messagecomposer.model.MessageBundle import com.wire.android.ui.home.messagecomposer.model.Ping +import com.wire.android.util.AUDIO_MIME_TYPE import com.wire.android.util.ImageUtil import com.wire.android.util.dispatchers.DispatcherProvider import com.wire.android.util.getAudioLengthInMs @@ -67,8 +68,6 @@ import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import okio.Path -import okio.Path.Companion.toPath import javax.inject.Inject @Suppress("LongParameterList", "TooManyFunctions") @@ -165,8 +164,8 @@ class SendMessageViewModel @Inject constructor( is ComposableMessageBundle.AudioMessageBundle -> { handleAssetMessageBundle( attachmentUri = messageBundle.attachmentUri, - audioPath = messageBundle.attachmentUri.uri.path?.toPath(), - conversationId = messageBundle.conversationId + conversationId = messageBundle.conversationId, + specifiedMimeType = AUDIO_MIME_TYPE, ) } @@ -208,12 +207,12 @@ class SendMessageViewModel @Inject constructor( private suspend fun handleAssetMessageBundle( conversationId: ConversationId, attachmentUri: UriAsset, - audioPath: Path? = null + specifiedMimeType: String? = null, // specify a particular mimetype, otherwise it will be taken from the uri / file extension ) { when (val result = handleUriAsset.invoke( uri = attachmentUri.uri, saveToDeviceIfInvalid = attachmentUri.saveToDeviceIfInvalid, - audioPath = audioPath + specifiedMimeType = specifiedMimeType )) { is HandleUriAssetUseCase.Result.Failure.AssetTooLarge -> { assetTooLargeDialogState = AssetTooLargeDialogState.Visible( diff --git a/app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt b/app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt index 483ca16711c..8baf53de6fc 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt @@ -25,7 +25,7 @@ import com.wire.kalium.logic.data.asset.AttachmentType import com.wire.kalium.logic.data.asset.KaliumFileSystem import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase import kotlinx.coroutines.withContext -import okio.Path +import java.util.UUID import javax.inject.Inject class HandleUriAssetUseCase @Inject constructor( @@ -38,14 +38,13 @@ class HandleUriAssetUseCase @Inject constructor( suspend fun invoke( uri: Uri, saveToDeviceIfInvalid: Boolean = false, - audioPath: Path? = null, + specifiedMimeType: String? = null, // specify a particular mimetype, otherwise it will be taken from the uri / file extension ): Result = withContext(dispatchers.io()) { - - val tempCachePath = kaliumFileSystem.rootCachePath + val tempAssetPath = kaliumFileSystem.tempFilePath(UUID.randomUUID().toString()) val assetBundle = fileManager.getAssetBundleFromUri( attachmentUri = uri, - tempCachePath = tempCachePath, - audioPath = audioPath + assetDestinationPath = tempAssetPath, + specifiedMimeType = specifiedMimeType, ) if (assetBundle != null) { // The max limit for sending assets changes between user and asset types. diff --git a/app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt index 37e4e48047d..f83e4af30da 100644 --- a/app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt @@ -414,7 +414,7 @@ class ImportMediaAuthenticatedViewModel @Inject constructor( } private suspend fun handleImportedAsset(uri: Uri): ImportedMediaAsset? = withContext(dispatchers.io()) { - when (val result = handleUriAsset.invoke(uri, saveToDeviceIfInvalid = false, audioPath = null)) { + when (val result = handleUriAsset.invoke(uri, saveToDeviceIfInvalid = false)) { is HandleUriAssetUseCase.Result.Failure.AssetTooLarge -> mapToImportedAsset(result.assetBundle, result.maxLimitInMB) HandleUriAssetUseCase.Result.Failure.Unknown -> null diff --git a/app/src/main/kotlin/com/wire/android/util/FileManager.kt b/app/src/main/kotlin/com/wire/android/util/FileManager.kt index 62841cace17..cc10289c9cf 100644 --- a/app/src/main/kotlin/com/wire/android/util/FileManager.kt +++ b/app/src/main/kotlin/com/wire/android/util/FileManager.kt @@ -108,28 +108,26 @@ class FileManager @Inject constructor(@ApplicationContext private val context: C @Suppress("TooGenericExceptionCaught") suspend fun getAssetBundleFromUri( attachmentUri: Uri, - tempCachePath: Path, - audioPath: Path? = null, + assetDestinationPath: Path, + specifiedMimeType: String? = null, // specify a particular mimetype, otherwise it will be taken from the uri / file extension dispatcher: DispatcherProvider = DefaultDispatcherProvider(), ): AssetBundle? = withContext(dispatcher.io()) { try { val assetKey = UUID.randomUUID().toString() val assetFileName = context.getFileName(attachmentUri) ?: throw IOException("The selected asset has an invalid name") - val fullTempAssetPath = "$tempCachePath/${UUID.randomUUID()}".toPath() - val assetPath = audioPath ?: fullTempAssetPath - val mimeType = if (audioPath != null) AUDIO_MIME_TYPE else attachmentUri + val mimeType = specifiedMimeType ?: attachmentUri .getMimeType(context) .orDefault(DEFAULT_FILE_MIME_TYPE) val attachmentType = AttachmentType.fromMimeTypeString(mimeType) val assetSize = if (attachmentType == AttachmentType.IMAGE) { - attachmentUri.resampleImageAndCopyToTempPath(context, fullTempAssetPath) + attachmentUri.resampleImageAndCopyToTempPath(context, assetDestinationPath) } else { // TODO: We should add also a video resampling logic soon, that way we could drastically reduce as well the number // of video assets hitting the max limit. - copyToPath(attachmentUri, fullTempAssetPath) + copyToPath(attachmentUri, assetDestinationPath) } - AssetBundle(assetKey, mimeType, assetPath, assetSize, assetFileName, attachmentType) + AssetBundle(assetKey, mimeType, assetDestinationPath, assetSize, assetFileName, attachmentType) } catch (e: IOException) { appLogger.e("There was an error while obtaining the file from disk", e) null diff --git a/kalium b/kalium index 5b9d85f831c..323fad3c7ba 160000 --- a/kalium +++ b/kalium @@ -1 +1 @@ -Subproject commit 5b9d85f831c699525b6e2b4b5f96c9b21cf18055 +Subproject commit 323fad3c7ba910ccafaa3a279007d5ace1607e8c