Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ interface FileSystemDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrReplace(filesystemEntity: FilesystemEntity)

@Query(
"""
DELETE FROM ${ProviderMeta.ProviderTableMeta.FILESYSTEM_TABLE_NAME}
WHERE ${ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH} = :localPath
AND ${ProviderMeta.ProviderTableMeta._ID} = :id
"""
)
suspend fun deleteByLocalPathAndId(localPath: String, id: Int)

@Query(
"""
SELECT *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ class AutoUploadWorker(

try {
var (uploadEntity, upload) = createEntityAndUpload(user, localPath, remotePath)

// if local file deleted, upload process cannot be started or retriable thus needs to be removed
if (path.isEmpty() || !file.exists()) {
Log_OC.w(TAG, "detected non-existing local file, removing entity")
deleteNonExistingFile(path, id, upload)
continue
}

try {
// Insert/update to IN_PROGRESS state before starting upload
val generatedId = uploadsStorageManager.uploadDao.insertOrReplace(uploadEntity)
Expand Down Expand Up @@ -359,21 +367,32 @@ class AutoUploadWorker(
"Exception during upload file, localPath: $localPath, remotePath: $remotePath," +
" exception: $e"
)

if (path.isEmpty() || !file.exists()) {
Log_OC.w(TAG, "detected non-existing local file, removing entity")
deleteNonExistingFile(path, id, upload)
continue
}
}
} catch (e: Exception) {
Log_OC.e(
TAG,
"Exception uploadFiles during creating entity and upload, localPath: $localPath, " +
"remotePath: $remotePath, exception: $e"
)
} finally {
// update last id so upload can continue where it left
lastId = id
}

// update last id so upload can continue where it left
lastId = id
}
}
}

private suspend fun deleteNonExistingFile(path: String, id: Int, upload: OCUpload) {
repository.deleteByLocalPathAndId(path, id)
uploadsStorageManager.removeUpload(upload)
}

private fun createEntityAndUpload(user: User, localPath: String, remotePath: String): Pair<UploadEntity, OCUpload> {
val (needsCharging, needsWifi, uploadAction) = getUploadSettings(syncedFolder)
Log_OC.d(TAG, "creating oc upload for ${user.accountName}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class FileSystemRepository(private val dao: FileSystemDao, private val context:
const val BATCH_SIZE = 50
}

suspend fun deleteByLocalPathAndId(path: String, id: Int) {
dao.deleteByLocalPathAndId(path, id)
}

suspend fun getFilePathsWithIds(syncedFolder: SyncedFolder, lastId: Int): List<Pair<String, Int>> {
val syncedFolderId = syncedFolder.id.toString()
Log_OC.d(TAG, "Fetching candidate files for syncedFolderId = $syncedFolderId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,10 +1283,32 @@ public void handleLocalBehaviour() {
handleLocalBehaviour(temporalFile, expectedFile, originalFile, client);
}

private void deleteNonExistingFile(File file) {
if (file.exists()) {
return;
}

Log_OC.d(TAG, "deleting non-existing file from upload list and file list");

uploadsStorageManager.removeUpload(mOCUploadId);

// some chunks can be uploaded and can still exists in db thus we have to remove it as well
getStorageManager().removeFile(mFile, true, true);
}

private void handleLocalBehaviour(File temporalFile,
File expectedFile,
File originalFile,
OwnCloudClient client) {

// only LOCAL_BEHAVIOUR_COPY not using original file
if (mLocalBehaviour != FileUploadWorker.LOCAL_BEHAVIOUR_COPY) {
// if file is not exists we should only delete from our app
deleteNonExistingFile(originalFile);
}

Log_OC.d(TAG, "handling local behaviour for: " + originalFile.getName() + " behaviour: " + mLocalBehaviour);

switch (mLocalBehaviour) {
case FileUploadWorker.LOCAL_BEHAVIOUR_DELETE:
try {
Expand All @@ -1305,6 +1327,9 @@ private void handleLocalBehaviour(File temporalFile,
move(temporalFile, expectedFile);
} catch (IOException e) {
Log_OC.e(TAG, e.getMessage());

// handling non-existing file for local copy as well
deleteNonExistingFile(temporalFile);
}
} else if (originalFile != null) {
try {
Expand Down
Loading