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

feat: Add transfer to DB after finish session & Retrieve transfer by link UUID #59

Merged
merged 4 commits into from
Oct 23, 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 @@ -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 {
sirambd marked this conversation as resolved.
Show resolved Hide resolved
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.
}
}
Loading