-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
441 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* 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.wire.android.ui.home.conversations.usecase | ||
|
||
import android.net.Uri | ||
import com.wire.android.ui.home.conversations.model.AssetBundle | ||
import com.wire.android.util.FileManager | ||
import com.wire.android.util.dispatchers.DispatcherProvider | ||
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 javax.inject.Inject | ||
|
||
class HandleUriAssetUseCase @Inject constructor( | ||
private val getAssetSizeLimit: GetAssetSizeLimitUseCase, | ||
private val fileManager: FileManager, | ||
private val kaliumFileSystem: KaliumFileSystem, | ||
private val dispatchers: DispatcherProvider | ||
) { | ||
|
||
suspend fun invoke( | ||
uri: Uri, | ||
saveToDeviceIfInvalid: Boolean = false, | ||
Check warning on line 40 in app/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt Codecov / codecov/patchapp/src/main/kotlin/com/wire/android/ui/home/conversations/usecase/HandleUriAssetUseCase.kt#L40
|
||
audioPath: Path? = null, | ||
): Result = withContext(dispatchers.io()) { | ||
|
||
val tempCachePath = kaliumFileSystem.rootCachePath | ||
val assetBundle = fileManager.getAssetBundleFromUri( | ||
attachmentUri = uri, | ||
tempCachePath = tempCachePath, | ||
audioPath = audioPath | ||
) | ||
if (assetBundle != null) { | ||
// The max limit for sending assets changes between user and asset types. | ||
// Check [GetAssetSizeLimitUseCase] class for more detailed information about the real limits. | ||
val maxSizeLimitInBytes = getAssetSizeLimit(isImage = assetBundle.assetType == AttachmentType.IMAGE) | ||
|
||
if (assetBundle.dataSize <= maxSizeLimitInBytes) { | ||
return@withContext Result.Success(assetBundle) | ||
} else { | ||
if (saveToDeviceIfInvalid) { | ||
with(assetBundle) { | ||
fileManager.saveToExternalMediaStorage( | ||
fileName, | ||
dataPath, | ||
dataSize, | ||
mimeType, | ||
dispatchers | ||
) | ||
} | ||
} | ||
return@withContext Result.Failure.AssetTooLarge(assetBundle, maxSizeLimitInBytes.div(sizeOf1MB).toInt()) | ||
} | ||
} else { | ||
return@withContext Result.Failure.Unknown | ||
} | ||
} | ||
|
||
companion object { | ||
private const val sizeOf1MB = 1024 * 1024 | ||
} | ||
|
||
sealed class Result { | ||
data class Success(val assetBundle: AssetBundle) : Result() | ||
sealed class Failure : Result() { | ||
data class AssetTooLarge(val assetBundle: AssetBundle, val maxLimitInMB: Int) : Failure() | ||
data object Unknown : Failure() | ||
} | ||
} | ||
} |
Oops, something went wrong.