Skip to content

Commit 5b0147d

Browse files
committed
fix(auto-upload): check qualification before insert
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 285fa05 commit 5b0147d

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
@@ -60,27 +59,19 @@ class FileSystemRepository(
6059
val entities = dao.getAutoUploadFilesEntities(syncedFolderId, BATCH_SIZE, lastId)
6160
val filtered = mutableListOf<Pair<String, Int>>()
6261

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

78-
if (entity.id != null) {
79-
filtered.add(path to entity.id)
80-
} else {
81-
Log_OC.w(TAG, "cant adding path to upload, id is null")
82-
}
83-
}
69+
Log_OC.d(TAG, "Adding path to upload: ${entity.localPath}")
70+
71+
if (entity.id != null) {
72+
filtered.add(entity.localPath!! to entity.id)
73+
} else {
74+
Log_OC.w(TAG, "cant adding path to upload, id is null")
8475
}
8576
}
8677

@@ -176,7 +167,7 @@ class FileSystemRepository(
176167
checkFileType: Boolean = false
177168
) {
178169
try {
179-
val file = localPath?.toFile()
170+
val file = SyncedFolderUtils.validateForAutoUpload(localPath)
180171
if (file == null) {
181172
Log_OC.w(TAG, "file null, cannot insert or replace: $localPath")
182173
return
@@ -187,7 +178,7 @@ class FileSystemRepository(
187178
return
188179
}
189180

190-
val entity = dao.getFileByPathAndFolder(localPath, syncedFolder.id.toString())
181+
val entity = dao.getFileByPathAndFolder(localPath!!, syncedFolder.id.toString())
191182

192183
val fileModified = (lastModified ?: file.lastModified())
193184
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)