Fixed fetchBytesFromUrl() for JS, but still fails due to CORS

This commit is contained in:
dankito 2024-09-01 23:46:13 +02:00
parent e9c0f2a77d
commit e0060d9380
4 changed files with 13 additions and 4 deletions

View File

@ -8,5 +8,5 @@ import java.net.URL
actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap =
BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size).asImageBitmap() BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size).asImageBitmap()
actual fun fetchBytesFromUrl(url: String): ByteArray = actual suspend fun fetchBytesFromUrl(url: String): ByteArray =
URL(url).openStream().buffered().use { it.readBytes() } URL(url).openStream().buffered().use { it.readBytes() }

View File

@ -4,4 +4,4 @@ import androidx.compose.ui.graphics.ImageBitmap
expect fun createImageBitmap(imageBytes: ByteArray): ImageBitmap expect fun createImageBitmap(imageBytes: ByteArray): ImageBitmap
expect fun fetchBytesFromUrl(url: String): ByteArray expect suspend fun fetchBytesFromUrl(url: String): ByteArray

View File

@ -8,5 +8,5 @@ import java.net.URL
actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap =
Image.makeFromEncoded(imageBytes).toComposeImageBitmap() Image.makeFromEncoded(imageBytes).toComposeImageBitmap()
actual fun fetchBytesFromUrl(url: String): ByteArray = actual suspend fun fetchBytesFromUrl(url: String): ByteArray =
URL(url).openStream().buffered().use { it.readAllBytes() } URL(url).openStream().buffered().use { it.readAllBytes() }

View File

@ -2,9 +2,18 @@ package net.codinux.banking.ui.service
import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toComposeImageBitmap import androidx.compose.ui.graphics.toComposeImageBitmap
import kotlinx.browser.window
import kotlinx.coroutines.await
import org.jetbrains.skia.Image import org.jetbrains.skia.Image
import org.khronos.webgl.Int8Array
actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap =
Image.makeFromEncoded(imageBytes).toComposeImageBitmap() Image.makeFromEncoded(imageBytes).toComposeImageBitmap()
actual fun fetchBytesFromUrl(url: String): ByteArray = ByteArray(0) actual suspend fun fetchBytesFromUrl(url: String): ByteArray {
val response = window.fetch(url).await()
val arrayBuffer = response.arrayBuffer().await()
val byteArray = Int8Array(arrayBuffer)
return byteArray.unsafeCast<ByteArray>()
}