Skip to content

Commit bd4ef78

Browse files
committed
fixed FileUploadHelper to make sure uploads are correctly reflecting progress
1 parent 46c312e commit bd4ef78

2 files changed

Lines changed: 31 additions & 28 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadHelper.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import com.nextcloud.client.database.entity.toUploadEntity
1818
import com.nextcloud.client.device.BatteryStatus
1919
import com.nextcloud.client.device.PowerManagementService
2020
import com.nextcloud.client.jobs.BackgroundJobManager
21-
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.currentUploadFileOperation
21+
import com.nextcloud.client.jobs.upload.FileUploadWorker.Companion.activeUploadFileOperations
2222
import com.nextcloud.client.network.Connectivity
2323
import com.nextcloud.client.network.ConnectivityService
2424
import com.nextcloud.utils.extensions.getUploadIds
@@ -360,17 +360,12 @@ class FileUploadHelper {
360360

361361
@Suppress("ReturnCount")
362362
fun isUploadingNow(upload: OCUpload?): Boolean {
363-
val currentUploadFileOperation = currentUploadFileOperation
364-
if (currentUploadFileOperation == null || currentUploadFileOperation.user == null) return false
365-
if (upload == null || upload.accountName != currentUploadFileOperation.user.accountName) return false
366-
367-
return if (currentUploadFileOperation.oldFile != null) {
368-
// For file conflicts check old file remote path
369-
upload.remotePath == currentUploadFileOperation.remotePath ||
370-
upload.remotePath == currentUploadFileOperation.oldFile!!
371-
.remotePath
372-
} else {
373-
upload.remotePath == currentUploadFileOperation.remotePath
363+
upload ?: return false
364+
365+
return activeUploadFileOperations.values.any { operation ->
366+
operation.user?.accountName == upload.accountName &&
367+
(upload.remotePath == operation.remotePath ||
368+
upload.remotePath == operation.oldFile?.remotePath)
374369
}
375370
}
376371

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import com.owncloud.android.lib.common.utils.Log_OC
4343
import com.owncloud.android.operations.UploadFileOperation
4444
import com.owncloud.android.ui.notifications.NotificationUtils
4545
import com.owncloud.android.utils.theme.ViewThemeUtils
46-
import kotlinx.coroutines.CoroutineScope
4746
import kotlinx.coroutines.Dispatchers
4847
import kotlinx.coroutines.cancel
4948
import kotlinx.coroutines.coroutineScope
@@ -84,8 +83,7 @@ class FileUploadWorker(
8483
const val TOTAL_UPLOAD_SIZE = "total_upload_size"
8584
const val SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION = "show_same_file_already_exists_notification"
8685

87-
private val activeUploadFileOperations = ConcurrentHashMap<String, UploadFileOperation>()
88-
86+
val activeUploadFileOperations = ConcurrentHashMap<String, UploadFileOperation>()
8987
private const val UPLOADS_ADDED_MESSAGE = "UPLOADS_ADDED"
9088
private const val UPLOAD_START_MESSAGE = "UPLOAD_START"
9189
private const val UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH"
@@ -123,7 +121,7 @@ class FileUploadWorker(
123121
fun isUploading(remotePath: String?, accountName: String?): Boolean {
124122
return activeUploadFileOperations.values.any {
125123
it.remotePath == remotePath && it.user.accountName == accountName
126-
}
124+
}
127125
}
128126

129127
fun getUploadAction(action: String): Int = when (action) {
@@ -263,10 +261,16 @@ class FileUploadWorker(
263261
val ocAccount = OwnCloudAccount(user.toPlatformAccount(), context)
264262
val client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context)
265263

266-
return@withContext parallelUpload(uploads, user, previouslyUploadedFileSize, totalUploadSize, client, accountName)
264+
return@withContext parallelUpload(
265+
uploads,
266+
user,
267+
previouslyUploadedFileSize,
268+
totalUploadSize,
269+
client,
270+
accountName
271+
)
267272
}
268273

269-
270274
private suspend fun parallelUpload(
271275
uploads: List<OCUpload>?,
272276
user: User,
@@ -279,9 +283,10 @@ class FileUploadWorker(
279283
return Result.success()
280284
}
281285

282-
val semaphore = Semaphore(5) // Limit to 5 parallel uploads
286+
val semaphore = Semaphore(10) // Limit to 10 parallel uploads
283287
val quotaExceeded = AtomicBoolean(false)
284288
val completedCount = AtomicInteger(0)
289+
val storageManager = FileDataStorageManager(user, context.contentResolver)
285290

286291
coroutineScope {
287292
for (upload in uploads) {
@@ -304,7 +309,7 @@ class FileUploadWorker(
304309
}
305310

306311
setWorkerState(user)
307-
val operation = createUploadFileOperation(upload, user)
312+
val operation = createUploadFileOperation(upload, user, storageManager)
308313
activeUploadFileOperations[operation.originalStoragePath] = operation
309314

310315
try {
@@ -320,10 +325,10 @@ class FileUploadWorker(
320325
)
321326
}
322327

323-
val result = upload(operation, user, client)
328+
val result = upload(operation, user, client)
324329

325-
val entity = uploadsStorageManager.uploadDao.getUploadById(upload.uploadId, accountName)
326-
uploadsStorageManager.updateStatus(entity, result.isSuccess)
330+
val entity = uploadsStorageManager.uploadDao.getUploadById(upload.uploadId, accountName)
331+
uploadsStorageManager.updateStatus(entity, result.isSuccess)
327332

328333
if (result.code == ResultCode.QUOTA_EXCEEDED) {
329334
Log_OC.w(TAG, "Quota exceeded, stopping uploads")
@@ -333,14 +338,13 @@ class FileUploadWorker(
333338
return@launch
334339
}
335340

336-
sendUploadFinishEvent(totalUploadSize, currentUploadIndex, operation, result)
337-
341+
sendUploadFinishEvent(totalUploadSize, currentUploadIndex, operation, result)
338342
} finally {
339343
activeUploadFileOperations.remove(operation.originalStoragePath)
340344
lastPercents.remove(operation.originalStoragePath)
341345
lastUpdateTimes.remove(operation.originalStoragePath)
342346
}
343-
}
347+
}
344348
}
345349
}
346350
}
@@ -383,7 +387,11 @@ class FileUploadWorker(
383387
return result
384388
}
385389

386-
private fun createUploadFileOperation(upload: OCUpload, user: User): UploadFileOperation = UploadFileOperation(
390+
private fun createUploadFileOperation(
391+
upload: OCUpload,
392+
user: User,
393+
storageManager: FileDataStorageManager
394+
): UploadFileOperation = UploadFileOperation(
387395
uploadsStorageManager,
388396
connectivityService,
389397
powerManagementService,
@@ -396,7 +404,7 @@ class FileUploadWorker(
396404
upload.isUseWifiOnly,
397405
upload.isWhileChargingOnly,
398406
true,
399-
FileDataStorageManager(user, context.contentResolver)
407+
storageManager
400408
).apply {
401409
addDataTransferProgressListener(this@FileUploadWorker)
402410
}

0 commit comments

Comments
 (0)