2 * ownCloud Android client application
4 * @author David A. Velasco
6 * Copyright (C) 2015 ownCloud Inc.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 package com
.owncloud
.android
.operations
;
25 import java
.io
.IOException
;
27 import com
.owncloud
.android
.MainApp
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
30 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
32 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
33 import com
.owncloud
.android
.lib
.resources
.files
.RenameRemoteFileOperation
;
34 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
35 import com
.owncloud
.android
.utils
.FileStorageUtils
;
39 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
41 public class RenameFileOperation
extends SyncOperation
{
43 private static final String TAG
= RenameFileOperation
.class.getSimpleName();
46 private String mRemotePath
;
47 private String mNewName
;
48 private String mNewRemotePath
;
55 * @param remotePath RemotePath of the OCFile instance describing the remote file or
57 * @param newName New name to set as the name of file.
59 public RenameFileOperation(String remotePath
, String newName
) {
60 mRemotePath
= remotePath
;
62 mNewRemotePath
= null
;
65 public OCFile
getFile() {
71 * Performs the rename operation.
73 * @param client Client object to communicate with the remote ownCloud server.
76 protected RemoteOperationResult
run(OwnCloudClient client
) {
77 RemoteOperationResult result
= null
;
79 mFile
= getStorageManager().getFileByPath(mRemotePath
);
81 // check if the new name is valid in the local file system
83 if (!isValidNewName()) {
84 return new RemoteOperationResult(ResultCode
.INVALID_LOCAL_FILE_NAME
);
86 String parent
= (new File(mFile
.getRemotePath())).getParent();
87 parent
= (parent
.endsWith(OCFile
.PATH_SEPARATOR
)) ? parent
: parent
+
88 OCFile
.PATH_SEPARATOR
;
89 mNewRemotePath
= parent
+ mNewName
;
90 if (mFile
.isFolder()) {
91 mNewRemotePath
+= OCFile
.PATH_SEPARATOR
;
94 // check local overwrite
95 if (getStorageManager().getFileByPath(mNewRemotePath
) != null
) {
96 return new RemoteOperationResult(ResultCode
.INVALID_OVERWRITE
);
99 RenameRemoteFileOperation operation
= new RenameRemoteFileOperation(mFile
.getFileName(),
100 mFile
.getRemotePath(),
101 mNewName
, mFile
.isFolder());
102 result
= operation
.execute(client
);
104 if (result
.isSuccess()) {
105 if (mFile
.isFolder()) {
106 getStorageManager().moveLocalFile(mFile
, mNewRemotePath
, parent
);
107 //saveLocalDirectory();
114 } catch (IOException e
) {
115 Log_OC
.e(TAG
, "Rename " + mFile
.getRemotePath() + " to " + ((mNewRemotePath
==null
) ?
116 mNewName
: mNewRemotePath
) + ": " +
117 ((result
!= null
) ? result
.getLogMessage() : ""), e
);
123 private void saveLocalFile() {
124 mFile
.setFileName(mNewName
);
126 // try to rename the local copy of the file
127 if (mFile
.isDown()) {
128 String oldPath
= mFile
.getStoragePath();
129 File f
= new File(oldPath
);
130 String parentStoragePath
= f
.getParent();
131 if (!parentStoragePath
.endsWith(File
.separator
))
132 parentStoragePath
+= File
.separator
;
133 if (f
.renameTo(new File(parentStoragePath
+ mNewName
))) {
134 String newPath
= parentStoragePath
+ mNewName
;
135 mFile
.setStoragePath(newPath
);
137 // notify MediaScanner about removed file
138 getStorageManager().deleteFileInMediaScan(oldPath
);
139 // notify to scan about new file
140 getStorageManager().triggerMediaScan(newPath
);
142 // else - NOTHING: the link to the local file is kept although the local name
144 // TODO - study conditions when this could be a problem
147 getStorageManager().saveFile(mFile
);
151 * Checks if the new name to set is valid in the file system
153 * The only way to be sure is trying to create a file with that name. It's made in the
154 * temporal directory for downloads, out of any account, and then removed.
156 * IMPORTANT: The test must be made in the same file system where files are download.
157 * The internal storage could be formatted with a different file system.
159 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities
160 * specific for the interactions with the file system
162 * @return 'True' if a temporal file named with the name to set could be
163 * created in the file system where local files are stored.
164 * @throws IOException When the temporal folder can not be created.
166 private boolean isValidNewName() throws IOException
{
167 // check tricky names
168 if (mNewName
== null
|| mNewName
.length() <= 0 || mNewName
.contains(File
.separator
)) {
171 // create a test file
172 String tmpFolderName
= FileStorageUtils
.getTemporalPath("");
173 File testFile
= new File(tmpFolderName
+ mNewName
);
174 File tmpFolder
= testFile
.getParentFile();
176 if (!tmpFolder
.isDirectory()) {
177 throw new IOException("Unexpected error: temporal directory could not be created");
180 testFile
.createNewFile(); // return value is ignored; it could be 'false' because
181 // the file already existed, that doesn't invalidate the name
182 } catch (IOException e
) {
183 Log_OC
.i(TAG
, "Test for validity of name " + mNewName
+ " in the file system failed");
186 boolean result
= (testFile
.exists() && testFile
.isFile());
188 // cleaning ; result is ignored, since there is not much we could do in case of failure,
189 // but repeat and repeat...