Merge pull request #669 from grecep/master
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / UploadFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 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.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.Set;
29 import java.util.concurrent.atomic.AtomicBoolean;
30
31 import org.apache.commons.httpclient.methods.PutMethod;
32 import org.apache.commons.httpclient.methods.RequestEntity;
33
34 import com.owncloud.android.datamodel.OCFile;
35 import com.owncloud.android.files.services.FileUploader;
36 import com.owncloud.android.lib.common.network.ProgressiveDataTransferer;
37 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
38 import com.owncloud.android.lib.common.OwnCloudClient;
39 import com.owncloud.android.lib.common.operations.OperationCancelledException;
40 import com.owncloud.android.lib.common.operations.RemoteOperation;
41 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
42 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
43 import com.owncloud.android.lib.common.utils.Log_OC;
44 import com.owncloud.android.lib.resources.files.ChunkedUploadRemoteFileOperation;
45 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
46 import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
47 import com.owncloud.android.utils.FileStorageUtils;
48
49 import android.accounts.Account;
50 import android.content.Context;
51
52
53 /**
54 * Remote operation performing the upload of a file to an ownCloud server
55 *
56 * @author David A. Velasco
57 */
58 public class UploadFileOperation extends RemoteOperation {
59
60 private static final String TAG = UploadFileOperation.class.getSimpleName();
61
62 private Account mAccount;
63 private OCFile mFile;
64 private OCFile mOldFile;
65 private String mRemotePath = null;
66 private boolean mChunked = false;
67 private boolean mIsInstant = false;
68 private boolean mRemoteFolderToBeCreated = false;
69 private boolean mForceOverwrite = false;
70 private int mLocalBehaviour = FileUploader.LOCAL_BEHAVIOUR_COPY;
71 private boolean mWasRenamed = false;
72 private String mOriginalFileName = null;
73 private String mOriginalStoragePath = null;
74 PutMethod mPutMethod = null;
75 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
76 private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
77 private Context mContext;
78
79 private UploadRemoteFileOperation mUploadOperation;
80
81 protected RequestEntity mEntity = null;
82
83
84 public UploadFileOperation( Account account,
85 OCFile file,
86 boolean chunked,
87 boolean isInstant,
88 boolean forceOverwrite,
89 int localBehaviour,
90 Context context) {
91 if (account == null)
92 throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation creation");
93 if (file == null)
94 throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation");
95 if (file.getStoragePath() == null || file.getStoragePath().length() <= 0
96 || !(new File(file.getStoragePath()).exists())) {
97 throw new IllegalArgumentException(
98 "Illegal file in UploadFileOperation; storage path invalid or file not found: "
99 + file.getStoragePath());
100 }
101
102 mAccount = account;
103 mFile = file;
104 mRemotePath = file.getRemotePath();
105 mChunked = chunked;
106 mIsInstant = isInstant;
107 mForceOverwrite = forceOverwrite;
108 mLocalBehaviour = localBehaviour;
109 mOriginalStoragePath = mFile.getStoragePath();
110 mOriginalFileName = mFile.getFileName();
111 mContext = context;
112 }
113
114 public Account getAccount() {
115 return mAccount;
116 }
117
118 public String getFileName() {
119 return mOriginalFileName;
120 }
121
122 public OCFile getFile() {
123 return mFile;
124 }
125
126 public OCFile getOldFile() {
127 return mOldFile;
128 }
129
130 public String getOriginalStoragePath() {
131 return mOriginalStoragePath;
132 }
133
134 public String getStoragePath() {
135 return mFile.getStoragePath();
136 }
137
138 public String getRemotePath() {
139 return mFile.getRemotePath();
140 }
141
142 public String getMimeType() {
143 return mFile.getMimetype();
144 }
145
146 public boolean isInstant() {
147 return mIsInstant;
148 }
149
150 public boolean isRemoteFolderToBeCreated() {
151 return mRemoteFolderToBeCreated;
152 }
153
154 public void setRemoteFolderToBeCreated() {
155 mRemoteFolderToBeCreated = true;
156 }
157
158 public boolean getForceOverwrite() {
159 return mForceOverwrite;
160 }
161
162 public boolean wasRenamed() {
163 return mWasRenamed;
164 }
165
166 public Set<OnDatatransferProgressListener> getDataTransferListeners() {
167 return mDataTransferListeners;
168 }
169
170 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
171 synchronized (mDataTransferListeners) {
172 mDataTransferListeners.add(listener);
173 }
174 if (mEntity != null) {
175 ((ProgressiveDataTransferer)mEntity).addDatatransferProgressListener(listener);
176 }
177 }
178
179 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
180 synchronized (mDataTransferListeners) {
181 mDataTransferListeners.remove(listener);
182 }
183 if (mEntity != null) {
184 ((ProgressiveDataTransferer)mEntity).removeDatatransferProgressListener(listener);
185 }
186 }
187
188 @Override
189 protected RemoteOperationResult run(OwnCloudClient client) {
190 RemoteOperationResult result = null;
191 boolean localCopyPassed = false, nameCheckPassed = false;
192 File temporalFile = null, originalFile = new File(mOriginalStoragePath), expectedFile = null;
193 try {
194 // / rename the file to upload, if necessary
195 if (!mForceOverwrite) {
196 String remotePath = getAvailableRemotePath(client, mRemotePath);
197 mWasRenamed = !remotePath.equals(mRemotePath);
198 if (mWasRenamed) {
199 createNewOCFile(remotePath);
200 }
201 }
202 nameCheckPassed = true;
203
204 String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile); // /
205 // not
206 // before
207 // getAvailableRemotePath()
208 // !!!
209 expectedFile = new File(expectedPath);
210
211 // check location of local file; if not the expected, copy to a
212 // temporal file before upload (if COPY is the expected behaviour)
213 if (!mOriginalStoragePath.equals(expectedPath) && mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY) {
214
215 if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) {
216 result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_FULL);
217 return result; // error condition when the file should be
218 // copied
219
220 } else {
221 String temporalPath = FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
222 mFile.setStoragePath(temporalPath);
223 temporalFile = new File(temporalPath);
224 if (!mOriginalStoragePath.equals(temporalPath)) { // preventing
225 // weird
226 // but
227 // possible
228 // situation
229 InputStream in = null;
230 OutputStream out = null;
231 try {
232 File temporalParent = temporalFile.getParentFile();
233 temporalParent.mkdirs();
234 if (!temporalParent.isDirectory()) {
235 throw new IOException("Unexpected error: parent directory could not be created");
236 }
237 temporalFile.createNewFile();
238 if (!temporalFile.isFile()) {
239 throw new IOException("Unexpected error: target file could not be created");
240 }
241 in = new FileInputStream(originalFile);
242 out = new FileOutputStream(temporalFile);
243 byte[] buf = new byte[1024];
244 int len;
245 while ((len = in.read(buf)) > 0) {
246 out.write(buf, 0, len);
247 }
248
249 } catch (Exception e) {
250 result = new RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_COPIED);
251 return result;
252
253 } finally {
254 try {
255 if (in != null)
256 in.close();
257 } catch (Exception e) {
258 Log_OC.d(TAG, "Weird exception while closing input stream for " + mOriginalStoragePath + " (ignoring)", e);
259 }
260 try {
261 if (out != null)
262 out.close();
263 } catch (Exception e) {
264 Log_OC.d(TAG, "Weird exception while closing output stream for " + expectedPath + " (ignoring)", e);
265 }
266 }
267 }
268 }
269 }
270 localCopyPassed = true;
271
272 /// perform the upload
273 if ( mChunked && (new File(mFile.getStoragePath())).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE ) {
274 mUploadOperation = new ChunkedUploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
275 mFile.getMimetype());
276 } else {
277 mUploadOperation = new UploadRemoteFileOperation(mFile.getStoragePath(), mFile.getRemotePath(),
278 mFile.getMimetype());
279 }
280 Iterator <OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
281 while (listener.hasNext()) {
282 mUploadOperation.addDatatransferProgressListener(listener.next());
283 }
284 result = mUploadOperation.execute(client);
285
286 /// move local temporal file or original file to its corresponding
287 // location in the ownCloud local folder
288 if (result.isSuccess()) {
289 if (mLocalBehaviour == FileUploader.LOCAL_BEHAVIOUR_FORGET) {
290 mFile.setStoragePath(null);
291
292 } else {
293 mFile.setStoragePath(expectedPath);
294 File fileToMove = null;
295 if (temporalFile != null) { // FileUploader.LOCAL_BEHAVIOUR_COPY
296 // ; see where temporalFile was
297 // set
298 fileToMove = temporalFile;
299 } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
300 fileToMove = originalFile;
301 }
302 if (!expectedFile.equals(fileToMove)) {
303 File expectedFolder = expectedFile.getParentFile();
304 expectedFolder.mkdirs();
305 if (!expectedFolder.isDirectory() || !fileToMove.renameTo(expectedFile)) {
306 mFile.setStoragePath(null); // forget the local file
307 // by now, treat this as a success; the file was
308 // uploaded; the user won't like that the local file
309 // is not linked, but this should be a very rare
310 // fail;
311 // the best option could be show a warning message
312 // (but not a fail)
313 // result = new
314 // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
315 // return result;
316 }
317 }
318 }
319 }
320
321 } catch (Exception e) {
322 // TODO something cleaner with cancellations
323 if (mCancellationRequested.get()) {
324 result = new RemoteOperationResult(new OperationCancelledException());
325 } else {
326 result = new RemoteOperationResult(e);
327 }
328
329 } finally {
330 if (temporalFile != null && !originalFile.equals(temporalFile)) {
331 temporalFile.delete();
332 }
333 if (result.isSuccess()) {
334 Log_OC.i(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
335 } else {
336 if (result.getException() != null) {
337 String complement = "";
338 if (!nameCheckPassed) {
339 complement = " (while checking file existence in server)";
340 } else if (!localCopyPassed) {
341 complement = " (while copying local file to " + FileStorageUtils.getSavePath(mAccount.name)
342 + ")";
343 }
344 Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage() + complement, result.getException());
345 } else {
346 Log_OC.e(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " + result.getLogMessage());
347 }
348 }
349 }
350
351 return result;
352 }
353
354 private void createNewOCFile(String newRemotePath) {
355 // a new OCFile instance must be created for a new remote path
356 OCFile newFile = new OCFile(newRemotePath);
357 newFile.setCreationTimestamp(mFile.getCreationTimestamp());
358 newFile.setFileLength(mFile.getFileLength());
359 newFile.setMimetype(mFile.getMimetype());
360 newFile.setModificationTimestamp(mFile.getModificationTimestamp());
361 newFile.setModificationTimestampAtLastSyncForData(mFile.getModificationTimestampAtLastSyncForData());
362 // newFile.setEtag(mFile.getEtag())
363 newFile.setKeepInSync(mFile.keepInSync());
364 newFile.setLastSyncDateForProperties(mFile.getLastSyncDateForProperties());
365 newFile.setLastSyncDateForData(mFile.getLastSyncDateForData());
366 newFile.setStoragePath(mFile.getStoragePath());
367 newFile.setParentId(mFile.getParentId());
368 mOldFile = mFile;
369 mFile = newFile;
370 }
371
372 /**
373 * Checks if remotePath does not exist in the server and returns it, or adds
374 * a suffix to it in order to avoid the server file is overwritten.
375 *
376 * @param string
377 * @return
378 */
379 private String getAvailableRemotePath(OwnCloudClient wc, String remotePath) throws Exception {
380 boolean check = existsFile(wc, remotePath);
381 if (!check) {
382 return remotePath;
383 }
384
385 int pos = remotePath.lastIndexOf(".");
386 String suffix = "";
387 String extension = "";
388 if (pos >= 0) {
389 extension = remotePath.substring(pos + 1);
390 remotePath = remotePath.substring(0, pos);
391 }
392 int count = 2;
393 do {
394 suffix = " (" + count + ")";
395 if (pos >= 0) {
396 check = existsFile(wc, remotePath + suffix + "." + extension);
397 }
398 else {
399 check = existsFile(wc, remotePath + suffix);
400 }
401 count++;
402 } while (check);
403
404 if (pos >= 0) {
405 return remotePath + suffix + "." + extension;
406 } else {
407 return remotePath + suffix;
408 }
409 }
410
411 private boolean existsFile(OwnCloudClient client, String remotePath){
412 ExistenceCheckRemoteOperation existsOperation = new ExistenceCheckRemoteOperation(remotePath, mContext, false);
413 RemoteOperationResult result = existsOperation.execute(client);
414 return result.isSuccess();
415 }
416
417 public void cancel() {
418 mUploadOperation.cancel();
419 }
420
421 }