Skip to content

Commit

Permalink
Merge pull request #17 from Infomaniak/shared-api-routes
Browse files Browse the repository at this point in the history
network: Share some api routes
  • Loading branch information
tevincent authored Aug 16, 2024
2 parents e2dee6d + 95fa53e commit f86eccf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ class UploadRepository internal constructor(private val uploadRequest: UploadReq
containerUUID: String,
fileUUID: String,
chunkIndex: Int,
lastChunk: Boolean,
isLastChunk: Boolean,
data: ByteArray,
): Boolean {
return uploadRequest.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, lastChunk, data)
return uploadRequest.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, isLastChunk, data)
}

@Throws(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.ResendEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.request.VerifyEmailCodeBody
import com.infomaniak.multiplatform_swisstransfer.network.utils.ApiRoutes
import com.infomaniak.multiplatform_swisstransfer.network.utils.SharedApiRoutes
import io.ktor.client.HttpClient
import io.ktor.client.request.post
import io.ktor.client.request.setBody
Expand Down Expand Up @@ -54,11 +55,11 @@ internal class UploadRequest(json: Json, httpClient: HttpClient) : BaseRequest(j
containerUUID: String,
fileUUID: String,
chunkIndex: Int,
lastChunk: Boolean,
isLastChunk: Boolean,
data: ByteArray,
): Boolean {
val httpResponse = httpClient.post(
urlString = ApiRoutes.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, lastChunk)
urlString = SharedApiRoutes.uploadChunk(uploadHost, containerUUID, fileUUID, chunkIndex, isLastChunk)
) {
setBody(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,12 @@ internal object ApiRoutes {
fun getTransfer(linkUUID: String): String {
return "links/$linkUUID"
}

fun downloadFiles(downloadHost: String, linkUUID: String): String {
return "https://$downloadHost/api/download/$linkUUID"
}

fun downloadFile(downloadHost: String, linkUUID: String, fileUUID: String?): String {
return "${downloadFiles(downloadHost, linkUUID)}/$fileUUID"
}
//endRegion

//region Upload
const val initUpload = "containers"
const val verifyEmailCode = "emails-validation"
const val resendEmailCode = "$verifyEmailCode/resend"
const val finishUpload = "uploadComplete"

fun uploadChunk(uploadHost: String, containerUUID: String, fileUUID: String, chunkIndex: Int, lastChunk: Boolean): String {
return "https://$uploadHost/api/uploadChunk/$containerUUID/$fileUUID/$chunkIndex/${lastChunk.int()}"
}
//endregion

//region Utils
private fun Boolean.int() = if (this) 1 else 0
//endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.infomaniak.multiplatform_swisstransfer.network.utils

fun Boolean.int() = if (this) 1 else 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.infomaniak.multiplatform_swisstransfer.network.utils

object SharedApiRoutes {

fun downloadFiles(downloadHost: String, linkUUID: String): String {
return "https://$downloadHost/api/download/$linkUUID"
}

fun downloadFile(downloadHost: String, linkUUID: String, fileUUID: String?): String {
return "${downloadFiles(downloadHost, linkUUID)}/$fileUUID"
}

fun uploadChunk(uploadHost: String, containerUUID: String, fileUUID: String, chunkIndex: Int, isLastChunk: Boolean): String {
return "https://$uploadHost/api/uploadChunk/$containerUUID/$fileUUID/$chunkIndex/${isLastChunk.int()}"
}
}

0 comments on commit f86eccf

Please sign in to comment.