Skip to content

Commit 877d53f

Browse files
committed
fix(auto-upload): clean stale upload entities
Signed-off-by: alperozturk96 <alper_ozturk@proton.me> # Conflicts: # app/src/main/java/com/nextcloud/client/database/dao/FileSystemDao.kt
1 parent 5d00ccc commit 877d53f

9 files changed

Lines changed: 1362 additions & 22 deletions

File tree

app/schemas/com.nextcloud.client.database.NextcloudDatabase/99.json

Lines changed: 1308 additions & 0 deletions
Large diffs are not rendered by default.

app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ import com.owncloud.android.db.ProviderMeta
9393
AutoMigration(from = 93, to = 94, spec = DatabaseMigrationUtil.ResetCapabilitiesPostMigration::class),
9494
AutoMigration(from = 94, to = 95, spec = DatabaseMigrationUtil.ResetCapabilitiesPostMigration::class),
9595
AutoMigration(from = 95, to = 96),
96-
AutoMigration(from = 96, to = 97, spec = DatabaseMigrationUtil.ResetCapabilitiesPostMigration::class)
96+
AutoMigration(from = 96, to = 97, spec = DatabaseMigrationUtil.ResetCapabilitiesPostMigration::class),
9797
// manual migration used for 97 to 98
98+
AutoMigration(from = 98, to = 99)
9899
],
99100
exportSchema = true
100101
)

app/src/main/java/com/nextcloud/client/database/dao/UploadDao.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ import com.owncloud.android.db.ProviderMeta.ProviderTableMeta
1616

1717
@Dao
1818
interface UploadDao {
19-
@Query(
20-
"""
21-
DELETE FROM ${ProviderTableMeta.UPLOADS_TABLE_NAME}
22-
WHERE ${ProviderTableMeta.UPLOADS_ACCOUNT_NAME} = :accountName
23-
AND ${ProviderTableMeta.UPLOADS_REMOTE_PATH} LIKE :remotePath || '%'
24-
"""
25-
)
26-
suspend fun removeEntities(accountName: String, remotePath: String)
27-
2819
@Query(
2920
"SELECT _id FROM " + ProviderTableMeta.UPLOADS_TABLE_NAME +
3021
" WHERE " + ProviderTableMeta.UPLOADS_STATUS + " = :status AND " +
@@ -52,6 +43,15 @@ interface UploadDao {
5243
)
5344
fun deleteByRemotePathAndAccountName(remotePath: String, accountName: String)
5445

46+
@Query(
47+
"""
48+
DELETE FROM ${ProviderTableMeta.UPLOADS_TABLE_NAME}
49+
WHERE ${ProviderTableMeta.UPLOADS_LOCAL_PATH} = :localPath
50+
AND ${ProviderTableMeta.UPLOADS_REMOTE_PATH} = :remotePath
51+
"""
52+
)
53+
suspend fun deleteByLocalRemotePath(localPath: String, remotePath: String)
54+
5555
@Query(
5656
"SELECT * FROM " + ProviderTableMeta.UPLOADS_TABLE_NAME +
5757
" WHERE " + ProviderTableMeta._ID + " = :id AND " +

app/src/main/java/com/nextcloud/client/database/entity/FilesystemEntity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ data class FilesystemEntity(
1919
val id: Int?,
2020
@ColumnInfo(name = ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH)
2121
val localPath: String?,
22+
@ColumnInfo(name = ProviderTableMeta.FILESYSTEM_FILE_REMOTE_PATH)
23+
val remotePath: String?,
2224
@ColumnInfo(name = ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER)
2325
val fileIsFolder: Int?,
2426
@ColumnInfo(name = ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ class AutoUploadWorker(
292292
try {
293293
// Insert/update to IN_PROGRESS state before starting upload
294294
val generatedId = uploadsStorageManager.uploadDao.insertOrReplace(uploadEntity)
295+
repository.updateRemotePath(upload, syncedFolder)
295296
uploadEntity = uploadEntity.copy(id = generatedId.toInt())
296297
upload.uploadId = generatedId
297298

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.nextcloud.client.database.entity.FilesystemEntity
1616
import com.nextcloud.utils.extensions.shouldSkipFile
1717
import com.owncloud.android.datamodel.SyncedFolder
1818
import com.owncloud.android.datamodel.UploadsStorageManager
19+
import com.owncloud.android.db.OCUpload
1920
import com.owncloud.android.lib.common.utils.Log_OC
2021
import com.owncloud.android.utils.SyncedFolderUtils
2122
import java.io.File
@@ -80,6 +81,21 @@ class FileSystemRepository(
8081
return filtered
8182
}
8283

84+
suspend fun updateRemotePath(upload: OCUpload, syncedFolder: SyncedFolder) {
85+
val syncedFolderIdStr = syncedFolder.id.toString()
86+
87+
try {
88+
dao.updateRemotePath(remotePath = upload.remotePath, localPath = upload.localPath, syncedFolderIdStr)
89+
Log_OC.d(
90+
TAG,
91+
"file system entity remote path updated. remotePath: ${upload.remotePath}, localPath: " +
92+
"${upload.localPath} for syncedFolderId=$syncedFolderIdStr"
93+
)
94+
} catch (e: Exception) {
95+
Log_OC.e(TAG, "updateRemotePath(): ${e.message}", e)
96+
}
97+
}
98+
8399
suspend fun markFileAsHandled(localPath: String, syncedFolder: SyncedFolder) {
84100
val syncedFolderIdStr = syncedFolder.id.toString()
85101

@@ -199,6 +215,7 @@ class FileSystemRepository(
199215
val newEntity = FilesystemEntity(
200216
id = entity?.id,
201217
localPath = localPath,
218+
remotePath = null, // will be updated later
202219
fileIsFolder = if (file.isDirectory) 1 else 0,
203220
fileFoundRecently = System.currentTimeMillis(),
204221
fileSentForUpload = 0, // Reset to 0 to queue for upload

app/src/main/java/com/owncloud/android/datamodel/UploadsStorageManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.nextcloud.client.account.CurrentAccountProvider;
2323
import com.nextcloud.client.account.User;
2424
import com.nextcloud.client.database.NextcloudDatabase;
25+
import com.nextcloud.client.database.dao.FileSystemDao;
2526
import com.nextcloud.client.database.dao.UploadDao;
2627
import com.nextcloud.client.database.entity.UploadEntity;
2728
import com.nextcloud.client.database.entity.UploadEntityKt;
@@ -69,7 +70,8 @@ public class UploadsStorageManager extends Observable {
6970
private final ContentResolver contentResolver;
7071
private final CurrentAccountProvider currentAccountProvider;
7172
private OCCapability capability;
72-
public final UploadDao uploadDao = NextcloudDatabase.getInstance(MainApp.getAppContext()).uploadDao();
73+
public final UploadDao uploadDao = NextcloudDatabase.instance().uploadDao();
74+
public final FileSystemDao fileSystemDao = NextcloudDatabase.instance().fileSystemDao();
7375

7476
public UploadsStorageManager(
7577
CurrentAccountProvider currentAccountProvider,

app/src/main/java/com/owncloud/android/db/ProviderMeta.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public class ProviderMeta {
2525
public static final String DB_NAME = "filelist";
26-
public static final int DB_VERSION = 98;
26+
public static final int DB_VERSION = 99;
2727

2828
private ProviderMeta() {
2929
// No instance
@@ -361,6 +361,7 @@ static public class ProviderTableMeta implements BaseColumns {
361361

362362
// Columns of filesystem data table
363363
public static final String FILESYSTEM_FILE_LOCAL_PATH = "local_path";
364+
public static final String FILESYSTEM_FILE_REMOTE_PATH = "remote_path";
364365
public static final String FILESYSTEM_FILE_MODIFIED = "modified_at";
365366
public static final String FILESYSTEM_FILE_IS_FOLDER = "is_folder";
366367
public static final String FILESYSTEM_FILE_FOUND_RECENTLY = "found_at";

app/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -778,14 +778,26 @@ class SyncedFoldersActivity :
778778
Log_OC.d(TAG, "auto-upload configuration sync status is disabled: " + item.remotePath)
779779

780780
lifecycleScope.launch(Dispatchers.IO) {
781-
fileUploadHelper.uploadsStorageManager.uploadDao
782-
.removeEntities(
783-
accountName = userAccountManager.user.accountName,
784-
remotePath = item.remotePath
785-
)
781+
removeEntityFromUploadEntities(item.id)
786782
}
787783
}
788784

785+
private suspend fun removeEntityFromUploadEntities(id: Long) {
786+
val storageManager = fileUploadHelper.uploadsStorageManager
787+
storageManager.fileSystemDao.getBySyncedFolderId(id.toString())
788+
.filter { it.localPath != null && it.remotePath != null }
789+
.forEach {
790+
Log_OC.d(
791+
TAG,
792+
"deleting upload entity localPath: ${it.localPath}, " + "remotePath: ${it.remotePath}"
793+
)
794+
storageManager.uploadDao.deleteByLocalRemotePath(
795+
localPath = it.localPath!!,
796+
remotePath = it.remotePath!!
797+
)
798+
}
799+
}
800+
789801
override fun onDeleteSyncedFolderPreference(syncedFolder: SyncedFolderParcelable?) {
790802
if (syncedFolder == null) {
791803
return
@@ -794,11 +806,7 @@ class SyncedFoldersActivity :
794806
Log_OC.d(TAG, "deleting auto upload configuration: " + syncedFolder.remotePath)
795807

796808
lifecycleScope.launch(Dispatchers.IO) {
797-
fileUploadHelper.uploadsStorageManager.uploadDao
798-
.removeEntities(
799-
accountName = userAccountManager.user.accountName,
800-
remotePath = syncedFolder.remotePath
801-
)
809+
removeEntityFromUploadEntities(syncedFolder.id)
802810
syncedFolderProvider.deleteSyncedFolder(syncedFolder.id)
803811
withContext(Dispatchers.Main) {
804812
adapter.removeItem(syncedFolder.section)

0 commit comments

Comments
 (0)