diff --git a/owncloudApp/src/main/java/com/owncloud/android/usecases/transfers/uploads/UploadFileFromContentUriUseCase.kt b/owncloudApp/src/main/java/com/owncloud/android/usecases/transfers/uploads/UploadFileFromContentUriUseCase.kt index d01b4097c50..0f12f3f7096 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/usecases/transfers/uploads/UploadFileFromContentUriUseCase.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/usecases/transfers/uploads/UploadFileFromContentUriUseCase.kt @@ -29,6 +29,7 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager import androidx.work.workDataOf import com.owncloud.android.domain.BaseUseCase +import com.owncloud.android.domain.camerauploads.model.UploadBehavior import com.owncloud.android.workers.RemoveLocalFileWorker import com.owncloud.android.workers.UploadFileFromContentUriWorker import timber.log.Timber @@ -47,7 +48,6 @@ class UploadFileFromContentUriUseCase( UploadFileFromContentUriWorker.KEY_PARAM_UPLOAD_ID to params.uploadIdInStorageManager ) val inputDataRemoveLocalFileWorker = workDataOf( - UploadFileFromContentUriWorker.KEY_PARAM_BEHAVIOR to params.behavior, UploadFileFromContentUriWorker.KEY_PARAM_CONTENT_URI to params.contentUri.toString(), ) @@ -64,13 +64,15 @@ class UploadFileFromContentUriUseCase( .addTag(params.uploadIdInStorageManager.toString()) .build() - val removeLocalFileWorker = OneTimeWorkRequestBuilder() - .setInputData(inputDataRemoveLocalFileWorker) - .build() - - workManager.beginWith(uploadFileFromContentUriWorker) - .then(removeLocalFileWorker) // File is already uploaded, so the original one can be removed if the behaviour is MOVE - .enqueue() + var workContinuation = workManager.beginWith(uploadFileFromContentUriWorker) + val behavior = UploadBehavior.fromString(params.behavior) + if (behavior == UploadBehavior.MOVE) { + val removeLocalFileWorker = OneTimeWorkRequestBuilder() + .setInputData(inputDataRemoveLocalFileWorker) + .build() + workContinuation = workContinuation.then(removeLocalFileWorker) // File is already uploaded, so the original one can be removed if the behaviour is MOVE + } + workContinuation.enqueue() Timber.i("Plain upload of ${params.contentUri.path} has been enqueued.") } diff --git a/owncloudApp/src/main/java/com/owncloud/android/workers/RemoveLocalFileWorker.kt b/owncloudApp/src/main/java/com/owncloud/android/workers/RemoveLocalFileWorker.kt index 4b3fc3d8efc..d365c144710 100644 --- a/owncloudApp/src/main/java/com/owncloud/android/workers/RemoveLocalFileWorker.kt +++ b/owncloudApp/src/main/java/com/owncloud/android/workers/RemoveLocalFileWorker.kt @@ -26,8 +26,6 @@ import androidx.core.net.toUri import androidx.documentfile.provider.DocumentFile import androidx.work.CoroutineWorker import androidx.work.WorkerParameters -import com.owncloud.android.domain.camerauploads.model.UploadBehavior -import com.owncloud.android.workers.UploadFileFromContentUriWorker.Companion.KEY_PARAM_BEHAVIOR import com.owncloud.android.workers.UploadFileFromContentUriWorker.Companion.KEY_PARAM_CONTENT_URI import org.koin.core.component.KoinComponent import timber.log.Timber @@ -40,15 +38,13 @@ class RemoveLocalFileWorker( workerParameters ), KoinComponent { - private lateinit var behavior: UploadBehavior private lateinit var contentUri: Uri override suspend fun doWork(): Result { if (!areParametersValid()) return Result.failure() return try { - if (behavior == UploadBehavior.MOVE) { - removeLocalFile() - } + val documentFile = DocumentFile.fromSingleUri(appContext, contentUri) + documentFile?.delete() Result.success() } catch (throwable: Throwable) { Timber.e(throwable) @@ -57,17 +53,10 @@ class RemoveLocalFileWorker( } private fun areParametersValid(): Boolean { - val paramBehavior = workerParameters.inputData.getString(KEY_PARAM_BEHAVIOR) val paramContentUri = workerParameters.inputData.getString(KEY_PARAM_CONTENT_URI) contentUri = paramContentUri?.toUri() ?: return false - behavior = paramBehavior?.let { UploadBehavior.fromString(it) } ?: return false return true } - - private fun removeLocalFile() { - val documentFile = DocumentFile.fromSingleUri(appContext, contentUri) - documentFile?.delete() - } }