From 05a41c7ddf0f25219980f58c50d3813975b46644 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 25 Apr 2022 11:25:11 +0100 Subject: [PATCH] add support for unencrypted images --- .../app/dapk/st/messenger/DecryptingFetcher.kt | 14 +++++++++++--- .../app/dapk/st/messenger/MessengerScreen.kt | 4 +++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/DecryptingFetcher.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/DecryptingFetcher.kt index 3e3226c..0907592 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/DecryptingFetcher.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/DecryptingFetcher.kt @@ -11,6 +11,7 @@ import coil.fetch.SourceResult import coil.size.Size import okhttp3.OkHttpClient import okhttp3.Request +import okhttp3.Response import okio.Buffer import java.security.MessageDigest import javax.crypto.Cipher @@ -28,9 +29,15 @@ class DecryptingFetcher : Fetcher { override suspend fun fetch(pool: BitmapPool, data: RoomEvent.Image, size: Size, options: Options): FetchResult { val response = http.newCall(Request.Builder().url(data.imageMeta.url).build()).execute() - val outputStream = Buffer() + val outputStream = when { + data.imageMeta.keys != null -> handleEncrypted(response, data.imageMeta.keys!!) + else -> response.body()?.source() ?: throw IllegalArgumentException("No bitmap response found") + } - val keys = data.imageMeta.keys!! + return SourceResult(outputStream, null, DataSource.NETWORK) + } + + private fun handleEncrypted(response: Response, keys: RoomEvent.Image.ImageMeta.Keys): Buffer { val key = Base64.decode(keys.k.replace('-', '+').replace('_', '/'), Base64.DEFAULT) val initVectorBytes = Base64.decode(keys.iv, Base64.DEFAULT) @@ -45,6 +52,7 @@ class DecryptingFetcher : Fetcher { val d = ByteArray(CRYPTO_BUFFER_SIZE) var decodedBytes: ByteArray + val outputStream = Buffer() response.body()?.let { it.byteStream().use { read = it.read(d) @@ -56,7 +64,7 @@ class DecryptingFetcher : Fetcher { } } } - return SourceResult(outputStream, null, DataSource.NETWORK) + return outputStream } override fun key(data: RoomEvent.Image) = data.imageMeta.url diff --git a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt index 6060af0..7fe8bdf 100644 --- a/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt +++ b/features/messenger/src/main/kotlin/app/dapk/st/messenger/MessengerScreen.kt @@ -212,6 +212,8 @@ private fun LazyItemScope.AlignedBubble( } } +private val decryptingFetcher = DecryptingFetcher() + @Composable private fun MessageImage(content: BubbleContent) { Box(modifier = Modifier.padding(start = 6.dp)) { @@ -242,7 +244,7 @@ private fun MessageImage(content: BubbleContent) { modifier = Modifier.size(content.message.imageMeta.scaleMeta(LocalDensity.current, LocalConfiguration.current)), painter = rememberImagePainter( data = content.message, - builder = { fetcher(DecryptingFetcher()) } + builder = { fetcher(decryptingFetcher) } ), contentDescription = null, )