Added MoveFileOperation to move OCFiles
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / MoveFileOperation.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 * Operation mmoving an {@link OCFile} to a different folder.
37 *
38 * @author David A. Velasco
39 */
40 public class MoveFileOperation extends SyncOperation {
41
42 private static final String TAG = MoveFileOperation.class.getSimpleName();
43
44 private String mPath;
45 private Account mAccount;
46 private String mNewParentPath;
47 private OCFile mFile;
48
49
50
51 /**
52 * Constructor
53 *
54 * @param path Remote path of the {@link OCFile} to move.
55 * @param newParentPath Path to the folder where the file will be moved into.
56 * @param account OwnCloud account containing both the file and the target folder
57 */
58 public MoveFileOperation(String path, String newParentPath, Account account) {
59 mPath = path;
60 mNewParentPath = newParentPath;
61 mAccount = account;
62 mFile = null;
63 }
64
65 public OCFile getFile() {
66 return mFile;
67 }
68
69
70 /**
71 * Performs the 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(mPath);
80
81 // check if the new name is valid in the local file system
82 try {
83 String parentPath = (new File(mFile.getRemotePath())).getParent();
84 parentPath = (parentPath.endsWith(OCFile.PATH_SEPARATOR))
85 ? parentPath
86 : parentPath + OCFile.PATH_SEPARATOR;
87
88 String mNewPath = mNewParentPath + mFile.getFileName();
89 if (mFile.isFolder() && !mNewPath.endsWith(OCFile.PATH_SEPARATOR)) {
90 mNewPath += OCFile.PATH_SEPARATOR;
91 }
92
93 // check local overwrite
94 if (getStorageManager().getFileByPath(mPath) != null) {
95 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
96 }
97
98 MoveRemoteFileOperation operation = new MoveRemoteFileOperation(
99 mFile.getRemotePath(),
100 mNewPath,
101 mFile.isFolder()
102 );
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, "Move " + mFile.getRemotePath() + " to " +
116 (mNewParentPath==null) + ": " +
117 ((result!= null) ? result.getLogMessage() : ""), e);
118 }
119
120 return result;
121 }
122
123
124 private void saveLocalDirectory() {
125 getStorageManager().moveFolder(mFile, mNewParentPath);
126 String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
127 File localDir = new File(localPath);
128 if (localDir.exists()) {
129 localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewParentPath));
130 // TODO - if renameTo fails, children files that are already down will result unlinked
131 }
132 }
133
134 private void saveLocalFile() {
135 mFile.setFileName(mNewName);
136
137 // try to rename the local copy of the file
138 if (mFile.isDown()) {
139 File f = new File(mFile.getStoragePath());
140 String parentStoragePath = f.getParent();
141 if (!parentStoragePath.endsWith(File.separator))
142 parentStoragePath += File.separator;
143 if (f.renameTo(new File(parentStoragePath + mNewName))) {
144 mFile.setStoragePath(parentStoragePath + mNewName);
145 }
146 // else - NOTHING: the link to the local file is kept although the local name can't be updated
147 // TODO - study conditions when this could be a problem
148 }
149
150 getStorageManager().saveFile(mFile);
151 }
152
153 /**
154 * Checks if the new name to set is valid in the file system
155 *
156 * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
157 * for downloads, out of any account, and then removed.
158 *
159 * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
160 * could be formatted with a different file system.
161 *
162 * TODO move this method, and maybe FileDownload.get***Path(), to a class with utilities specific for the interactions with the file system
163 *
164 * @return 'True' if a temporal file named with the name to set could be created in the file system where
165 * local files are stored.
166 * @throws IOException When the temporal folder can not be created.
167 */
168 private boolean isValidNewName() throws IOException {
169 // check tricky names
170 if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator) || mNewName.contains("%")) {
171 return false;
172 }
173 // create a test file
174 String tmpFolderName = FileStorageUtils.getTemporalPath("");
175 File testFile = new File(tmpFolderName + mNewName);
176 File tmpFolder = testFile.getParentFile();
177 tmpFolder.mkdirs();
178 if (!tmpFolder.isDirectory()) {
179 throw new IOException("Unexpected error: temporal directory could not be created");
180 }
181 try {
182 testFile.createNewFile(); // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
183 } catch (IOException e) {
184 Log_OC.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
185 return false;
186 }
187 boolean result = (testFile.exists() && testFile.isFile());
188
189 // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
190 testFile.delete();
191
192 return result;
193 }
194
195 }