From f668a1b8ea4de65de66b0f7c64956998e4443b6a Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 9 Aug 2022 21:26:12 +0100 Subject: [PATCH] adding mimetype and reusing filename image content properties --- .../kotlin/app/dapk/st/graph/AppModule.kt | 1 + .../message/internal/ImageContentReader.kt | 36 +++++++++++++++ .../message/internal/SendMessageUseCase.kt | 45 +------------------ .../ApiTimelineMessageContentDeserializer.kt | 5 +-- .../sync/internal/sync/RoomEventFactory.kt | 1 - .../src/test/kotlin/test/TestMatrix.kt | 1 + 6 files changed, 41 insertions(+), 48 deletions(-) create mode 100644 matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/ImageContentReader.kt diff --git a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt index 0776f5b..5ae5432 100644 --- a/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt +++ b/app/src/main/kotlin/app/dapk/st/graph/AppModule.kt @@ -443,6 +443,7 @@ internal class AndroidImageContentReader(private val contentResolver: ContentRes height = options.outHeight, width = options.outWidth, size = output.size.toLong(), + mimeType = options.outMimeType, fileName = androidUri.lastPathSegment ?: "file", content = output ) diff --git a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/ImageContentReader.kt b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/ImageContentReader.kt new file mode 100644 index 0000000..8395092 --- /dev/null +++ b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/ImageContentReader.kt @@ -0,0 +1,36 @@ +package app.dapk.st.matrix.message.internal + +interface ImageContentReader { + fun read(uri: String): ImageContent + + data class ImageContent( + val height: Int, + val width: Int, + val size: Long, + val fileName: String, + val mimeType: String, + val content: ByteArray + ) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as ImageContent + + if (height != other.height) return false + if (width != other.width) return false + if (size != other.size) return false + if (!content.contentEquals(other.content)) return false + + return true + } + + override fun hashCode(): Int { + var result = height + result = 31 * result + width + result = 31 * result + size.hashCode() + result = 31 * result + content.contentHashCode() + return result + } + } +} \ No newline at end of file diff --git a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt index 4166a2c..b094e4f 100644 --- a/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt +++ b/matrix/services/message/src/main/kotlin/app/dapk/st/matrix/message/internal/SendMessageUseCase.kt @@ -38,21 +38,15 @@ internal class SendMessageUseCase( } is MessageService.Message.ImageMessage -> { - println("Sending message") val imageContent = imageContentReader.read(message.content.uri) - - println("content: ${imageContent.size}") - - val uri = httpClient.execute(uploadRequest(imageContent.content, imageContent.fileName, "image/png")).contentUri - println("Got uri $uri") - + val uri = httpClient.execute(uploadRequest(imageContent.content, imageContent.fileName, imageContent.mimeType)).contentUri val request = sendRequest( roomId = message.roomId, eventType = EventType.ROOM_MESSAGE, txId = message.localId, content = MessageService.Message.Content.ImageContent( url = uri, - filename = "foobar.png", + filename = imageContent.fileName, MessageService.Message.Content.ImageContent.Info( height = imageContent.height, width = imageContent.width, @@ -66,38 +60,3 @@ internal class SendMessageUseCase( } } - - -interface ImageContentReader { - fun read(uri: String): ImageContent - - data class ImageContent( - val height: Int, - val width: Int, - val size: Long, - val fileName: String, - val content: ByteArray - ) { - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as ImageContent - - if (height != other.height) return false - if (width != other.width) return false - if (size != other.size) return false - if (!content.contentEquals(other.content)) return false - - return true - } - - override fun hashCode(): Int { - var result = height - result = 31 * result + width - result = 31 * result + size.hashCode() - result = 31 * result + content.contentHashCode() - return result - } - } -} \ No newline at end of file diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt index 615169b..4fd1604 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/request/ApiTimelineMessageContentDeserializer.kt @@ -19,10 +19,7 @@ internal object ApiTimelineMessageContentDeserializer : KSerializer ApiTimelineEvent.TimelineMessage.Content.Text.serializer().deserialize(decoder) "m.image" -> ApiTimelineEvent.TimelineMessage.Content.Image.serializer().deserialize(decoder) - else -> { - println(element) - ApiTimelineEvent.TimelineMessage.Content.Ignored - } + else -> ApiTimelineEvent.TimelineMessage.Content.Ignored } } diff --git a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt index 6541121..4dfcd7b 100644 --- a/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt +++ b/matrix/services/sync/src/main/kotlin/app/dapk/st/matrix/sync/internal/sync/RoomEventFactory.kt @@ -44,7 +44,6 @@ internal class RoomEventFactory( private fun ApiTimelineEvent.TimelineMessage.readImageMeta(userCredentials: UserCredentials): RoomEvent.Image.ImageMeta { val content = this.content as ApiTimelineEvent.TimelineMessage.Content.Image - println(content) return RoomEvent.Image.ImageMeta( content.info?.width, content.info?.height, diff --git a/test-harness/src/test/kotlin/test/TestMatrix.kt b/test-harness/src/test/kotlin/test/TestMatrix.kt index 7a5996a..b758207 100644 --- a/test-harness/src/test/kotlin/test/TestMatrix.kt +++ b/test-harness/src/test/kotlin/test/TestMatrix.kt @@ -338,6 +338,7 @@ class JavaImageContentReader : ImageContentReader { height = image.height, width = image.width, size = size, + mimeType = "image/${file.extension}", fileName = file.name, content = file.readBytes() )