Skip to content

Commit

Permalink
Check if new name already exists in the local database before calling…
Browse files Browse the repository at this point in the history
… remote operation
  • Loading branch information
abelgardep committed May 19, 2021
1 parent e5c4665 commit ea25d2a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ class LocalStorageProvider(
*/
fun getDefaultSavePathFor(accountName: String?, remotePath: String): String = getSavePath(accountName) + remotePath

/**
* Get expected remote path for a file creation, rename, move etc
*/
fun getExpectedRemotePath(remotePath: String, newName: String, isFolder: Boolean): String {
var parent = (File(remotePath)).parent ?: throw IllegalArgumentException()
parent = if (parent.endsWith(File.separator)) parent else parent + File.separator
val newRemotePath = parent + newName
if (isFolder) {
newRemotePath.plus(File.separator)
}
return newRemotePath
}

/**
* Get absolute path to tmp folder inside datafolder in sd-card for given accountName.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package com.owncloud.android.data.files.repository
import com.owncloud.android.data.LocalStorageProvider
import com.owncloud.android.data.files.datasources.LocalFileDataSource
import com.owncloud.android.data.files.datasources.RemoteFileDataSource
import com.owncloud.android.domain.exceptions.ConflictException
import com.owncloud.android.domain.exceptions.FileAlreadyExistsException
import com.owncloud.android.domain.exceptions.FileNotFoundException
import com.owncloud.android.domain.files.FileRepository
import com.owncloud.android.domain.files.model.MIME_DIR
Expand Down Expand Up @@ -147,13 +147,29 @@ class OCFileRepository(
}
}

override fun renameFile(oldName: String, oldRemotePath: String, newName: String, isFolder: Boolean) {
override fun renameFile(ocFile: OCFile, newName: String) {
// 1. Compose new remote path
val newRemotePath = localStorageProvider.getExpectedRemotePath(
remotePath = ocFile.remotePath,
newName = newName,
isFolder = ocFile.isFolder
)

// 2. Check if file already exists in database
if (localFileDataSource.getFileByRemotePath(newRemotePath, ocFile.owner) != null) {
throw FileAlreadyExistsException()
}

// 3. Perform remote operation
remoteFileDataSource.renameFile(
oldName = oldName,
oldRemotePath = oldRemotePath,
oldName = ocFile.fileName,
oldRemotePath = ocFile.remotePath,
newName = newName,
isFolder = isFolder
isFolder = ocFile.isFolder
)

// 4. Save new remote path in the local database
localFileDataSource.saveFile(ocFile.copy(remotePath = newRemotePath))
}

override fun saveFile(file: OCFile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* ownCloud Android client application
*
* @author David González Verdugo
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.owncloud.android.domain.exceptions

import java.lang.Exception

class FileAlreadyExistsException : Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ interface FileRepository {
fun moveFile(listOfFilesToMove: List<OCFile>, targetFile: OCFile)
fun refreshFolder(remotePath: String)
fun removeFile(listOfFilesToRemove: List<OCFile>, removeOnlyLocalCopy: Boolean)
fun renameFile(oldName: String, oldRemotePath: String, newName: String, isFolder: Boolean)
fun renameFile(ocFile: OCFile, newName: String)
fun saveFile(file: OCFile)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ class RenameFileUseCase(
if (!fileNameValidator.validate(newNameTrimmed)) throw FileNameException(type = FileNameException.FileNameExceptionType.FILE_NAME_FORBIDDEN_CHARACTERS)

return fileRepository.renameFile(
oldName = params.ocFileToRename.fileName,
oldRemotePath = params.ocFileToRename.remotePath,
ocFile = params.ocFileToRename,
newName = params.newName,
isFolder = params.ocFileToRename.isFolder
)
}

Expand Down

0 comments on commit ea25d2a

Please sign in to comment.