Skip to content

Commit

Permalink
getUploadByRemotePath in background thread
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 16, 2024
1 parent 8320c12 commit 7167916
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.Semaphore
import javax.inject.Inject

Expand All @@ -57,6 +59,8 @@ class FileUploadHelper {
@Inject
lateinit var fileStorageManager: FileDataStorageManager

private val ioScope = CoroutineScope(Dispatchers.IO)

init {
MainApp.getAppComponent().inject(this)
}
Expand Down Expand Up @@ -231,11 +235,13 @@ class FileUploadHelper {
}

fun cancelFileUpload(remotePath: String, accountName: String) {
val upload = uploadsStorageManager.getUploadByRemotePath(remotePath)
if (upload != null) {
cancelFileUploads(listOf(upload), accountName)
} else {
Log_OC.e(TAG, "Error cancelling current upload because upload does not exist!")
ioScope.launch {
val upload = uploadsStorageManager.getUploadByRemotePath(remotePath)
if (upload != null) {
cancelFileUploads(listOf(upload), accountName)
} else {
Log_OC.e(TAG, "Error cancelling current upload because upload does not exist!")
}
}
}

Expand Down Expand Up @@ -266,8 +272,21 @@ class FileUploadHelper {
return false
}

val upload: OCUpload = uploadsStorageManager.getUploadByRemotePath(file.remotePath) ?: return false
return upload.uploadStatus == UploadStatus.UPLOAD_IN_PROGRESS
val uploadCompletableFuture = CompletableFuture.supplyAsync {
uploadsStorageManager.getUploadByRemotePath(file.remotePath)
}
return try {
val upload = uploadCompletableFuture.get()
if (upload != null) {
upload.uploadStatus == UploadStatus.UPLOAD_IN_PROGRESS
} else {
false
}
} catch (e: ExecutionException) {
false
} catch (e: InterruptedException) {
false
}
}

private fun checkConnectivity(connectivityService: ConnectivityService): Boolean {
Expand Down

0 comments on commit 7167916

Please sign in to comment.