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