From a1f93e096eea04f87ec710ba90abdf80e8efa547 Mon Sep 17 00:00:00 2001 From: charles Date: Thu, 28 Apr 2022 10:41:35 +1000 Subject: [PATCH] Handle file downloads --- .../messaging/file_server/FileServerApi.kt | 19 ++++++++----------- .../messaging/jobs/AttachmentDownloadJob.kt | 2 +- .../messaging/open_groups/Endpoint.kt | 2 +- .../messaging/open_groups/OpenGroupApi.kt | 9 ++++----- .../libsession/utilities/DownloadUtilities.kt | 2 +- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerApi.kt b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerApi.kt index 7ade6f5436..e50bf62c61 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerApi.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerApi.kt @@ -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, Exception> { - val url = HttpUrl.parse(server) ?: return Promise.ofFail(OpenGroupApi.Error.InvalidURL) + private fun send(request: Request): Promise { + 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 { + fun download(file: String): Promise { 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) } } \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt index c43672f7a3..8b19737b0c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt @@ -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) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/open_groups/Endpoint.kt b/libsession/src/main/java/org/session/libsession/messaging/open_groups/Endpoint.kt index 6412749141..79abfeb59a 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/open_groups/Endpoint.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/open_groups/Endpoint.kt @@ -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) diff --git a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupApi.kt b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupApi.kt index 841a872016..f34f631d1c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupApi.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupApi.kt @@ -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 { - 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 { + fun download(fileId: String, room: String, server: String): Promise { val request = Request( verb = GET, room = room, diff --git a/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt b/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt index 68fb53d381..0a61d1ede0 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/DownloadUtilities.kt @@ -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) {