Merge branch 'master' of https://github.com/owncloud/android into material_buttons
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RenameFileOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * @author masensio
6 * Copyright (C) 2015 ownCloud Inc.
7 *
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.
11 *
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.
16 *
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/>.
19 *
20 */
21
22 package com.owncloud.android.operations;
23
24 import java.io.File;
25 import java.io.IOException;
26
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;
36
37
38 /**
39 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
40 */
41 public class RenameFileOperation extends SyncOperation {
42
43 private static final String TAG = RenameFileOperation.class.getSimpleName();
44
45 private OCFile mFile;
46 private String mRemotePath;
47 private String mNewName;
48 private String mNewRemotePath;
49
50
51
52 /**
53 * Constructor
54 *
55 * @param remotePath RemotePath of the OCFile instance describing the remote file or
56 * folder to rename
57 * @param newName New name to set as the name of file.
58 */
59 public RenameFileOperation(String remotePath, String newName) {
60 mRemotePath = remotePath;
61 mNewName = newName;
62 mNewRemotePath = null;
63 }
64
65 public OCFile getFile() {
66 return mFile;
67 }
68
69
70 /**
71 * Performs the rename operation.
72 *
73 * @param client Client object to communicate with the remote ownCloud server.
74 */
75 @Override
76 protected RemoteOperationResult run(OwnCloudClient client) {
77 RemoteOperationResult result = null;
78
79 mFile = getStorageManager().getFileByPath(mRemotePath);
80
81 // check if the new name is valid in the local file system
82 try {
83 if (!isValidNewName()) {
84 return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
85 }
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;
92 }
93
94 // check local overwrite
95 if (getStorageManager().getFileByPath(mNewRemotePath) != null) {
96 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
97 }
98
99 RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(),
100 mFile.getRemotePath(),
101 mNewName, mFile.isFolder());
102 result = operation.execute(client);
103
104 if (result.isSuccess()) {
105 if (mFile.isFolder()) {
106 getStorageManager().moveLocalFile(mFile, mNewRemotePath, parent);
107 //saveLocalDirectory();
108
109 } else {
110 saveLocalFile();
111 }
112 }
113
114 } catch (IOException e) {
115 Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ?
116 mNewName : mNewRemotePath) + ": " +
117 ((result!= null) ? result.getLogMessage() : ""), e);
118 }
119
120 return result;
121 }
122
123 private void saveLocalFile() {
124 mFile.setFileName(mNewName);
125
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);
136
137 // notify MediaScanner about removed file
138 getStorageManager().deleteFileInMediaScan(oldPath);
139 // notify to scan about new file
140 getStorageManager().triggerMediaScan(newPath);
141 }
142 // else - NOTHING: the link to the local file is kept although the local name
143 // can't be updated
144 // TODO - study conditions when this could be a problem
145 }
146
147 getStorageManager().saveFile(mFile);
148 }
149
150 /**
151 * Checks if the new name to set is valid in the file system
152 *
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.
155 *
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.
158 *
159 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities
160 * specific for the interactions with the file system
161 *
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.
165 */
166 private boolean isValidNewName() throws IOException {
167 // check tricky names
168 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator)) {
169 return false;
170 }
171 // create a test file
172 String tmpFolderName = FileStorageUtils.getTemporalPath("");
173 File testFile = new File(tmpFolderName + mNewName);
174 File tmpFolder = testFile.getParentFile();
175 tmpFolder.mkdirs();
176 if (!tmpFolder.isDirectory()) {
177 throw new IOException("Unexpected error: temporal directory could not be created");
178 }
179 try {
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");
184 return false;
185 }
186 boolean result = (testFile.exists() && testFile.isFile());
187
188 // cleaning ; result is ignored, since there is not much we could do in case of failure,
189 // but repeat and repeat...
190 testFile.delete();
191
192 return result;
193 }
194
195 }