Skip to content

Commit 3f04e7a

Browse files
alperozturk96backportbot[bot]
authored andcommitted
use saveFileWithParent
Signed-off-by: alperozturk <[email protected]>
1 parent 09d95ca commit 3f04e7a

File tree

3 files changed

+38
-57
lines changed

3 files changed

+38
-57
lines changed

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -547,46 +547,65 @@ public boolean saveFile(OCFile ocFile) {
547547
}
548548

549549
/**
550-
* traverses a files parent tree to be able to store a file with its parents. Throws a
551-
* RemoteOperationFailedException in case the parent can't be retrieved.
550+
* Ensures that an {@link OCFile} and all of its parent folders are stored locally.
551+
* <p>
552+
* If the file has no parent ID and is not the root folder, this method recursively:
553+
* <ul>
554+
* <li>Resolves the parent path</li>
555+
* <li>Loads the parent from local storage or fetches it from the server</li>
556+
* <li>Saves all missing parent folders</li>
557+
* <li>Assigns the resolved parent ID to the file</li>
558+
* </ul>
559+
*
560+
* @param ocFile the file to be saved together with its parent hierarchy
561+
* @param context Android context used for remote operations
552562
*
553-
* @param ocFile the file
554-
* @param context the app context
555-
* @return the parent file
563+
* @return the same {@link OCFile} instance with a valid parent ID
564+
*
565+
* @throws RemoteOperationFailedException if a parent folder cannot be retrieved
566+
* from the server
556567
*/
557568
public OCFile saveFileWithParent(OCFile ocFile, Context context) {
558569
if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) {
570+
Log_OC.d(TAG, "saving file with parents: " + ocFile.getRemotePath());
571+
559572
String remotePath = ocFile.getRemotePath();
560573
String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName()));
561574

562575
OCFile parentFile = getFileByPath(parentPath);
563576
OCFile returnFile;
564577

565578
if (parentFile == null) {
566-
// remote request
567-
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath);
568-
// TODO Deprecated
569-
RemoteOperationResult result = operation.execute(getUser(), context);
570-
if (result.isSuccess()) {
571-
OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
572-
573-
returnFile = saveFileWithParent(remoteFolder, context);
579+
Log_OC.d(TAG, "Parent not found locally, fetching: " + parentPath);
580+
581+
final var operation = new ReadFileRemoteOperation(parentPath);
582+
final var result = operation.execute(getUser(), context);
583+
584+
if (result.isSuccess() && result.getData().get(0) instanceof RemoteFile remoteFile) {
585+
OCFile folder = FileStorageUtils.fillOCFile(remoteFile);
586+
Log_OC.d(TAG, "Fetched parent folder: " + folder);
587+
returnFile = saveFileWithParent(folder, context);
574588
} else {
575589
Exception exception = result.getException();
576590
String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / "
577591
+ result.getLogMessage(context);
578592

593+
Log_OC.e(TAG, message);
594+
579595
if (exception != null) {
580596
throw new RemoteOperationFailedException(message, exception);
581597
} else {
582598
throw new RemoteOperationFailedException(message);
583599
}
584600
}
585601
} else {
602+
Log_OC.d(TAG, "parent file exists, calling saveFileWithParent: " + ocFile.getRemotePath());
586603
returnFile = saveFileWithParent(parentFile, context);
587604
}
588605

589-
ocFile.setParentId(returnFile.getFileId());
606+
long parentId = returnFile.getFileId();
607+
Log_OC.d(TAG, "saving parent id of: " + ocFile.getRemotePath() + " with: " + parentId);
608+
ocFile.setParentId(parentId);
590609
saveFile(ocFile);
591610
}
592611

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ protected RemoteOperationResult run(OwnCloudClient client) {
267267
updateUserProfile();
268268
}
269269

270+
fileDataStorageManager.saveFileWithParent(mLocalFolder, mContext);
271+
272+
Log_OC.d(TAG, "STEP 1 --- refreshing folder " + mLocalFolder.getRemotePath());
273+
270274
result = checkForChanges(client);
271275

272276
if (result.isSuccess()) {

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListSearchTask.kt

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ package com.owncloud.android.ui.fragment
1111
import android.annotation.SuppressLint
1212
import android.app.Activity
1313
import android.content.ContentValues
14-
import android.content.Context
1514
import androidx.lifecycle.lifecycleScope
1615
import com.nextcloud.client.account.User
1716
import com.nextcloud.client.preferences.AppPreferences
@@ -41,7 +40,6 @@ import kotlinx.coroutines.launch
4140
import kotlinx.coroutines.withContext
4241
import kotlinx.coroutines.withTimeoutOrNull
4342
import java.lang.ref.WeakReference
44-
import java.util.concurrent.TimeUnit
4543

4644
@Suppress("LongParameterList", "ReturnCount", "TooGenericExceptionCaught", "DEPRECATION", "MagicNumber")
4745
@SuppressLint("NotifyDataSetChanged")
@@ -106,14 +104,6 @@ class OCFileListSearchTask(
106104
fragment.adapter.files
107105
}
108106

109-
val subDirectories = newList
110-
.filter { it.isFolder }
111-
.sortedBy { it.fileId }
112-
113-
subDirectories.forEach { dir ->
114-
fetchDirContent(dir, fileDataStorageManager, fragment.context)
115-
}
116-
117107
val sortedNewList = sortSearchData(newList, searchType, null, setNewSortOrder = {
118108
fragment.adapter.setSortOrder(it)
119109
})
@@ -135,39 +125,6 @@ class OCFileListSearchTask(
135125

136126
fun isFinished(): Boolean = job?.isCompleted == true
137127

138-
private suspend fun fetchDirContent(
139-
folder: OCFile,
140-
storageManager: FileDataStorageManager?,
141-
context: Context?
142-
) = withContext(Dispatchers.IO) {
143-
if (context == null || storageManager == null) {
144-
Log_OC.e(TAG, "sub directory content cannot be fetched, context or storage manager is null")
145-
return@withContext
146-
}
147-
148-
val currentSyncTime = System.currentTimeMillis()
149-
val shouldIgnoreETag = (currentSyncTime - folder.lastSyncDateForProperties) > TimeUnit.MINUTES.toMillis(5)
150-
151-
Log_OC.d(TAG, "fetching content for: " + folder.remotePath + " ignore eTag: " + shouldIgnoreETag)
152-
153-
val operation =
154-
RefreshFolderOperation(
155-
folder,
156-
currentSyncTime,
157-
shouldIgnoreETag,
158-
shouldIgnoreETag,
159-
storageManager,
160-
currentUser,
161-
context
162-
)
163-
val result = operation.execute(currentUser, context)
164-
if (result.isSuccess) {
165-
Log_OC.d(TAG, "sub directory content is fetched")
166-
} else {
167-
Log_OC.e(TAG, "sub directory content cannot be fetched")
168-
}
169-
}
170-
171128
private suspend fun loadCachedDbFiles(searchType: SearchRemoteOperation.SearchType): List<OCFile> {
172129
val storage = fileDataStorageManager ?: return emptyList()
173130
return if (searchType == SearchRemoteOperation.SearchType.SHARED_FILTER) {
@@ -179,6 +136,7 @@ class OCFileListSearchTask(
179136
}.mapNotNull { storage.createFileInstance(it) }
180137
}
181138

139+
@Suppress("DEPRECATION")
182140
private suspend fun fetchRemoteResults(): RemoteOperationResult<List<Any>>? {
183141
val fragment = fragmentReference.get() ?: return null
184142
val context = fragment.context ?: return null

0 commit comments

Comments
 (0)