|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2025 TSI-mc <surinder.kumar@t-systems.com> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only |
| 6 | + */ |
| 7 | +package com.owncloud.android.operations |
| 8 | + |
| 9 | +import android.content.Context |
| 10 | +import com.nextcloud.client.account.User |
| 11 | +import com.owncloud.android.MainApp |
| 12 | +import com.owncloud.android.datamodel.FileDataStorageManager |
| 13 | +import com.owncloud.android.datamodel.OCFile |
| 14 | +import com.owncloud.android.lib.common.OwnCloudClient |
| 15 | +import com.owncloud.android.lib.common.operations.RemoteOperation |
| 16 | +import com.owncloud.android.lib.common.operations.RemoteOperationResult |
| 17 | +import com.owncloud.android.lib.common.utils.Log_OC |
| 18 | +import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation |
| 19 | +import com.owncloud.android.lib.resources.files.SearchRemoteOperation |
| 20 | +import com.owncloud.android.lib.resources.files.model.RemoteFile |
| 21 | +import com.owncloud.android.operations.common.SyncOperation |
| 22 | +import com.owncloud.android.utils.FileStorageUtils |
| 23 | + |
| 24 | +/** |
| 25 | + * fetch OCFile meta data if not present in local db |
| 26 | + * |
| 27 | + * @see com.owncloud.android.ui.asynctasks.FetchRemoteFileTask reference for this operation |
| 28 | + * |
| 29 | + * @param ocFile file for which metadata has to retrieve |
| 30 | + * @param removeFileFromDb if you want to remove ocFile from local db to avoid duplicate entries for same fileId |
| 31 | + */ |
| 32 | +class FetchRemoteFileOperation( |
| 33 | + private val context: Context, |
| 34 | + private val user: User, |
| 35 | + private val ocFile: OCFile, |
| 36 | + private val removeFileFromDb: Boolean = false, |
| 37 | + storageManager: FileDataStorageManager, |
| 38 | +) : SyncOperation(storageManager) { |
| 39 | + |
| 40 | + @Deprecated("Deprecated in Java") |
| 41 | + override fun run(client: OwnCloudClient?): RemoteOperationResult<*>? { |
| 42 | + val searchRemoteOperation = SearchRemoteOperation( |
| 43 | + ocFile.localId.toString(), |
| 44 | + SearchRemoteOperation.SearchType.FILE_ID_SEARCH, |
| 45 | + false, |
| 46 | + storageManager.getCapability(user) |
| 47 | + ) |
| 48 | + val remoteOperationResult: RemoteOperationResult<List<RemoteFile>> = |
| 49 | + searchRemoteOperation.execute(user, context) |
| 50 | + |
| 51 | + if (remoteOperationResult.isSuccess && remoteOperationResult.resultData != null) { |
| 52 | + if (remoteOperationResult.resultData.isEmpty()) { |
| 53 | + Log_OC.e(TAG, "No remote file found with id: ${ocFile.localId}.") |
| 54 | + return remoteOperationResult |
| 55 | + } |
| 56 | + val remotePath = (remoteOperationResult.resultData[0]).remotePath |
| 57 | + |
| 58 | + val operation = ReadFileRemoteOperation(remotePath) |
| 59 | + val result = operation.execute(user, context) |
| 60 | + |
| 61 | + if (!result.isSuccess) { |
| 62 | + val exception = result.exception |
| 63 | + val message = |
| 64 | + "Fetching file " + remotePath + " fails with: " + result.getLogMessage(MainApp.getAppContext()) |
| 65 | + Log_OC.e(TAG, exception?.message ?: message) |
| 66 | + |
| 67 | + return result |
| 68 | + } |
| 69 | + |
| 70 | + val remoteFile = result.data[0] as RemoteFile |
| 71 | + |
| 72 | + // remove file from local db |
| 73 | + if (removeFileFromDb) { |
| 74 | + storageManager.removeFile(ocFile, true, true) |
| 75 | + } |
| 76 | + |
| 77 | + var ocFile = FileStorageUtils.fillOCFile(remoteFile) |
| 78 | + FileStorageUtils.searchForLocalFileInDefaultPath(ocFile, user.accountName) |
| 79 | + ocFile = storageManager.saveFileWithParent(ocFile, context) |
| 80 | + |
| 81 | + // also sync folder content |
| 82 | + val toSync: OCFile? = if (ocFile?.isFolder == true) { |
| 83 | + ocFile |
| 84 | + } else { |
| 85 | + ocFile?.parentId?.let { storageManager.getFileById(it) } |
| 86 | + } |
| 87 | + |
| 88 | + val currentSyncTime = System.currentTimeMillis() |
| 89 | + val refreshFolderOperation: RemoteOperation<Any> = RefreshFolderOperation( |
| 90 | + toSync, |
| 91 | + currentSyncTime, |
| 92 | + true, |
| 93 | + true, |
| 94 | + storageManager, |
| 95 | + user, |
| 96 | + context |
| 97 | + ) |
| 98 | + val refreshOperationResult = refreshFolderOperation.execute(user, context) |
| 99 | + |
| 100 | + // set the fetched ocFile to resultData to be handled at ui end |
| 101 | + refreshOperationResult.resultData = ocFile |
| 102 | + |
| 103 | + return refreshOperationResult |
| 104 | + } |
| 105 | + return remoteOperationResult |
| 106 | + } |
| 107 | + |
| 108 | + companion object { |
| 109 | + private val TAG = FetchRemoteFileOperation::class.java.simpleName |
| 110 | + } |
| 111 | +} |
0 commit comments