Skip to content

Commit 6872a3d

Browse files
Merge pull request #16175 from nextcloud/backport/16132/stable-3.35
[stable-3.35] fix(auto-upload): remove non-existing local files without restarting
2 parents 13b0bad + 83e8dd2 commit 6872a3d

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ interface FileSystemDao {
1919
@Insert(onConflict = OnConflictStrategy.REPLACE)
2020
fun insertOrReplace(filesystemEntity: FilesystemEntity)
2121

22+
@Query(
23+
"""
24+
DELETE FROM ${ProviderMeta.ProviderTableMeta.FILESYSTEM_TABLE_NAME}
25+
WHERE ${ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH} = :localPath
26+
AND ${ProviderMeta.ProviderTableMeta._ID} = :id
27+
"""
28+
)
29+
suspend fun deleteByLocalPathAndId(localPath: String, id: Int)
30+
2231
@Query(
2332
"""
2433
SELECT *

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ class AutoUploadWorker(
321321

322322
try {
323323
var (uploadEntity, upload) = createEntityAndUpload(user, localPath, remotePath)
324+
325+
// if local file deleted, upload process cannot be started or retriable thus needs to be removed
326+
if (path.isEmpty() || !file.exists()) {
327+
Log_OC.w(TAG, "detected non-existing local file, removing entity")
328+
deleteNonExistingFile(path, id, upload)
329+
continue
330+
}
331+
324332
try {
325333
// Insert/update to IN_PROGRESS state before starting upload
326334
val generatedId = uploadsStorageManager.uploadDao.insertOrReplace(uploadEntity)
@@ -359,21 +367,32 @@ class AutoUploadWorker(
359367
"Exception during upload file, localPath: $localPath, remotePath: $remotePath," +
360368
" exception: $e"
361369
)
370+
371+
if (path.isEmpty() || !file.exists()) {
372+
Log_OC.w(TAG, "detected non-existing local file, removing entity")
373+
deleteNonExistingFile(path, id, upload)
374+
continue
375+
}
362376
}
363377
} catch (e: Exception) {
364378
Log_OC.e(
365379
TAG,
366380
"Exception uploadFiles during creating entity and upload, localPath: $localPath, " +
367381
"remotePath: $remotePath, exception: $e"
368382
)
383+
} finally {
384+
// update last id so upload can continue where it left
385+
lastId = id
369386
}
370-
371-
// update last id so upload can continue where it left
372-
lastId = id
373387
}
374388
}
375389
}
376390

391+
private suspend fun deleteNonExistingFile(path: String, id: Int, upload: OCUpload) {
392+
repository.deleteByLocalPathAndId(path, id)
393+
uploadsStorageManager.removeUpload(upload)
394+
}
395+
377396
private fun createEntityAndUpload(user: User, localPath: String, remotePath: String): Pair<UploadEntity, OCUpload> {
378397
val (needsCharging, needsWifi, uploadAction) = getUploadSettings(syncedFolder)
379398
Log_OC.d(TAG, "creating oc upload for ${user.accountName}")

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class FileSystemRepository(private val dao: FileSystemDao, private val context:
2828
const val BATCH_SIZE = 50
2929
}
3030

31+
suspend fun deleteByLocalPathAndId(path: String, id: Int) {
32+
dao.deleteByLocalPathAndId(path, id)
33+
}
34+
3135
suspend fun getFilePathsWithIds(syncedFolder: SyncedFolder, lastId: Int): List<Pair<String, Int>> {
3236
val syncedFolderId = syncedFolder.id.toString()
3337
Log_OC.d(TAG, "Fetching candidate files for syncedFolderId = $syncedFolderId")

app/src/main/java/com/owncloud/android/operations/UploadFileOperation.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,32 @@ public void handleLocalBehaviour() {
12831283
handleLocalBehaviour(temporalFile, expectedFile, originalFile, client);
12841284
}
12851285

1286+
private void deleteNonExistingFile(File file) {
1287+
if (file.exists()) {
1288+
return;
1289+
}
1290+
1291+
Log_OC.d(TAG, "deleting non-existing file from upload list and file list");
1292+
1293+
uploadsStorageManager.removeUpload(mOCUploadId);
1294+
1295+
// some chunks can be uploaded and can still exists in db thus we have to remove it as well
1296+
getStorageManager().removeFile(mFile, true, true);
1297+
}
1298+
12861299
private void handleLocalBehaviour(File temporalFile,
12871300
File expectedFile,
12881301
File originalFile,
12891302
OwnCloudClient client) {
1303+
1304+
// only LOCAL_BEHAVIOUR_COPY not using original file
1305+
if (mLocalBehaviour != FileUploadWorker.LOCAL_BEHAVIOUR_COPY) {
1306+
// if file is not exists we should only delete from our app
1307+
deleteNonExistingFile(originalFile);
1308+
}
1309+
1310+
Log_OC.d(TAG, "handling local behaviour for: " + originalFile.getName() + " behaviour: " + mLocalBehaviour);
1311+
12901312
switch (mLocalBehaviour) {
12911313
case FileUploadWorker.LOCAL_BEHAVIOUR_DELETE:
12921314
try {
@@ -1305,6 +1327,9 @@ private void handleLocalBehaviour(File temporalFile,
13051327
move(temporalFile, expectedFile);
13061328
} catch (IOException e) {
13071329
Log_OC.e(TAG, e.getMessage());
1330+
1331+
// handling non-existing file for local copy as well
1332+
deleteNonExistingFile(temporalFile);
13081333
}
13091334
} else if (originalFile != null) {
13101335
try {

0 commit comments

Comments
 (0)