@@ -58,7 +58,7 @@ import java.util.Date
5858class UploadWorker (appContext : Context , params : WorkerParameters ) : CoroutineWorker(appContext, params) {
5959 private lateinit var contentResolver: ContentResolver
6060
61- private val failedNames = mutableListOf< String >()
61+ private val failedNamesMap = mutableMapOf< String , String >()
6262 private val successNames = mutableListOf<String >()
6363 private var failedCount = 0
6464 private var successCount = 0
@@ -77,7 +77,7 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
7777 if (runAttemptCount >= MAX_RETRY_COUNT ) return Result .failure()
7878
7979 return runUploadCatching {
80- var syncNewPendingUploads = false
80+ var syncNewPendingUploads: Boolean
8181 var result: Result
8282 var retryError = 0
8383 var lastUploadFileName = " "
@@ -167,32 +167,50 @@ class UploadWorker(appContext: Context, params: WorkerParameters) : CoroutineWor
167167 SentryLog .d(TAG , " startSyncFiles> upload for ${uploadFiles.count()} " )
168168
169169 for ((index, uploadFile) in uploadFiles.withIndex()) {
170- SentryLog .d(TAG , " startSyncFiles> size: ${uploadFile.fileSize} " )
171-
172- val fileUploadedWithSuccess = uploadFile.initUpload(isLastFile = index == uploadFiles.lastIndex)
173- if (fileUploadedWithSuccess) {
174- SentryLog .i(TAG , " startSyncFiles: file uploaded with success" )
175- successNames.add(uploadFile.fileName)
176- successCount++
177- } else {
178- SentryLog .i(TAG , " startSyncFiles: file upload failed" )
179- failedNames.add(uploadFile.fileName)
180- failedCount++
181- }
170+ val isLastFile = index == uploadFiles.lastIndex
171+ startUpload(uploadFile, isLastFile)
182172
183173 pendingCount--
184174
185- if (uploadFile.isSync() && UploadFile .getAllPendingPriorityFilesCount() > 0 ) break
175+ // Stop recursion if all files have been processed and there are only errors.
176+ if (isLastFile && failedNamesMap.count() == UploadFile .getAllPendingUploadsCount()) break
177+ // If there is a new file during the sync and it has has priority (ex: Manual uploads),
178+ // then we start again in order to process the priority files first.
179+ if (uploadFile.isSync() && UploadFile .getAllPendingPriorityFilesCount() > 0 ) return @withContext startSyncFiles()
186180 }
187181
188182 uploadedCount = successCount
189183
190184 SentryLog .d(TAG , " startSyncFiles: finish with $uploadedCount uploaded" )
191185
192- currentUploadFile?.showUploadedFilesNotification(applicationContext, successCount, successNames, failedCount, failedNames)
186+ currentUploadFile?.showUploadedFilesNotification(
187+ context = applicationContext,
188+ successCount = successCount,
189+ successNames = successNames,
190+ failedCount = failedCount,
191+ failedNames = failedNamesMap.values,
192+ )
193193 if (uploadedCount > 0 ) Result .success() else Result .failure()
194194 }
195195
196+ private suspend fun startUpload (uploadFile : UploadFile , isLastFile : Boolean ) {
197+ SentryLog .d(TAG , " startSyncFiles> size: ${uploadFile.fileSize} " )
198+
199+ val fileUploadedWithSuccess = uploadFile.initUpload(isLastFile)
200+ if (fileUploadedWithSuccess) {
201+ SentryLog .i(TAG , " startSyncFiles: file uploaded with success" )
202+ successNames.add(uploadFile.fileName)
203+ if (failedNamesMap[uploadFile.uri] != null ) failedNamesMap.remove(uploadFile.uri)
204+ successCount++
205+ } else {
206+ SentryLog .i(TAG , " startSyncFiles: file upload failed" )
207+ if (failedNamesMap[uploadFile.uri] == null ) {
208+ failedNamesMap[uploadFile.uri] = uploadFile.fileName
209+ failedCount++
210+ }
211+ }
212+ }
213+
196214 private fun checkUploadCountReliability (realm : Realm ) {
197215 val allPendingUploadsCount = UploadFile .getAllPendingUploadsCount(realm)
198216 if (allPendingUploadsCount != pendingCount) {
0 commit comments