Skip to content

Commit

Permalink
Merge pull request #59 from Infomaniak/feat-retrieve-transfer-by-link…
Browse files Browse the repository at this point in the history
…UUID

feat: Add transfer to DB after finish session & Retrieve transfer by link UUID
  • Loading branch information
tevincent authored Oct 23, 2024
2 parents fa14c67 + e2d4cac commit aa94caa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
) {

/**
Expand Down Expand Up @@ -86,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.
Expand All @@ -98,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.
Expand Down Expand Up @@ -143,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.
Expand Down Expand Up @@ -180,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,
Expand All @@ -194,13 +187,7 @@ class UploadManager(
}

/**
* Finishes 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 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.
* Finishes an upload session and add the transfer to the database .
*
* @param uuid The UUID of the upload session.
* @throws CancellationException If the operation is cancelled.
Expand Down Expand Up @@ -233,7 +220,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.
}
}

0 comments on commit aa94caa

Please sign in to comment.