Add RenameOperation to OperationsService
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / RenameFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import com.owncloud.android.datamodel.OCFile;
24 import com.owncloud.android.lib.common.OwnCloudClient;
25 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
26 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
27 import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation;
28 import com.owncloud.android.operations.common.SyncOperation;
29 import com.owncloud.android.utils.FileStorageUtils;
30 import com.owncloud.android.utils.Log_OC;
31
32 import android.accounts.Account;
33
34
35 /**
36 * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
37 *
38 * @author David A. Velasco
39 * @author masensio
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 Account mAccount;
48 private String mNewName;
49 private String mNewRemotePath;
50
51
52
53 /**
54 * Constructor
55 *
56 * @param remotePath RemotePath of the OCFile instance describing the remote file or folder to rename
57 * @param account OwnCloud account containing the remote file
58 * @param newName New name to set as the name of file.
59 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
60 */
61 public RenameFileOperation(String remotePath, Account account, String newName) {
62 mRemotePath = remotePath;
63 mAccount = account;
64 mNewName = newName;
65 mNewRemotePath = null;
66 }
67
68 public OCFile getFile() {
69 return mFile;
70 }
71
72
73 /**
74 * Performs the rename operation.
75 *
76 * @param client Client object to communicate with the remote ownCloud server.
77 */
78 @Override
79 protected RemoteOperationResult run(OwnCloudClient client) {
80 RemoteOperationResult result = null;
81
82 mFile = getStorageManager().getFileByPath(mRemotePath);
83
84 // check if the new name is valid in the local file system
85 try {
86 if (!isValidNewName()) {
87 return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
88 }
89 String parent = (new File(mFile.getRemotePath())).getParent();
90 parent = (parent.endsWith(OCFile.PATH_SEPARATOR)) ? parent : parent + OCFile.PATH_SEPARATOR;
91 mNewRemotePath = parent + mNewName;
92 if (mFile.isFolder()) {
93 mNewRemotePath += OCFile.PATH_SEPARATOR;
94 }
95
96 // check local overwrite
97 if (getStorageManager().getFileByPath(mNewRemotePath) != null) {
98 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
99 }
100
101 RenameRemoteFileOperation operation = new RenameRemoteFileOperation(mFile.getFileName(), mFile.getRemotePath(),
102 mNewName, mFile.isFolder());
103 result = operation.execute(client);
104
105 if (result.isSuccess()) {
106 if (mFile.isFolder()) {
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) ? mNewName : mNewRemotePath) + ": " +
116 ((result!= null) ? result.getLogMessage() : ""), e);
117 }
118
119 return result;
120 }
121
122
123 private void saveLocalDirectory() {
124 getStorageManager().moveFolder(mFile, mNewRemotePath);
125 String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
126 File localDir = new File(localPath);
127 if (localDir.exists()) {
128 localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewRemotePath));
129 // TODO - if renameTo fails, children files that are already down will result unlinked
130 }
131 }
132
133 private void saveLocalFile() {
134 mFile.setFileName(mNewName);
135
136 // try to rename the local copy of the file
137 if (mFile.isDown()) {
138 File f = new File(mFile.getStoragePath());
139 String parentStoragePath = f.getParent();
140 if (!parentStoragePath.endsWith(File.separator))
141 parentStoragePath += File.separator;
142 if (f.renameTo(new File(parentStoragePath + mNewName))) {
143 mFile.setStoragePath(parentStoragePath + mNewName);
144 }
145 // else - NOTHING: the link to the local file is kept although the local name can't be updated
146 // TODO - study conditions when this could be a problem
147 }
148
149 getStorageManager().saveFile(mFile);
150 }
151
152 /**
153 * Checks if the new name to set is valid in the file system
154 *
155 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
156 * for downloads, out of any account, and then removed.
157 *
158 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
159 * could be formatted with a different file system.
160 *
161 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
162 *
163 * @return 'True' if a temporal file named with the name to set could be created in the file system where
164 * local files are stored.
165 * @throws IOException When the temporal folder can not be created.
166 */
167 private boolean isValidNewName() throws IOException {
168 // check tricky names
169 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator) || mNewName.contains("%")) {
170 return false;
171 }
172 // create a test file
173 String tmpFolderName = FileStorageUtils.getTemporalPath("");
174 File testFile = new File(tmpFolderName + mNewName);
175 File tmpFolder = testFile.getParentFile();
176 tmpFolder.mkdirs();
177 if (!tmpFolder.isDirectory()) {
178 throw new IOException("Unexpected error: temporal directory could not be created");
179 }
180 try {
181 testFile.createNewFile(); // return value is ignored; it could be 'false' because 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, but repeat and repeat...
189 testFile.delete();
190
191 return result;
192 }
193
194 }