Handle file downloads

This commit is contained in:
charles 2022-04-28 10:41:35 +10:00
parent 4f0cedb53b
commit a1f93e096e
5 changed files with 15 additions and 19 deletions

View File

@ -6,7 +6,6 @@ import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.MediaType
import okhttp3.RequestBody
import org.session.libsession.messaging.open_groups.OpenGroupApi
import org.session.libsession.snode.OnionRequestAPI
import org.session.libsignal.utilities.Base64
import org.session.libsignal.utilities.HTTP
@ -52,8 +51,8 @@ object FileServerApi {
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
}
private fun send(request: Request): Promise<Map<*, *>, Exception> {
val url = HttpUrl.parse(server) ?: return Promise.ofFail(OpenGroupApi.Error.InvalidURL)
private fun send(request: Request): Promise<ByteArray, Exception> {
val url = HttpUrl.parse(server) ?: return Promise.ofFail(Error.InvalidURL)
val urlBuilder = HttpUrl.Builder()
.scheme(url.scheme())
.host(url.host())
@ -75,7 +74,7 @@ object FileServerApi {
}
return if (request.useOnionRouting) {
OnionRequestAPI.sendOnionRequest(requestBuilder.build(), server, serverPublicKey).map {
JsonUtil.fromJson(it.body, Map::class.java)
it.body ?: throw Error.ParsingFailed
}.fail { e ->
Log.e("Loki", "File server request failed.", e)
}
@ -96,16 +95,14 @@ object FileServerApi {
"Content-Type" to "application/octet-stream"
)
)
return send(request).map { json ->
json["result"] as? Long ?: throw OpenGroupApi.Error.ParsingFailed
return send(request).map { response ->
val json = JsonUtil.fromJson(response, Map::class.java)
json["result"] as? Long ?: throw Error.ParsingFailed
}
}
fun download(file: Long): Promise<ByteArray, Exception> {
fun download(file: String): Promise<ByteArray, Exception> {
val request = Request(verb = HTTP.Verb.GET, endpoint = "file/$file")
return send(request).map { json ->
val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed
Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed
}
return send(request)
}
}

View File

@ -101,7 +101,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
} else {
val url = HttpUrl.parse(attachment.url)!!
val fileID = url.pathSegments().last()
OpenGroupApi.download(fileID.toLong(), openGroup.room, openGroup.server).get().let {
OpenGroupApi.download(fileID, openGroup.room, openGroup.server).get().let {
tempFile.writeBytes(it)
}
}

View File

@ -51,7 +51,7 @@ sealed class Endpoint(val value: String) {
data class RoomFile(val roomToken: String) : Endpoint("room/$roomToken/file")
data class RoomFileIndividual(
val roomToken: String,
val fileId: Long
val fileId: String
) : Endpoint("room/$roomToken/file/$fileId")
// Inbox/Outbox (Message Requests)

View File

@ -310,20 +310,19 @@ object OpenGroupApi {
verb = GET,
room = roomID,
server = server,
endpoint = Endpoint.RoomFileIndividual(roomID, imageId)
endpoint = Endpoint.RoomFileIndividual(roomID, imageId.toString())
)
return send(request).map { it.body ?: throw Error.ParsingFailed }
}
// region Upload/Download
fun upload(file: ByteArray, room: String, server: String): Promise<Long, Exception> {
val base64EncodedFile = encodeBytes(file)
val parameters = mapOf("file" to base64EncodedFile)
val parameters = mapOf("file" to file)
val request = Request(
verb = POST,
room = room,
server = server,
endpoint = Endpoint.File,
endpoint = Endpoint.RoomFile(room),
parameters = parameters
)
return getResponseBodyJson(request).map { json ->
@ -331,7 +330,7 @@ object OpenGroupApi {
}
}
fun download(fileId: Long, room: String, server: String): Promise<ByteArray, Exception> {
fun download(fileId: String, room: String, server: String): Promise<ByteArray, Exception> {
val request = Request(
verb = GET,
room = room,

View File

@ -36,7 +36,7 @@ object DownloadUtilities {
val url = HttpUrl.parse(urlAsString)!!
val fileID = url.pathSegments().last()
try {
FileServerApi.download(fileID.toLong()).get().let {
FileServerApi.download(fileID).get().let {
outputStream.write(it)
}
} catch (e: Exception) {