mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-12 09:33:39 +00:00
Handle file downloads
This commit is contained in:
parent
4f0cedb53b
commit
a1f93e096e
@ -6,7 +6,6 @@ import okhttp3.Headers
|
|||||||
import okhttp3.HttpUrl
|
import okhttp3.HttpUrl
|
||||||
import okhttp3.MediaType
|
import okhttp3.MediaType
|
||||||
import okhttp3.RequestBody
|
import okhttp3.RequestBody
|
||||||
import org.session.libsession.messaging.open_groups.OpenGroupApi
|
|
||||||
import org.session.libsession.snode.OnionRequestAPI
|
import org.session.libsession.snode.OnionRequestAPI
|
||||||
import org.session.libsignal.utilities.Base64
|
import org.session.libsignal.utilities.Base64
|
||||||
import org.session.libsignal.utilities.HTTP
|
import org.session.libsignal.utilities.HTTP
|
||||||
@ -52,8 +51,8 @@ object FileServerApi {
|
|||||||
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
|
return RequestBody.create(MediaType.get("application/json"), parametersAsJSON)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun send(request: Request): Promise<Map<*, *>, Exception> {
|
private fun send(request: Request): Promise<ByteArray, Exception> {
|
||||||
val url = HttpUrl.parse(server) ?: return Promise.ofFail(OpenGroupApi.Error.InvalidURL)
|
val url = HttpUrl.parse(server) ?: return Promise.ofFail(Error.InvalidURL)
|
||||||
val urlBuilder = HttpUrl.Builder()
|
val urlBuilder = HttpUrl.Builder()
|
||||||
.scheme(url.scheme())
|
.scheme(url.scheme())
|
||||||
.host(url.host())
|
.host(url.host())
|
||||||
@ -75,7 +74,7 @@ object FileServerApi {
|
|||||||
}
|
}
|
||||||
return if (request.useOnionRouting) {
|
return if (request.useOnionRouting) {
|
||||||
OnionRequestAPI.sendOnionRequest(requestBuilder.build(), server, serverPublicKey).map {
|
OnionRequestAPI.sendOnionRequest(requestBuilder.build(), server, serverPublicKey).map {
|
||||||
JsonUtil.fromJson(it.body, Map::class.java)
|
it.body ?: throw Error.ParsingFailed
|
||||||
}.fail { e ->
|
}.fail { e ->
|
||||||
Log.e("Loki", "File server request failed.", e)
|
Log.e("Loki", "File server request failed.", e)
|
||||||
}
|
}
|
||||||
@ -96,16 +95,14 @@ object FileServerApi {
|
|||||||
"Content-Type" to "application/octet-stream"
|
"Content-Type" to "application/octet-stream"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return send(request).map { json ->
|
return send(request).map { response ->
|
||||||
json["result"] as? Long ?: throw OpenGroupApi.Error.ParsingFailed
|
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")
|
val request = Request(verb = HTTP.Verb.GET, endpoint = "file/$file")
|
||||||
return send(request).map { json ->
|
return send(request)
|
||||||
val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed
|
|
||||||
Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -101,7 +101,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
|
|||||||
} else {
|
} else {
|
||||||
val url = HttpUrl.parse(attachment.url)!!
|
val url = HttpUrl.parse(attachment.url)!!
|
||||||
val fileID = url.pathSegments().last()
|
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)
|
tempFile.writeBytes(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ sealed class Endpoint(val value: String) {
|
|||||||
data class RoomFile(val roomToken: String) : Endpoint("room/$roomToken/file")
|
data class RoomFile(val roomToken: String) : Endpoint("room/$roomToken/file")
|
||||||
data class RoomFileIndividual(
|
data class RoomFileIndividual(
|
||||||
val roomToken: String,
|
val roomToken: String,
|
||||||
val fileId: Long
|
val fileId: String
|
||||||
) : Endpoint("room/$roomToken/file/$fileId")
|
) : Endpoint("room/$roomToken/file/$fileId")
|
||||||
|
|
||||||
// Inbox/Outbox (Message Requests)
|
// Inbox/Outbox (Message Requests)
|
||||||
|
@ -310,20 +310,19 @@ object OpenGroupApi {
|
|||||||
verb = GET,
|
verb = GET,
|
||||||
room = roomID,
|
room = roomID,
|
||||||
server = server,
|
server = server,
|
||||||
endpoint = Endpoint.RoomFileIndividual(roomID, imageId)
|
endpoint = Endpoint.RoomFileIndividual(roomID, imageId.toString())
|
||||||
)
|
)
|
||||||
return send(request).map { it.body ?: throw Error.ParsingFailed }
|
return send(request).map { it.body ?: throw Error.ParsingFailed }
|
||||||
}
|
}
|
||||||
|
|
||||||
// region Upload/Download
|
// region Upload/Download
|
||||||
fun upload(file: ByteArray, room: String, server: String): Promise<Long, Exception> {
|
fun upload(file: ByteArray, room: String, server: String): Promise<Long, Exception> {
|
||||||
val base64EncodedFile = encodeBytes(file)
|
val parameters = mapOf("file" to file)
|
||||||
val parameters = mapOf("file" to base64EncodedFile)
|
|
||||||
val request = Request(
|
val request = Request(
|
||||||
verb = POST,
|
verb = POST,
|
||||||
room = room,
|
room = room,
|
||||||
server = server,
|
server = server,
|
||||||
endpoint = Endpoint.File,
|
endpoint = Endpoint.RoomFile(room),
|
||||||
parameters = parameters
|
parameters = parameters
|
||||||
)
|
)
|
||||||
return getResponseBodyJson(request).map { json ->
|
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(
|
val request = Request(
|
||||||
verb = GET,
|
verb = GET,
|
||||||
room = room,
|
room = room,
|
||||||
|
@ -36,7 +36,7 @@ object DownloadUtilities {
|
|||||||
val url = HttpUrl.parse(urlAsString)!!
|
val url = HttpUrl.parse(urlAsString)!!
|
||||||
val fileID = url.pathSegments().last()
|
val fileID = url.pathSegments().last()
|
||||||
try {
|
try {
|
||||||
FileServerApi.download(fileID.toLong()).get().let {
|
FileServerApi.download(fileID).get().let {
|
||||||
outputStream.write(it)
|
outputStream.write(it)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user