Skip to content
Snippets Groups Projects
Commit b28f0ab1 authored by Koen's avatar Koen
Browse files

Fixed Glide image loading.

parent 700b764c
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okio.IOException
import userpackage.Protocol
import java.nio.ByteBuffer
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
......@@ -132,7 +133,7 @@ class ApiMethods {
}
}
suspend fun getDataFromServerAndReassemble(dataLink: Protocol.URLInfoDataLink): ByteArray = withContext(Dispatchers.IO) {
suspend fun getDataFromServerAndReassemble(dataLink: Protocol.URLInfoDataLink): ByteBuffer = withContext(Dispatchers.IO) {
val server = dataLink.serversList.first() //TODO: Handle multiple servers better
val blobProcessRangesToGet = Protocol.RangesForProcess.newBuilder()
.addAllRanges(dataLink.sectionsList)
......@@ -140,8 +141,9 @@ class ApiMethods {
.build()
val blobEvents = getEvents(server, dataLink.system, Protocol.RangesForSystem.newBuilder()
.addRangesForProcesses(blobProcessRangesToGet).build()).eventsList.map { SignedEvent.fromProto(it) }
blobEvents.reassembleSections(dataLink.byteCount.toInt(), dataLink.sectionsList)
val reassembledData = blobEvents.reassembleSections(dataLink.byteCount.toInt(), dataLink.sectionsList)
?: throw Exception("Failed to reassemble sections due to missing data")
return@withContext reassembledData
}
suspend fun <T> executeCall(call: Call, handler: (Response) -> T): T = suspendCancellableCoroutine { continuation ->
......
......@@ -7,6 +7,7 @@ import kotlinx.serialization.Serializable
import userpackage.Protocol
import userpackage.Protocol.Claim
import userpackage.Protocol.URLInfoSystemLink
import java.nio.ByteBuffer
fun ByteArray.toHexString(): String = joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }
......@@ -146,8 +147,8 @@ fun Protocol.ImageManifest.toURLInfoSystemLinkUrl(system: Protocol.PublicKey, pr
return "polycentric://" + urlInfo.toByteArray().toBase64Url()
}
fun List<SignedEvent>.reassembleSections(totalSize: Int, sections: Iterable<Protocol.Range>): ByteArray? {
val totalArray = ByteArray(totalSize)
fun List<SignedEvent>.reassembleSections(totalSize: Int, sections: Iterable<Protocol.Range>): ByteBuffer? {
val totalArray = ByteBuffer.allocate(totalSize)
var offset = 0
for (section in sections) {
......@@ -160,7 +161,7 @@ fun List<SignedEvent>.reassembleSections(totalSize: Int, sections: Iterable<Prot
sectionEvents.forEach {
val blobSection = it.event.content
val size = blobSection.size
blobSection.copyInto(totalArray, offset)
blobSection.copyInto(totalArray.array(), offset)
offset += size
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment