From 37e75411729bd0b8a0315716369fc02a8b8ae725 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Tue, 22 Oct 2024 16:54:33 +0200 Subject: [PATCH 1/4] refactor: Rename UUID by linkUUID if needed --- .../managers/TransferManager.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt index 46687709..ed6f65ab 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/TransferManager.kt @@ -60,7 +60,7 @@ class TransferManager internal constructor( * Retrieves a flow of transfers based on the specified transfer direction. * * @see addTransferByUrl - * @see addTransferByUUID + * @see addTransferByLinkUUID * * @param transferDirection The direction of the transfers to retrieve (e.g., [TransferDirection.SENT] or [TransferDirection.RECEIVED]). * @return A `Flow` that emits a list of transfers matching the specified direction. @@ -81,7 +81,7 @@ class TransferManager internal constructor( * * @see getTransfers * - * @param transferUUID The UUID corresponding to the uploaded transfer link. + * @param linkUUID The UUID corresponding to the uploaded transfer link. * @throws CancellationException If the operation is cancelled. * @throws ApiException If there is an error related to the API during transfer retrieval. * @throws UnexpectedApiErrorFormatException Unparsable api error response. @@ -96,8 +96,8 @@ class TransferManager internal constructor( UnknownException::class, RealmException::class, ) - suspend fun addTransferByUUID(transferUUID: String) = withContext(Dispatchers.IO) { - addTransfer(transferRepository.getTransferByLinkUUID(transferUUID).data, TransferDirection.SENT) + suspend fun addTransferByLinkUUID(linkUUID: String) = withContext(Dispatchers.IO) { + addTransfer(transferRepository.getTransferByLinkUUID(linkUUID).data, TransferDirection.SENT) } /** From 65d5b825a147378f6846d72cdbb01dd200a657a7 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Tue, 22 Oct 2024 17:03:52 +0200 Subject: [PATCH 2/4] feat: Add the transfer to the database if upload finish with success --- .../SwissTransferInjection.kt | 2 +- .../managers/UploadManager.kt | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt index 13333088..9fe80ff2 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/SwissTransferInjection.kt @@ -65,7 +65,7 @@ class SwissTransferInjection { val accountManager by lazy { AccountManager(appSettingsController, uploadController, transferController, realmProvider) } /** A manager used to orchestrate Uploads operations. */ - val uploadManager by lazy { UploadManager(uploadController, uploadRepository) } + val uploadManager by lazy { UploadManager(uploadController, uploadRepository, transferManager) } /** An utils to help use shared routes */ val sharedApiUrlCreator by lazy { SharedApiUrlCreator(transferController, uploadController) } diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt index f636632b..9bf75f1c 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt @@ -44,10 +44,12 @@ import kotlin.coroutines.cancellation.CancellationException * * @property uploadController The controller for managing upload data in the database. * @property uploadRepository The repository for interacting with the SwissTransfer API for uploads. + * @property transferManager Transfer operations */ class UploadManager( private val uploadController: UploadController, private val uploadRepository: UploadRepository, + private val transferManager: TransferManager, ) { /** @@ -194,7 +196,7 @@ class UploadManager( } /** - * Finishes an upload session. + * Finishes an upload session and add the transfer to the database . * * This method retrieves an upload session from the database using the provided `uuid`. * If the session is found and has a remote container UUID, it creates a `FinishUploadBody` object @@ -233,7 +235,12 @@ class UploadManager( language = uploadSession.language.code, recipientsEmails = uploadSession.recipientsEmails, ) - uploadRepository.finishUpload(finishUploadBody) - uploadController.removeUploadSession(containerUUID) + val finishUploadResponse = runCatching { + uploadRepository.finishUpload(finishUploadBody).first() + }.getOrElse { throw UnknownException(it) } + uploadController.removeUploadSession(uploadSession.uuid) + + transferManager.addTransferByLinkUUID(finishUploadResponse.linkUUID) + // TODO: If we can't retrieve the transfer cause of the Internet, we should put it in Realm and try again later. } } From dd9d79697040ebef56744c6dc2f175c438be6126 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 23 Oct 2024 09:04:48 +0200 Subject: [PATCH 3/4] refactor: Update UploadManager kDocs --- .../managers/UploadManager.kt | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt index 9bf75f1c..d8cc6279 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt @@ -88,8 +88,6 @@ class UploadManager( /** * Creates a new upload session in the database. * - * This method inserts a new upload session into the database using the provided `newUploadSession` data. - * * @param newUploadSession The data for the new upload session. * @throws RealmException If an error occurs during database access. * @throws CancellationException If the operation is cancelled. @@ -100,12 +98,7 @@ class UploadManager( } /** - * Initializes an upload session. - * - * This method retrieves an upload session from the database using the provided `uuid`. - * If the session is found, it creates an `InitUploadBody` object with the session data and the `recaptcha` token. - * It then calls the `initUpload()` method of the `uploadRepository` to initiate the upload session on the server. - * Finally, it updates the upload session in the database with the response received from the server. + * Initializes an upload session and update it in database with the remote data. * * @param uuid The UUID of the upload session. * @param recaptcha The reCAPTCHA token or an empty string in any. @@ -145,10 +138,6 @@ class UploadManager( /** * Uploads a chunk of data for a file in an upload session. * - * This method retrieves an upload session from the database using the provided `uuid`. - * If the session is found and has a remote upload host and remote container, it calls the `uploadChunk()` method of the - * `uploadRepository` to send the chunk data to the server. - * * @param uuid The UUID of the upload session. * @param fileUUID The UUID of the file being uploaded. * @param chunkIndex The index of the chunk being uploaded. @@ -198,12 +187,6 @@ class UploadManager( /** * Finishes an upload session and add the transfer to the database . * - * This method retrieves an upload session from the database using the provided `uuid`. - * If the session is found and has a remote container UUID, it creates a `FinishUploadBody` object - * with the necessary data and calls the `finishUpload()` method of the `uploadRepository` to - * finalize the upload session on the server. - * Finally, it removes the upload session from the database. - * * @param uuid The UUID of the upload session. * @throws CancellationException If the operation is cancelled. * @throws ApiException If there is a general API error. From e2d4cac2d4943729fa4006c1803e26e53999c9c7 Mon Sep 17 00:00:00 2001 From: Abdourahamane Boinaidi Date: Wed, 23 Oct 2024 09:25:24 +0200 Subject: [PATCH 4/4] refactor: A little refactor --- .../multiplatform_swisstransfer/managers/UploadManager.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt index d8cc6279..a3e4a9a2 100644 --- a/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt +++ b/STCore/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/managers/UploadManager.kt @@ -22,8 +22,8 @@ import com.infomaniak.multiplatform_swisstransfer.common.exceptions.UnknownExcep import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController -import com.infomaniak.multiplatform_swisstransfer.exceptions.NullPropertyException import com.infomaniak.multiplatform_swisstransfer.exceptions.NotFoundException +import com.infomaniak.multiplatform_swisstransfer.exceptions.NullPropertyException import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ApiException import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ContainerErrorsException import com.infomaniak.multiplatform_swisstransfer.network.exceptions.NetworkException @@ -171,8 +171,10 @@ class UploadManager( ): Unit = withContext(Dispatchers.IO) { val uploadSession = uploadController.getUploadByUUID(uuid) ?: throw NotFoundException("${UploadSession::class.simpleName} not found in DB with uuid = $uuid") - val remoteUploadHost = uploadSession.remoteUploadHost ?: throw NullPropertyException("Remote upload host cannot be null") - val remoteContainer = uploadSession.remoteContainer ?: throw NullPropertyException("Remote container cannot be null") + val remoteUploadHost = uploadSession.remoteUploadHost + ?: throw NullPropertyException("Remote upload host cannot be null") + val remoteContainer = uploadSession.remoteContainer + ?: throw NullPropertyException("Remote container cannot be null") uploadRepository.uploadChunk( uploadHost = remoteUploadHost,