Do not encode images to JPEG before uploading. Fixes GIF upload #115

This commit is contained in:
Oleg Koretsky
2023-02-13 11:57:20 +02:00
parent 80bede648f
commit 108ad4dadc
2 changed files with 31 additions and 40 deletions
@@ -1,36 +1,40 @@
package com.vitorpamplona.amethyst.ui.actions package com.vitorpamplona.amethyst.ui.actions
import android.graphics.Bitmap import android.content.ContentResolver
import android.net.Uri
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.io.ByteArrayOutputStream import okhttp3.*
import java.io.IOException
import java.util.UUID
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody import okio.BufferedSink
import okhttp3.OkHttpClient import okio.source
import okhttp3.Request import java.io.IOException
import okhttp3.RequestBody import java.util.*
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
object ImageUploader { object ImageUploader {
private fun encodeImage(bitmap: Bitmap): ByteArray { fun uploadImage(
val byteArrayOutPutStream = ByteArrayOutputStream() uri: Uri,
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutPutStream) contentResolver: ContentResolver,
return byteArrayOutPutStream.toByteArray() onSuccess: (String) -> Unit,
} ) {
val contentType = contentResolver.getType(uri)
fun uploadImage(bitmap: Bitmap, onSuccess: (String) -> Unit) {
val client = OkHttpClient.Builder().build() val client = OkHttpClient.Builder().build()
val body: RequestBody = MultipartBody.Builder() val body: RequestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)
.addFormDataPart( .addFormDataPart(
"image", "image",
"${UUID.randomUUID()}.png", "${UUID.randomUUID()}",
encodeImage(bitmap).toRequestBody("image/png".toMediaType()) object : RequestBody() {
override fun contentType(): MediaType? =
contentType?.toMediaType()
override fun writeTo(sink: BufferedSink) {
contentResolver.openInputStream(uri)!!.use { inputStream ->
sink.writeAll(inputStream.source())
}
}
}
) )
.build() .build()
@@ -1,22 +1,14 @@
package com.vitorpamplona.amethyst.ui.actions package com.vitorpamplona.amethyst.ui.actions
import android.content.Context import android.content.Context
import android.graphics.ImageDecoder
import android.net.Uri import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.*
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.model.decodePublicKey
import com.vitorpamplona.amethyst.model.toHexKey
import com.vitorpamplona.amethyst.ui.components.isValidURL import com.vitorpamplona.amethyst.ui.components.isValidURL
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
import nostr.postr.toNpub import nostr.postr.toNpub
@@ -107,17 +99,12 @@ class NewPostViewModel: ViewModel() {
} }
fun upload(it: Uri, context: Context) { fun upload(it: Uri, context: Context) {
val img = if (Build.VERSION.SDK_INT < 28) { ImageUploader.uploadImage(
MediaStore.Images.Media.getBitmap(context.contentResolver, it) uri = it,
} else { contentResolver = context.contentResolver,
ImageDecoder.decodeBitmap(ImageDecoder.createSource(context.contentResolver, it)) ) {
} message = TextFieldValue(message.text + "\n\n" + it)
urlPreview = findUrlInMessage()
img?.let {
ImageUploader.uploadImage(img) {
message = TextFieldValue(message.text + "\n\n" + it)
urlPreview = findUrlInMessage()
}
} }
} }