adding mimetype and reusing filename image content properties

This commit is contained in:
Adam Brown 2022-08-09 21:26:12 +01:00
parent 934f80df1f
commit f668a1b8ea
6 changed files with 41 additions and 48 deletions

View File

@ -443,6 +443,7 @@ internal class AndroidImageContentReader(private val contentResolver: ContentRes
height = options.outHeight, height = options.outHeight,
width = options.outWidth, width = options.outWidth,
size = output.size.toLong(), size = output.size.toLong(),
mimeType = options.outMimeType,
fileName = androidUri.lastPathSegment ?: "file", fileName = androidUri.lastPathSegment ?: "file",
content = output content = output
) )

View File

@ -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
}
}
}

View File

@ -38,21 +38,15 @@ internal class SendMessageUseCase(
} }
is MessageService.Message.ImageMessage -> { is MessageService.Message.ImageMessage -> {
println("Sending message")
val imageContent = imageContentReader.read(message.content.uri) val imageContent = imageContentReader.read(message.content.uri)
val uri = httpClient.execute(uploadRequest(imageContent.content, imageContent.fileName, imageContent.mimeType)).contentUri
println("content: ${imageContent.size}")
val uri = httpClient.execute(uploadRequest(imageContent.content, imageContent.fileName, "image/png")).contentUri
println("Got uri $uri")
val request = sendRequest( val request = sendRequest(
roomId = message.roomId, roomId = message.roomId,
eventType = EventType.ROOM_MESSAGE, eventType = EventType.ROOM_MESSAGE,
txId = message.localId, txId = message.localId,
content = MessageService.Message.Content.ImageContent( content = MessageService.Message.Content.ImageContent(
url = uri, url = uri,
filename = "foobar.png", filename = imageContent.fileName,
MessageService.Message.Content.ImageContent.Info( MessageService.Message.Content.ImageContent.Info(
height = imageContent.height, height = imageContent.height,
width = imageContent.width, 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
}
}
}

View File

@ -19,10 +19,7 @@ internal object ApiTimelineMessageContentDeserializer : KSerializer<ApiTimelineE
return when (element.jsonObject["msgtype"]?.jsonPrimitive?.content) { return when (element.jsonObject["msgtype"]?.jsonPrimitive?.content) {
"m.text" -> ApiTimelineEvent.TimelineMessage.Content.Text.serializer().deserialize(decoder) "m.text" -> ApiTimelineEvent.TimelineMessage.Content.Text.serializer().deserialize(decoder)
"m.image" -> ApiTimelineEvent.TimelineMessage.Content.Image.serializer().deserialize(decoder) "m.image" -> ApiTimelineEvent.TimelineMessage.Content.Image.serializer().deserialize(decoder)
else -> { else -> ApiTimelineEvent.TimelineMessage.Content.Ignored
println(element)
ApiTimelineEvent.TimelineMessage.Content.Ignored
}
} }
} }

View File

@ -44,7 +44,6 @@ internal class RoomEventFactory(
private fun ApiTimelineEvent.TimelineMessage.readImageMeta(userCredentials: UserCredentials): RoomEvent.Image.ImageMeta { private fun ApiTimelineEvent.TimelineMessage.readImageMeta(userCredentials: UserCredentials): RoomEvent.Image.ImageMeta {
val content = this.content as ApiTimelineEvent.TimelineMessage.Content.Image val content = this.content as ApiTimelineEvent.TimelineMessage.Content.Image
println(content)
return RoomEvent.Image.ImageMeta( return RoomEvent.Image.ImageMeta(
content.info?.width, content.info?.width,
content.info?.height, content.info?.height,

View File

@ -338,6 +338,7 @@ class JavaImageContentReader : ImageContentReader {
height = image.height, height = image.height,
width = image.width, width = image.width,
size = size, size = size,
mimeType = "image/${file.extension}",
fileName = file.name, fileName = file.name,
content = file.readBytes() content = file.readBytes()
) )