Skip to content

Commit

Permalink
refactor: Update launchTask
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd committed Feb 25, 2025
1 parent 39c45e2 commit 7c93c9a
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions app/src/main/java/com/infomaniak/drive/data/api/UploadTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,35 +114,24 @@ class UploadTask(
}

private suspend fun launchTask() = coroutineScope {
val chunkConfig = try {
fileChunkSizeManager.computeChunkConfig(
fileSize = uploadFile.fileSize,
defaultFileChunkSize = uploadFile.getValidChunks()?.validChuckSize?.toLong(),
)
} catch (exception: IllegalArgumentException) {
uploadFile.resetUploadTokenAndCancelSession()
fileChunkSizeManager.computeChunkConfig(fileSize = uploadFile.fileSize)
}

val chunkConfig = getChunkConfig()
val totalChunks = chunkConfig.totalChunks
val uploadedChunks = uploadFile.getValidChunks()
val isNewUploadSession = uploadedChunks?.needToResetUpload(chunkConfig.fileChunkSize) ?: true

context.contentResolver.openInputStream(uploadFile.getOriginalUri(context))?.buffered()?.use { fileInputStream ->

val uploadedChunks = uploadFile.getValidChunks()
val isNewUploadSession = uploadedChunks?.needToResetUpload(chunkConfig.fileChunkSize) ?: true

val uploadHost = if (isNewUploadSession) {
uploadFile.prepareUploadSession(totalChunks)
} else {
uploadFile.uploadHost
}!!
val uploadHost = if (isNewUploadSession) {
uploadFile.prepareUploadSession(totalChunks)
} else {
uploadFile.uploadHost
}!!

Sentry.addBreadcrumb(Breadcrumb().apply {
category = UploadWorker.BREADCRUMB_TAG
message = "start ${uploadFile.fileName} with $totalChunks chunks and $uploadedChunks uploadedChunks"
level = SentryLevel.INFO
})

Sentry.addBreadcrumb(Breadcrumb().apply {
category = UploadWorker.BREADCRUMB_TAG
message = "start ${uploadFile.fileName} with $totalChunks chunks and $uploadedChunks uploadedChunks"
level = SentryLevel.INFO
})
context.contentResolver.openInputStream(uploadFile.getOriginalUri(context))?.buffered()?.use { fileInputStream ->

SentryLog.d("kDrive", " upload task started with total chunk: $totalChunks, valid: $uploadedChunks")

Expand All @@ -160,6 +149,18 @@ class UploadTask(
if (isActive) onFinish(uploadFile.getUriObject())
}

private fun getChunkConfig(): FileChunkSizeManager.ChunkConfig {
return try {
fileChunkSizeManager.computeChunkConfig(
fileSize = uploadFile.fileSize,
defaultFileChunkSize = uploadFile.getValidChunks()?.validChuckSize?.toLong(),
)
} catch (exception: IllegalArgumentException) {
uploadFile.resetUploadTokenAndCancelSession()
fileChunkSizeManager.computeChunkConfig(fileSize = uploadFile.fileSize)
}
}

private suspend fun uploadChunks(
chunkConfig: FileChunkSizeManager.ChunkConfig,
validChunksIds: List<Int>?,
Expand Down

0 comments on commit 7c93c9a

Please sign in to comment.