2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.operations
;
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
;
32 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
34 import org
.apache
.commons
.httpclient
.methods
.PutMethod
;
35 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
37 import android
.accounts
.Account
;
38 import android
.content
.Context
;
39 import android
.net
.Uri
;
41 import com
.owncloud
.android
.MainApp
;
42 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
43 import com
.owncloud
.android
.datamodel
.OCFile
;
44 import com
.owncloud
.android
.files
.services
.FileUploader
;
45 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
46 import com
.owncloud
.android
.lib
.common
.network
.OnDatatransferProgressListener
;
47 import com
.owncloud
.android
.lib
.common
.network
.ProgressiveDataTransferer
;
48 import com
.owncloud
.android
.lib
.common
.operations
.OperationCancelledException
;
49 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
50 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
51 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
52 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
53 import com
.owncloud
.android
.lib
.resources
.files
.ChunkedUploadRemoteFileOperation
;
54 import com
.owncloud
.android
.lib
.resources
.files
.ExistenceCheckRemoteOperation
;
55 import com
.owncloud
.android
.lib
.resources
.files
.UploadRemoteFileOperation
;
56 import com
.owncloud
.android
.utils
.FileStorageUtils
;
57 import com
.owncloud
.android
.utils
.UriUtils
;
61 * Remote operation performing the upload of a file to an ownCloud server
63 public class UploadFileOperation
extends RemoteOperation
{
65 private static final String TAG
= UploadFileOperation
.class.getSimpleName();
67 private Account mAccount
;
69 private OCFile mOldFile
;
70 private String mRemotePath
= null
;
71 private boolean mChunked
= false
;
72 private boolean mIsInstant
= false
;
73 private boolean mRemoteFolderToBeCreated
= false
;
74 private boolean mForceOverwrite
= false
;
75 private int mLocalBehaviour
= FileUploader
.LOCAL_BEHAVIOUR_COPY
;
76 private boolean mWasRenamed
= false
;
77 private String mOriginalFileName
= null
;
78 private String mOriginalStoragePath
= null
;
79 PutMethod mPutMethod
= null
;
80 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
81 private AtomicBoolean mCancellationRequested
= new AtomicBoolean(false
);
82 private Context mContext
;
84 private UploadRemoteFileOperation mUploadOperation
;
86 protected RequestEntity mEntity
= null
;
89 public UploadFileOperation( Account account
,
93 boolean forceOverwrite
,
97 throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation " +
100 throw new IllegalArgumentException("Illegal NULL file in UploadFileOperation creation");
101 if (file
.getStoragePath() == null
|| file
.getStoragePath().length() <= 0) {
102 throw new IllegalArgumentException(
103 "Illegal file in UploadFileOperation; storage path invalid: "
104 + file
.getStoragePath());
109 mRemotePath
= file
.getRemotePath();
111 mIsInstant
= isInstant
;
112 mForceOverwrite
= forceOverwrite
;
113 mLocalBehaviour
= localBehaviour
;
114 mOriginalStoragePath
= mFile
.getStoragePath();
115 mOriginalFileName
= mFile
.getFileName();
119 public Account
getAccount() {
123 public String
getFileName() {
124 return mOriginalFileName
;
127 public OCFile
getFile() {
131 public OCFile
getOldFile() {
135 public String
getOriginalStoragePath() {
136 return mOriginalStoragePath
;
139 public String
getStoragePath() {
140 return mFile
.getStoragePath();
143 public String
getRemotePath() {
144 return mFile
.getRemotePath();
147 public String
getMimeType() {
148 return mFile
.getMimetype();
151 public boolean isInstant() {
155 public boolean isRemoteFolderToBeCreated() {
156 return mRemoteFolderToBeCreated
;
159 public void setRemoteFolderToBeCreated() {
160 mRemoteFolderToBeCreated
= true
;
163 public boolean getForceOverwrite() {
164 return mForceOverwrite
;
167 public boolean wasRenamed() {
171 public Set
<OnDatatransferProgressListener
> getDataTransferListeners() {
172 return mDataTransferListeners
;
175 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
176 synchronized (mDataTransferListeners
) {
177 mDataTransferListeners
.add(listener
);
179 if (mEntity
!= null
) {
180 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListener(listener
);
184 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
185 synchronized (mDataTransferListeners
) {
186 mDataTransferListeners
.remove(listener
);
188 if (mEntity
!= null
) {
189 ((ProgressiveDataTransferer
)mEntity
).removeDatatransferProgressListener(listener
);
194 protected RemoteOperationResult
run(OwnCloudClient client
) {
195 RemoteOperationResult result
= null
;
196 boolean localCopyPassed
= false
, nameCheckPassed
= false
;
197 File temporalFile
= null
, originalFile
= new File(mOriginalStoragePath
), expectedFile
= null
;
199 // / rename the file to upload, if necessary
200 if (!mForceOverwrite
) {
201 String remotePath
= getAvailableRemotePath(client
, mRemotePath
);
202 mWasRenamed
= !remotePath
.equals(mRemotePath
);
204 createNewOCFile(remotePath
);
207 nameCheckPassed
= true
;
209 String expectedPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
); // /
212 // getAvailableRemotePath()
214 expectedFile
= new File(expectedPath
);
216 // check location of local file; if not the expected, copy to a
217 // temporal file before upload (if COPY is the expected behaviour)
218 if (!mOriginalStoragePath
.equals(expectedPath
) &&
219 mLocalBehaviour
== FileUploader
.LOCAL_BEHAVIOUR_COPY
) {
221 if (FileStorageUtils
.getUsableSpace(mAccount
.name
) < originalFile
.length()) {
222 result
= new RemoteOperationResult(ResultCode
.LOCAL_STORAGE_FULL
);
223 return result
; // error condition when the file should be
228 String temporalPath
= FileStorageUtils
.getTemporalPath(mAccount
.name
) +
229 mFile
.getRemotePath();
230 mFile
.setStoragePath(temporalPath
);
231 temporalFile
= new File(temporalPath
);
233 File temporalParent
= temporalFile
.getParentFile();
234 temporalParent
.mkdirs();
235 if (!temporalParent
.isDirectory()) {
236 throw new IOException("Unexpected error: parent directory could not be created");
238 temporalFile
.createNewFile();
239 if (!temporalFile
.isFile()) {
240 throw new IOException("Unexpected error: target file could not be created");
243 InputStream
in = null
;
244 OutputStream out
= null
;
248 // In case document provider schema as 'content://'
249 if (mOriginalStoragePath
.startsWith(UriUtils
.URI_CONTENT_SCHEME
)) {
251 Uri uri
= Uri
.parse(mOriginalStoragePath
);
253 in = MainApp
.getAppContext().getContentResolver().openInputStream(uri
);
254 out
= new FileOutputStream(temporalFile
);
257 byte[] data
= new byte[16384];
259 while (!mCancellationRequested
.get() &&
260 (nRead
= in.read(data
, 0, data
.length
)) != -1) {
261 out
.write(data
, 0, nRead
);
266 if (!mOriginalStoragePath
.equals(temporalPath
)) { // preventing
272 in = new FileInputStream(originalFile
);
273 out
= new FileOutputStream(temporalFile
);
274 byte[] buf
= new byte[1024];
276 while (!mCancellationRequested
.get() && (len
= in.read(buf
)) > 0) {
277 out
.write(buf
, 0, len
);
282 if (mCancellationRequested
.get()) {
283 result
= new RemoteOperationResult(new OperationCancelledException());
287 } catch (Exception e
) {
288 result
= new RemoteOperationResult(ResultCode
.LOCAL_STORAGE_NOT_COPIED
);
295 } catch (Exception e
) {
296 Log_OC
.d(TAG
, "Weird exception while closing input stream for " +
297 mOriginalStoragePath
+ " (ignoring)", e
);
302 } catch (Exception e
) {
303 Log_OC
.d(TAG
, "Weird exception while closing output stream for " +
304 expectedPath
+ " (ignoring)", e
);
309 localCopyPassed
= (result
== null
);
311 /// perform the upload
313 (new File(mFile
.getStoragePath())).length() >
314 ChunkedUploadRemoteFileOperation
.CHUNK_SIZE
) {
315 mUploadOperation
= new ChunkedUploadRemoteFileOperation(mFile
.getStoragePath(),
316 mFile
.getRemotePath(), mFile
.getMimetype());
318 mUploadOperation
= new UploadRemoteFileOperation(mFile
.getStoragePath(),
319 mFile
.getRemotePath(), mFile
.getMimetype());
321 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
322 while (listener
.hasNext()) {
323 mUploadOperation
.addDatatransferProgressListener(listener
.next());
325 if (!mCancellationRequested
.get()) {
326 result
= mUploadOperation
.execute(client
);
328 /// move local temporal file or original file to its corresponding
329 // location in the ownCloud local folder
330 if (result
.isSuccess()) {
331 if (mLocalBehaviour
== FileUploader
.LOCAL_BEHAVIOUR_FORGET
) {
332 mFile
.setStoragePath(null
);
333 } else if (mLocalBehaviour
== FileUploader
.LOCAL_BEHAVIOUR_REMOVE
){
334 mFile
.setStoragePath(null
);
335 originalFile
.delete();
337 mFile
.setStoragePath(expectedPath
);
338 File fileToMove
= null
;
339 if (temporalFile
!= null
) { // FileUploader.LOCAL_BEHAVIOUR_COPY
340 // ; see where temporalFile was
342 fileToMove
= temporalFile
;
343 } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
344 fileToMove
= originalFile
;
346 if (!expectedFile
.equals(fileToMove
)) {
347 File expectedFolder
= expectedFile
.getParentFile();
348 expectedFolder
.mkdirs();
349 if (!expectedFolder
.isDirectory() || !fileToMove
.renameTo(expectedFile
)) {
350 mFile
.setStoragePath(null
); // forget the local file
351 // by now, treat this as a success; the file was
352 // uploaded; the user won't like that the local file
353 // is not linked, but this should be a very rare
355 // the best option could be show a warning message
358 // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
363 FileDataStorageManager
.triggerMediaScan(originalFile
.getAbsolutePath());
364 FileDataStorageManager
.triggerMediaScan(expectedFile
.getAbsolutePath());
368 } catch (Exception e
) {
369 // TODO something cleaner with cancellations
370 if (mCancellationRequested
.get()) {
371 result
= new RemoteOperationResult(new OperationCancelledException());
373 result
= new RemoteOperationResult(e
);
377 if (temporalFile
!= null
&& !originalFile
.equals(temporalFile
)) {
378 temporalFile
.delete();
380 if (result
.isSuccess()) {
381 Log_OC
.i(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+ ": " +
382 result
.getLogMessage());
384 if (result
.getException() != null
) {
385 String complement
= "";
386 if (!nameCheckPassed
) {
387 complement
= " (while checking file existence in server)";
388 } else if (!localCopyPassed
) {
389 complement
= " (while copying local file to " +
390 FileStorageUtils
.getSavePath(mAccount
.name
)
393 Log_OC
.e(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+
394 ": " + result
.getLogMessage() + complement
, result
.getException());
396 Log_OC
.e(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+
397 ": " + result
.getLogMessage());
405 private void createNewOCFile(String newRemotePath
) {
406 // a new OCFile instance must be created for a new remote path
407 OCFile newFile
= new OCFile(newRemotePath
);
408 newFile
.setCreationTimestamp(mFile
.getCreationTimestamp());
409 newFile
.setFileLength(mFile
.getFileLength());
410 newFile
.setMimetype(mFile
.getMimetype());
411 newFile
.setModificationTimestamp(mFile
.getModificationTimestamp());
412 newFile
.setModificationTimestampAtLastSyncForData(
413 mFile
.getModificationTimestampAtLastSyncForData());
414 // newFile.setEtag(mFile.getEtag())
415 newFile
.setFavorite(mFile
.isFavorite());
416 newFile
.setLastSyncDateForProperties(mFile
.getLastSyncDateForProperties());
417 newFile
.setLastSyncDateForData(mFile
.getLastSyncDateForData());
418 newFile
.setStoragePath(mFile
.getStoragePath());
419 newFile
.setParentId(mFile
.getParentId());
425 * Checks if remotePath does not exist in the server and returns it, or adds
426 * a suffix to it in order to avoid the server file is overwritten.
432 private String
getAvailableRemotePath(OwnCloudClient wc
, String remotePath
) throws Exception
{
433 boolean check
= existsFile(wc
, remotePath
);
438 int pos
= remotePath
.lastIndexOf(".");
440 String extension
= "";
442 extension
= remotePath
.substring(pos
+ 1);
443 remotePath
= remotePath
.substring(0, pos
);
447 suffix
= " (" + count
+ ")";
449 check
= existsFile(wc
, remotePath
+ suffix
+ "." + extension
);
452 check
= existsFile(wc
, remotePath
+ suffix
);
458 return remotePath
+ suffix
+ "." + extension
;
460 return remotePath
+ suffix
;
464 private boolean existsFile(OwnCloudClient client
, String remotePath
){
465 ExistenceCheckRemoteOperation existsOperation
=
466 new ExistenceCheckRemoteOperation(remotePath
, mContext
, false
);
467 RemoteOperationResult result
= existsOperation
.execute(client
);
468 return result
.isSuccess();
471 public void cancel() {
472 mCancellationRequested
= new AtomicBoolean(true
);
473 if (mUploadOperation
!= null
) {
474 mUploadOperation
.cancel();