Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: Share some api routes #17

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
tevincent marked this conversation as resolved.
Show resolved Hide resolved

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()}"
}
}
Loading