Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New arch] Copy #3253

Merged
merged 13 commits into from
May 27, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ import org.koin.core.KoinComponent
import org.koin.core.inject
import timber.log.Timber
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.Vector

class FileDataStorageManager : KoinComponent {
Expand Down Expand Up @@ -625,77 +620,6 @@ class FileDataStorageManager : KoinComponent {
// return updatedCount > 0
}

fun copyLocalFile(originalFile: OCFile?, targetPath: String, targetFileRemoteId: String) {
// FIXME: 13/10/2020 : New_arch: Copy file

// if (originalFile != null && originalFile.fileExists() && ROOT_PATH != originalFile.fileName) {
// // 1. Copy in database
// val ocTargetFile = OCFile(targetPath)
// val parentId = getFileByPath(FileStorageUtils.getParentPath(targetPath))!!.fileId
// ocTargetFile.parentId = parentId
// ocTargetFile.remoteId = targetFileRemoteId
// ocTargetFile.fileLength = originalFile.fileLength
// ocTargetFile.mimetype = originalFile.mimetype
// ocTargetFile.modificationTimestamp = System.currentTimeMillis()
// saveFile(ocTargetFile)
//
// // 2. Copy in local file system
// var copied = false
// val localPath = FileStorageUtils.getDefaultSavePathFor(account.name, originalFile)
// val localFile = File(localPath)
// val defaultSavePath = FileStorageUtils.getSavePath(account.name)
// if (localFile.exists()) {
// val targetFile = File(defaultSavePath + targetPath)
// val targetFolder = targetFile.parentFile
// if (targetFolder != null && !targetFolder.exists()) {
// targetFolder.mkdirs()
// }
// copied = copyFile(localFile, targetFile)
// }
//
// Timber.d("Local file COPIED : $copied")
// }
}

private fun copyFile(src: File, target: File): Boolean {
var ret = true

var input: InputStream? = null
var out: OutputStream? = null

try {
input = FileInputStream(src)
out = FileOutputStream(target)
val buf = ByteArray(1024)
var len: Int
do {
len = input.read()
out.write(buf, 0, len)
} while (len > 0)
} catch (ex: IOException) {
ret = false
} finally {
if (input != null) {
try {
input.close()
} catch (e: IOException) {
Timber.e(e)
}

}
if (out != null) {
try {
out.close()
} catch (e: IOException) {
Timber.e(e)
}

}
}

return ret
}

// TODO: New_arch: Remove this and call usecase inside FilesViewModel
fun getFolderContent(parentId: Long): List<OCFile> = runBlocking(CoroutinesDispatcherProvider().io) {
val getFolderContentUseCase: GetFolderContentUseCase by inject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.owncloud.android.domain.authentication.usecases.SupportsOAuth2UseCase
import com.owncloud.android.domain.capabilities.usecases.GetCapabilitiesAsLiveDataUseCase
import com.owncloud.android.domain.capabilities.usecases.GetStoredCapabilitiesUseCase
import com.owncloud.android.domain.capabilities.usecases.RefreshCapabilitiesFromServerAsyncUseCase
import com.owncloud.android.domain.files.usecases.CopyFileUseCase
import com.owncloud.android.domain.files.usecases.CreateFolderAsyncUseCase
import com.owncloud.android.domain.files.usecases.GetFileByIdUseCase
import com.owncloud.android.domain.files.usecases.GetFileByRemotePathUseCase
Expand Down Expand Up @@ -79,6 +80,7 @@ val useCaseModule = module {

// Files
factory { CreateFolderAsyncUseCase(get()) }
factory { CopyFileUseCase(get()) }
factory { GetFileByIdUseCase(get()) }
factory { GetFileByRemotePathUseCase(get()) }
factory { GetFolderContentUseCase(get()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ val viewModelModule = module {

viewModel { PreviewImageViewModel(get(), get(), get()) }
viewModel { FileDetailsViewModel(get(), get(), get(), get(), get()) }
viewModel { FileOperationViewModel(get(), get(), get(), get(), get()) }
viewModel { FileOperationViewModel(get(), get(), get(), get(), get(), get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.owncloud.android.R
import com.owncloud.android.domain.exceptions.AccountNotNewException
import com.owncloud.android.domain.exceptions.AccountNotTheSameException
import com.owncloud.android.domain.exceptions.BadOcVersionException
import com.owncloud.android.domain.exceptions.CopyIntoDescendantException
import com.owncloud.android.domain.exceptions.FileNotFoundException
import com.owncloud.android.domain.exceptions.ForbiddenException
import com.owncloud.android.domain.exceptions.IncorrectAddressException
Expand Down Expand Up @@ -72,6 +73,7 @@ fun Throwable.parseError(
resources.getString(stringId)
}
is MoveIntoDescendantException -> resources.getString(R.string.move_file_invalid_into_descendent)
is CopyIntoDescendantException -> resources.getString(R.string.copy_file_invalid_into_descendent)
is ForbiddenException -> resources.getString(R.string.forbidden_permissions)
is FileNotFoundException -> resources.getString(R.string.common_not_found)
is InstanceNotConfiguredException -> resources.getString(R.string.auth_not_configured_title)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package com.owncloud.android.presentation.ui.files.operations
import com.owncloud.android.domain.files.model.OCFile

sealed class FileOperation {
data class CopyOperation(val listOfFilesToCopy: List<OCFile>, val targetFolder: OCFile) : FileOperation()
data class MoveOperation(val listOfFilesToMove: List<OCFile>, val targetFolder: OCFile) : FileOperation()
data class RemoveOperation(val listOfFilesToRemove: List<OCFile>, val removeOnlyLocalCopy: Boolean) : FileOperation()
data class RenameOperation(val ocFileToRename: OCFile, val newName: String) : FileOperation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.owncloud.android.domain.BaseUseCaseWithResult
import com.owncloud.android.domain.UseCaseResult
import com.owncloud.android.domain.exceptions.NoNetworkConnectionException
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.files.usecases.CopyFileUseCase
import com.owncloud.android.domain.files.usecases.MoveFileUseCase
import com.owncloud.android.domain.files.usecases.RemoveFileUseCase
import com.owncloud.android.domain.files.usecases.RenameFileUseCase
Expand All @@ -38,13 +39,17 @@ import kotlinx.coroutines.launch
import timber.log.Timber

class FileOperationViewModel(
private val copyFileUseCase: CopyFileUseCase,
private val moveFileUseCase: MoveFileUseCase,
private val removeFileUseCase: RemoveFileUseCase,
private val renameFileUseCase: RenameFileUseCase,
private val contextProvider: ContextProvider,
private val coroutinesDispatcherProvider: CoroutinesDispatcherProvider
) : ViewModel() {

private val _copyFileLiveData = MediatorLiveData<Event<UIResult<OCFile>>>()
val copyFileLiveData: LiveData<Event<UIResult<OCFile>>> = _copyFileLiveData

private val _moveFileLiveData = MediatorLiveData<Event<UIResult<OCFile>>>()
val moveFileLiveData: LiveData<Event<UIResult<OCFile>>> = _moveFileLiveData

Expand All @@ -59,9 +64,19 @@ class FileOperationViewModel(
is FileOperation.MoveOperation -> moveOperation(fileOperation)
is FileOperation.RemoveOperation -> removeOperation(fileOperation)
is FileOperation.RenameOperation -> renameOperation(fileOperation)
is FileOperation.CopyOperation -> copyOperation(fileOperation)
}
}

private fun copyOperation(fileOperation: FileOperation.CopyOperation) {
runOperation(
liveData = _copyFileLiveData,
useCase = copyFileUseCase,
useCaseParams = CopyFileUseCase.Params(fileOperation.listOfFilesToCopy, fileOperation.targetFolder),
postValue = fileOperation.targetFolder
)
}

private fun moveOperation(fileOperation: FileOperation.MoveOperation) {
runOperation(
liveData = _moveFileLiveData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ import com.owncloud.android.domain.UseCaseResult
import com.owncloud.android.domain.exceptions.NoConnectionWithServerException
import com.owncloud.android.domain.exceptions.validation.FileNameException
import com.owncloud.android.domain.files.model.OCFile
import com.owncloud.android.domain.files.usecases.CopyFileUseCase
import com.owncloud.android.domain.files.usecases.CreateFolderAsyncUseCase
import com.owncloud.android.domain.files.usecases.MoveFileUseCase
import com.owncloud.android.domain.files.usecases.RemoveFileUseCase
import com.owncloud.android.domain.files.usecases.RenameFileUseCase
import com.owncloud.android.files.services.FileUploader
import com.owncloud.android.files.services.TransferRequester
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.operations.CopyFileOperation
import com.owncloud.android.operations.RefreshFolderOperation
import com.owncloud.android.operations.SynchronizeFileOperation
import com.owncloud.android.operations.UploadFileOperation
Expand Down Expand Up @@ -348,21 +348,21 @@ class DocumentsStorageProvider : DocumentsProvider() {
val targetParentDocId = targetParentDocumentId.toLong()
val targetParentFile = getFileByIdOrException(targetParentDocId)

CopyFileOperation(
sourceFile.remotePath,
targetParentFile.remotePath
).apply {
execute(currentStorageManager, context).also { result ->
syncRequired = false
checkOperationResult(result, targetParentFile.id.toString())
//Returns the document id of the document copied at the target destination
var newPath = targetParentFile.remotePath + sourceFile.fileName
if (sourceFile.isFolder) {
newPath += File.separator
}
val newFile = getFileByPathOrException(newPath)
return newFile.id.toString()
}
val copyFileUseCase: CopyFileUseCase by inject()

copyFileUseCase.execute(
CopyFileUseCase.Params(
listOfFilesToCopy = listOf(sourceFile),
targetFolder = targetParentFile
)
).also { result ->
syncRequired = false
checkUseCaseResult(result, targetParentFile.id.toString())
// Returns the document id of the document copied at the target destination
var newPath = targetParentFile.remotePath + sourceFile.fileName
if (sourceFile.isFolder) newPath += File.separator
val newFile = getFileByPathOrException(newPath)
return newFile.id.toString()
}
}

Expand All @@ -389,7 +389,7 @@ class DocumentsStorageProvider : DocumentsProvider() {
).also { result ->
syncRequired = false
checkUseCaseResult(result, targetParentFile.id.toString())
//Returns the document id of the document moved to the target destination
// Returns the document id of the document moved to the target destination
var newPath = targetParentFile.remotePath + sourceFile.fileName
if (sourceFile.isFolder) newPath += File.separator
val newFile = getFileByPathOrException(newPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.owncloud.android.operations.CheckCurrentCredentialsOperation;
import com.owncloud.android.operations.CopyFileOperation;
import com.owncloud.android.operations.SynchronizeFileOperation;
import com.owncloud.android.operations.SynchronizeFolderOperation;
import com.owncloud.android.operations.common.SyncOperation;
Expand All @@ -64,7 +63,6 @@ public class OperationsService extends Service {
public static final String EXTRA_ACCOUNT = "ACCOUNT";
public static final String EXTRA_SERVER_URL = "SERVER_URL";
public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
public static final String EXTRA_NEW_PARENT_PATH = "NEW_PARENT_PATH";
public static final String EXTRA_FILE = "FILE";
public static final String EXTRA_PUSH_ONLY = "PUSH_ONLY";
public static final String EXTRA_SYNC_REGULAR_FILES = "SYNC_REGULAR_FILES";
Expand All @@ -73,7 +71,6 @@ public class OperationsService extends Service {

public static final String ACTION_SYNC_FILE = "SYNC_FILE";
public static final String ACTION_SYNC_FOLDER = "SYNC_FOLDER";
public static final String ACTION_COPY_FILE = "COPY_FILE";
public static final String ACTION_CHECK_CURRENT_CREDENTIALS = "CHECK_CURRENT_CREDENTIALS";

private ConcurrentMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>
Expand Down Expand Up @@ -468,14 +465,6 @@ private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {

break;
}
case ACTION_COPY_FILE: {
// Copy file/folder
String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
operation = new CopyFileOperation(remotePath, newParentPath);

break;
}
case ACTION_CHECK_CURRENT_CREDENTIALS:
// Check validity of currently stored credentials for a given account
operation = new CheckCurrentCredentialsOperation(account);
Expand Down
Loading