Skip to content

Commit 70ba60c

Browse files
Merge pull request #16711 from nextcloud/fix/auto-upload-check-qualification-before-insert
fix(auto-upload): check qualification before insert
2 parents ea61c8f + 9f9a5c1 commit 70ba60c

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

app/src/main/java/com/nextcloud/client/jobs/autoUpload/FileSystemRepository.kt

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import android.provider.MediaStore
1414
import com.nextcloud.client.database.dao.FileSystemDao
1515
import com.nextcloud.client.database.entity.FilesystemEntity
1616
import com.nextcloud.utils.extensions.shouldSkipFile
17-
import com.nextcloud.utils.extensions.toFile
1817
import com.owncloud.android.datamodel.SyncedFolder
1918
import com.owncloud.android.datamodel.UploadsStorageManager
2019
import com.owncloud.android.lib.common.utils.Log_OC
@@ -62,27 +61,19 @@ class FileSystemRepository(
6261
val entities = dao.getAutoUploadFilesEntities(syncedFolderId, BATCH_SIZE, lastId)
6362
val filtered = mutableListOf<Pair<String, Int>>()
6463

65-
entities.forEach { entity ->
66-
entity.localPath?.let { path ->
67-
val file = File(path)
68-
if (!file.exists()) {
69-
Log_OC.w(TAG, "Ignoring file for upload (doesn't exist): $path")
70-
deleteAutoUploadAndUploadEntity(syncedFolder, path, entity)
71-
} else if (!SyncedFolderUtils.isQualifiedFolder(file.parent)) {
72-
Log_OC.w(TAG, "Ignoring file for upload (unqualified folder): $path")
73-
deleteAutoUploadAndUploadEntity(syncedFolder, path, entity)
74-
} else if (!SyncedFolderUtils.isFileNameQualifiedForAutoUpload(file.name)) {
75-
Log_OC.w(TAG, "Ignoring file for upload (unqualified file): $path")
76-
deleteAutoUploadAndUploadEntity(syncedFolder, path, entity)
77-
} else {
78-
Log_OC.d(TAG, "Adding path to upload: $path")
64+
for (entity in entities) {
65+
val file = SyncedFolderUtils.validateForAutoUpload(entity.localPath)
66+
if (file == null) {
67+
deleteAutoUploadAndUploadEntity(syncedFolder, entity.localPath ?: "", entity)
68+
continue
69+
}
7970

80-
if (entity.id != null) {
81-
filtered.add(path to entity.id)
82-
} else {
83-
Log_OC.w(TAG, "cant adding path to upload, id is null")
84-
}
85-
}
71+
Log_OC.d(TAG, "Adding path to upload: ${entity.localPath}")
72+
73+
if (entity.id != null) {
74+
filtered.add(entity.localPath!! to entity.id)
75+
} else {
76+
Log_OC.w(TAG, "cant adding path to upload, id is null")
8677
}
8778
}
8879

@@ -178,7 +169,7 @@ class FileSystemRepository(
178169
checkFileType: Boolean = false
179170
) {
180171
try {
181-
val file = localPath?.toFile()
172+
val file = SyncedFolderUtils.validateForAutoUpload(localPath)
182173
if (file == null) {
183174
Log_OC.w(TAG, "file null, cannot insert or replace: $localPath")
184175
return
@@ -189,7 +180,7 @@ class FileSystemRepository(
189180
return
190181
}
191182

192-
val entity = dao.getFileByPathAndFolder(localPath, syncedFolder.id.toString())
183+
val entity = dao.getFileByPathAndFolder(localPath!!, syncedFolder.id.toString())
193184

194185
val fileModified = (lastModified ?: file.lastModified())
195186
val hasNotChanged = entity?.fileModified == fileModified

app/src/main/java/com/owncloud/android/utils/SyncedFolderUtils.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
*/
88
package com.owncloud.android.utils
99

10+
import com.nextcloud.utils.extensions.toFile
1011
import com.owncloud.android.datamodel.MediaFolder
1112
import com.owncloud.android.datamodel.MediaFolderType
1213
import com.owncloud.android.datamodel.SyncedFolder
14+
import com.owncloud.android.lib.common.utils.Log_OC
1315
import java.io.File
1416

1517
/**
1618
* Utility class with methods for processing synced folders.
1719
*/
1820
object SyncedFolderUtils {
21+
private const val TAG = "SyncedFolderUtils"
22+
1923
private val DISQUALIFIED_MEDIA_DETECTION_SOURCE = listOf(
2024
"cover.jpg",
2125
"cover.jpeg",
@@ -174,4 +178,25 @@ object SyncedFolderUtils {
174178
}
175179
return false
176180
}
181+
182+
@Suppress("ReturnCount")
183+
fun validateForAutoUpload(path: String?): File? {
184+
val file = path?.toFile()
185+
if (file == null) {
186+
Log_OC.w(TAG, "Ignoring file for upload (doesn't exist): $path")
187+
return null
188+
}
189+
190+
if (!isQualifiedFolder(file.parent)) {
191+
Log_OC.w(TAG, "Ignoring file for upload (unqualified folder): $path")
192+
return null
193+
}
194+
195+
if (!isFileNameQualifiedForAutoUpload(file.name)) {
196+
Log_OC.w(TAG, "Ignoring file for upload (unqualified file): $path")
197+
return null
198+
}
199+
200+
return file
201+
}
177202
}

0 commit comments

Comments
 (0)