Skip to content

Commit 96e130c

Browse files
Merge pull request #17229 from nextcloud/backport/17191/stable-34.0.x
[stable-34.0.x] fix(file-upload-worker): better error handling
2 parents 63f64ae + c32d44f commit 96e130c

2 files changed

Lines changed: 65 additions & 10 deletions

File tree

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.nextcloud.client.network.ConnectivityService
2626
import com.nextcloud.client.preferences.AppPreferences
2727
import com.nextcloud.utils.ForegroundServiceHelper
2828
import com.nextcloud.utils.extensions.getPercent
29+
import com.nextcloud.utils.extensions.isNonRetryable
2930
import com.nextcloud.utils.extensions.toFile
3031
import com.owncloud.android.R
3132
import com.owncloud.android.datamodel.ForegroundServiceType
@@ -34,6 +35,7 @@ import com.owncloud.android.datamodel.SyncedFolderProvider
3435
import com.owncloud.android.datamodel.ThumbnailsCacheManager
3536
import com.owncloud.android.datamodel.UploadsStorageManager
3637
import com.owncloud.android.db.OCUpload
38+
import com.owncloud.android.db.UploadResult
3739
import com.owncloud.android.lib.common.OwnCloudAccount
3840
import com.owncloud.android.lib.common.OwnCloudClient
3941
import com.owncloud.android.lib.common.OwnCloudClientManagerFactory
@@ -204,6 +206,18 @@ class FileUploadWorker(
204206
.setSilent(true)
205207
.build()
206208

209+
private enum class UploadFilesResult {
210+
Success,
211+
Error,
212+
Retry;
213+
214+
fun toWorkerResult(): Result = when (this) {
215+
Success -> Result.success()
216+
Error -> Result.failure()
217+
Retry -> Result.retry()
218+
}
219+
}
220+
207221
@Suppress("ReturnCount", "LongMethod", "DEPRECATION")
208222
private suspend fun uploadFiles(): Result = withContext(Dispatchers.IO) {
209223
val accountName = inputData.getString(ACCOUNT)
@@ -246,6 +260,7 @@ class FileUploadWorker(
246260
val client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, context)
247261
val syncFolderHelper = SyncFolderHelper(context)
248262
val syncedFolders = syncedFolderProvider.syncedFolders
263+
var uploadFilesResult = UploadFilesResult.Success
249264

250265
for ((index, upload) in uploads.withIndex()) {
251266
ensureActive()
@@ -288,20 +303,33 @@ class FileUploadWorker(
288303
)
289304

290305
val result = withContext(Dispatchers.IO) {
291-
upload(upload, operation, user, client)
306+
upload(operation, user, client)
292307
}
293308
activeOperations.remove(upload.uploadId)
294309

310+
// check quota first
295311
if (result.code == ResultCode.QUOTA_EXCEEDED) {
296312
Log_OC.w(TAG, "Quota exceeded, stopping uploads")
297313
notificationManager.showQuotaExceedNotification(operation)
298314
break
299315
}
300316

317+
// check upload result for worker
318+
val uploadResult = UploadResult.fromOperationResult(result)
319+
if (!result.isSuccess) {
320+
Log_OC.e(TAG, "upload failed for ${upload.remotePath}: ${result.code}")
321+
if (uploadResult.isNonRetryable()) {
322+
uploadFilesResult = UploadFilesResult.Error
323+
} else if (uploadFilesResult != UploadFilesResult.Error) {
324+
// only set retry if any other not failed before
325+
uploadFilesResult = UploadFilesResult.Retry
326+
}
327+
}
328+
301329
sendUploadFinishEvent(totalUploadSize, currentUploadIndex, operation, result)
302330
}
303331

304-
return@withContext Result.success()
332+
return@withContext uploadFilesResult.toWorkerResult()
305333
}
306334

307335
@Suppress("ReturnCount")
@@ -356,21 +384,24 @@ class FileUploadWorker(
356384

357385
@Suppress("TooGenericExceptionCaught", "DEPRECATION")
358386
private suspend fun upload(
359-
upload: OCUpload,
360387
operation: UploadFileOperation,
361388
user: User,
362389
client: OwnCloudClient
363390
): RemoteOperationResult<Any?> = withContext(Dispatchers.IO) {
364-
lateinit var result: RemoteOperationResult<Any?>
391+
var result: RemoteOperationResult<Any?>
365392

366393
try {
367394
val storageManager = operation.storageManager
368-
result = operation.execute(client)
369-
val task = ThumbnailsCacheManager.ThumbnailGenerationTask(storageManager, user)
370-
val file = File(operation.originalStoragePath)
371-
val remoteId: String? = operation.file.remoteId
372-
task.execute(ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId))
373395
fileUploadEventBroadcaster.sendUploadStarted(operation, context)
396+
result = operation.execute(client)
397+
398+
// only generate a thumbnail if the upload actually succeeded
399+
if (result.isSuccess) {
400+
val task = ThumbnailsCacheManager.ThumbnailGenerationTask(storageManager, user)
401+
val file = File(operation.originalStoragePath)
402+
val remoteId: String? = operation.file.remoteId
403+
task.execute(ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId))
404+
}
374405
} catch (e: Exception) {
375406
Log_OC.e(TAG, "Error uploading", e)
376407
result = RemoteOperationResult(e)
@@ -405,7 +436,7 @@ class FileUploadWorker(
405436
private var lastUpdateTime = 0L
406437

407438
/**
408-
* Receives from [com.owncloud.android.operations.UploadFileOperation.normalUpload]
439+
* Receives from [UploadFileOperation.normalUpload]
409440
*/
410441
@Suppress("MagicNumber")
411442
override fun onTransferProgress(

gradle/verification-metadata.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20804,6 +20804,14 @@
2080420804
<sha256 value="9ee93f81089c2f4b3841a7efa5c77f845bf13811d5d6a340d35cb960537cfb10" origin="Generated by Gradle" reason="Artifact is not signed"/>
2080520805
</artifact>
2080620806
</component>
20807+
<component group="com.github.nextcloud" name="android-library" version="0282f675c8f11af9f1da91436f1fa95abe2333fe">
20808+
<artifact name="android-library-0282f675c8f11af9f1da91436f1fa95abe2333fe.aar">
20809+
<sha256 value="bddfb0de83c7ff95220cc804bf6e2ccfa3634ccecce7b0e34403f84f311d0187" origin="Generated by Gradle" reason="Artifact is not signed"/>
20810+
</artifact>
20811+
<artifact name="android-library-0282f675c8f11af9f1da91436f1fa95abe2333fe.module">
20812+
<sha256 value="c0cbe0bb8321a22135cd376242ef6ee39615ad984214934dd703187b75b78801" origin="Generated by Gradle" reason="Artifact is not signed"/>
20813+
</artifact>
20814+
</component>
2080720815
<component group="com.github.nextcloud" name="android-library" version="0303be50f9">
2080820816
<artifact name="android-library-0303be50f9.aar">
2080920817
<sha256 value="d10959e216de68719efc582c5698b7e78385a53900672cb024ecc36111be6d2f" origin="Generated by Gradle" reason="Artifact is not signed"/>
@@ -20932,6 +20940,14 @@
2093220940
<sha256 value="324ae2a4961a86e623e9112db4db616b589526834cf37a709ca932c865f4843b" origin="Generated by Gradle"/>
2093320941
</artifact>
2093420942
</component>
20943+
<component group="com.github.nextcloud" name="android-library" version="1bdcdd40eb">
20944+
<artifact name="android-library-1bdcdd40eb.aar">
20945+
<sha256 value="dea88ca0af50d654bb20c44165a0a09d92cca3e984b03f286d63cd9de5e16340" origin="Generated by Gradle" reason="Artifact is not signed"/>
20946+
</artifact>
20947+
<artifact name="android-library-1bdcdd40eb.module">
20948+
<sha256 value="e36ca21dec5c8da36d526c4f463b0f47cabe3aa7967f57463208f24ce128e3b8" origin="Generated by Gradle" reason="Artifact is not signed"/>
20949+
</artifact>
20950+
</component>
2093520951
<component group="com.github.nextcloud" name="android-library" version="1c847678482c739d0b92e27a445d2f5f168f7d33">
2093620952
<artifact name="android-library-1c847678482c739d0b92e27a445d2f5f168f7d33.aar">
2093720953
<sha256 value="2fbe1710b99ab094a4a0c7d1fb6cb32241dfc78188af994fd1ea6e214a754d84" origin="Generated by Gradle" reason="Artifact is not signed"/>
@@ -21388,6 +21404,14 @@
2138821404
<sha256 value="12e69eebf36959a9fc9c4ae61343ec17e74f5af581accee3a14a6b8b4adacc54" origin="Generated by Gradle" reason="Artifact is not signed"/>
2138921405
</artifact>
2139021406
</component>
21407+
<component group="com.github.nextcloud" name="android-library" version="64bab56dce">
21408+
<artifact name="android-library-64bab56dce.aar">
21409+
<sha256 value="57510727181131abcd62696b4de737b5769df4de9276dc868335f8caf65aa547" origin="Generated by Gradle" reason="Artifact is not signed"/>
21410+
</artifact>
21411+
<artifact name="android-library-64bab56dce.module">
21412+
<sha256 value="9c2fa4cf6b957dd26184d88ba952fa6ae1aacdc3b959fd36c7773edd7041eda4" origin="Generated by Gradle" reason="Artifact is not signed"/>
21413+
</artifact>
21414+
</component>
2139121415
<component group="com.github.nextcloud" name="android-library" version="67f7bf5eb832b5fa24f06d9b84db0e21d4c8638c">
2139221416
<artifact name="android-library-67f7bf5eb832b5fa24f06d9b84db0e21d4c8638c.aar">
2139321417
<sha256 value="c253a126ca3c32dd5b373ab1c64668e4603305b3113b052fc0fc5e3c833a913c" origin="Generated by Gradle" reason="Artifact is not signed"/>

0 commit comments

Comments
 (0)