diff --git a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt index b322b1bc7fc4..96756a0646b8 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt @@ -159,20 +159,19 @@ class FileSystemRepository(private val dao: FileSystemDao, private val context: return } + val entity = dao.getFileByPathAndFolder(localPath, syncedFolder.id.toString()) + val fileSentForUpload = (entity != null && entity.fileSentForUpload == 1) + if (fileSentForUpload) { + Log_OC.d(TAG, "File was sent for upload, checking if it changed...") + } + val fileModified = (lastModified ?: file.lastModified()) - val shouldSkipFileBasedOnFolderSettings = syncedFolder.shouldSkipFile(file, fileModified, creationTime) - if (shouldSkipFileBasedOnFolderSettings) { + if (syncedFolder.shouldSkipFile(file, fileModified, creationTime, fileSentForUpload)) { return } - val entity = dao.getFileByPathAndFolder(localPath, syncedFolder.id.toString()) - if (entity != null && entity.fileSentForUpload == 1) { - Log_OC.w( - TAG, - "file already uploaded path: $localPath, " + - "syncedFolder: ${syncedFolder.localPath}, ${syncedFolder.id}" - ) - return + if (fileSentForUpload) { + Log_OC.d(TAG, "File was sent for upload before but has changed, will re-upload: $localPath") } val crc = getFileChecksum(file) @@ -182,7 +181,7 @@ class FileSystemRepository(private val dao: FileSystemDao, private val context: localPath = localPath, fileIsFolder = if (file.isDirectory) 1 else 0, fileFoundRecently = System.currentTimeMillis(), - fileSentForUpload = 0, + fileSentForUpload = 0, // Reset to 0 to queue for upload syncedFolderId = syncedFolder.id.toString(), crc32 = crc?.toString(), fileModified = fileModified diff --git a/app/src/main/java/com/nextcloud/utils/extensions/SyncedFolderExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/SyncedFolderExtensions.kt index c7bdadfe6f54..7f8cac806f2f 100644 --- a/app/src/main/java/com/nextcloud/utils/extensions/SyncedFolderExtensions.kt +++ b/app/src/main/java/com/nextcloud/utils/extensions/SyncedFolderExtensions.kt @@ -22,7 +22,12 @@ private const val TAG = "SyncedFolderExtensions" * Determines whether a file should be skipped during auto-upload based on folder settings. */ @Suppress("ReturnCount") -fun SyncedFolder.shouldSkipFile(file: File, lastModified: Long, creationTime: Long?): Boolean { +fun SyncedFolder.shouldSkipFile( + file: File, + lastModified: Long, + creationTime: Long?, + fileSentForUpload: Boolean +): Boolean { Log_OC.d(TAG, "Checking file: ${file.name}, lastModified=$lastModified, lastScan=$lastScanTimestampMs") if (isExcludeHidden && file.isHidden) { @@ -38,15 +43,19 @@ fun SyncedFolder.shouldSkipFile(file: File, lastModified: Long, creationTime: Lo return true } } else { - Log_OC.w(TAG, "file sent for upload - cannot determine creation time: ${file.absolutePath}") + Log_OC.w(TAG, "file will be inserted to db - cannot determine creation time: ${file.absolutePath}") return false } } - // Skip files that haven't changed since last scan (already processed) - // BUT only if this is not the first scan - if (lastScanTimestampMs != -1L && lastModified < lastScanTimestampMs) { - Log_OC.d(TAG, "Skipping unchanged file (last modified < last scan): ${file.absolutePath}") + // Skip files that haven't changed since last scan ONLY if they were sent for upload + // AND only if this is not the first scan + if (fileSentForUpload && lastScanTimestampMs != -1L && lastModified < lastScanTimestampMs) { + Log_OC.d( + TAG, + "Skipping unchanged file that was already sent for upload (last modified < last scan): " + + "${file.absolutePath}" + ) return true } diff --git a/app/src/test/java/com/owncloud/android/utils/AutoUploadHelperTest.kt b/app/src/test/java/com/owncloud/android/utils/AutoUploadHelperTest.kt index 993ee41a2915..a7aff50a8cc3 100644 --- a/app/src/test/java/com/owncloud/android/utils/AutoUploadHelperTest.kt +++ b/app/src/test/java/com/owncloud/android/utils/AutoUploadHelperTest.kt @@ -132,10 +132,10 @@ class AutoUploadHelperTest { type = MediaFolderType.CUSTOM ) - val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null) + val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null, true) assertTrue(shouldSkipOldFile) - val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null) + val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null, false) assertTrue(!shouldSkipNewFile) } @@ -157,10 +157,10 @@ class AutoUploadHelperTest { lastScanTimestampMs = currentTime } - val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null) + val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null, true) assertTrue(shouldSkipOldFile) - val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null) + val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null, false) assertTrue(!shouldSkipNewFile) } @@ -305,10 +305,10 @@ class AutoUploadHelperTest { setEnabled(true, currentTime) } - val shouldSkipOldFile = folderSkipOld.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime) + val shouldSkipOldFile = folderSkipOld.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime, true) assertTrue(shouldSkipOldFile) - val shouldSkipNewFile = folderSkipOld.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime) + val shouldSkipNewFile = folderSkipOld.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime, false) assertTrue(!shouldSkipNewFile) val folderUploadAll = createTestFolder( @@ -320,11 +320,11 @@ class AutoUploadHelperTest { } val shouldSkipOldFileIfAlsoUploadExistingFile = - folderUploadAll.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime) + folderUploadAll.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime, true) assertTrue(!shouldSkipOldFileIfAlsoUploadExistingFile) val shouldSkipNewFileIfAlsoUploadExistingFile = - folderUploadAll.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime) + folderUploadAll.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime, false) assertTrue(!shouldSkipNewFileIfAlsoUploadExistingFile) } }