1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.operations
;
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
;
29 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
31 import org
.apache
.commons
.httpclient
.methods
.PutMethod
;
32 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
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
;
49 import android
.accounts
.Account
;
50 import android
.content
.Context
;
54 * Remote operation performing the upload of a file to an ownCloud server
56 * @author David A. Velasco
58 public class UploadFileOperation
extends RemoteOperation
{
60 private static final String TAG
= UploadFileOperation
.class.getSimpleName();
62 private Account mAccount
;
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
;
79 private UploadRemoteFileOperation mUploadOperation
;
81 protected RequestEntity mEntity
= null
;
84 public UploadFileOperation( Account account
,
88 boolean forceOverwrite
,
92 throw new IllegalArgumentException("Illegal NULL account in UploadFileOperation creation");
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());
104 mRemotePath
= file
.getRemotePath();
106 mIsInstant
= isInstant
;
107 mForceOverwrite
= forceOverwrite
;
108 mLocalBehaviour
= localBehaviour
;
109 mOriginalStoragePath
= mFile
.getStoragePath();
110 mOriginalFileName
= mFile
.getFileName();
114 public Account
getAccount() {
118 public String
getFileName() {
119 return mOriginalFileName
;
122 public OCFile
getFile() {
126 public OCFile
getOldFile() {
130 public String
getOriginalStoragePath() {
131 return mOriginalStoragePath
;
134 public String
getStoragePath() {
135 return mFile
.getStoragePath();
138 public String
getRemotePath() {
139 return mFile
.getRemotePath();
142 public String
getMimeType() {
143 return mFile
.getMimetype();
146 public boolean isInstant() {
150 public boolean isRemoteFolderToBeCreated() {
151 return mRemoteFolderToBeCreated
;
154 public void setRemoteFolderToBeCreated() {
155 mRemoteFolderToBeCreated
= true
;
158 public boolean getForceOverwrite() {
159 return mForceOverwrite
;
162 public boolean wasRenamed() {
166 public Set
<OnDatatransferProgressListener
> getDataTransferListeners() {
167 return mDataTransferListeners
;
170 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
171 synchronized (mDataTransferListeners
) {
172 mDataTransferListeners
.add(listener
);
174 if (mEntity
!= null
) {
175 ((ProgressiveDataTransferer
)mEntity
).addDatatransferProgressListener(listener
);
179 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
180 synchronized (mDataTransferListeners
) {
181 mDataTransferListeners
.remove(listener
);
183 if (mEntity
!= null
) {
184 ((ProgressiveDataTransferer
)mEntity
).removeDatatransferProgressListener(listener
);
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
;
194 // / rename the file to upload, if necessary
195 if (!mForceOverwrite
) {
196 String remotePath
= getAvailableRemotePath(client
, mRemotePath
);
197 mWasRenamed
= !remotePath
.equals(mRemotePath
);
199 createNewOCFile(remotePath
);
202 nameCheckPassed
= true
;
204 String expectedPath
= FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
); // /
207 // getAvailableRemotePath()
209 expectedFile
= new File(expectedPath
);
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
) {
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
221 String temporalPath
= FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
222 mFile
.setStoragePath(temporalPath
);
223 temporalFile
= new File(temporalPath
);
224 if (!mOriginalStoragePath
.equals(temporalPath
)) { // preventing
229 InputStream
in = null
;
230 OutputStream out
= null
;
232 File temporalParent
= temporalFile
.getParentFile();
233 temporalParent
.mkdirs();
234 if (!temporalParent
.isDirectory()) {
235 throw new IOException("Unexpected error: parent directory could not be created");
237 temporalFile
.createNewFile();
238 if (!temporalFile
.isFile()) {
239 throw new IOException("Unexpected error: target file could not be created");
241 in = new FileInputStream(originalFile
);
242 out
= new FileOutputStream(temporalFile
);
243 byte[] buf
= new byte[1024];
245 while ((len
= in.read(buf
)) > 0) {
246 out
.write(buf
, 0, len
);
249 } catch (Exception e
) {
250 result
= new RemoteOperationResult(ResultCode
.LOCAL_STORAGE_NOT_COPIED
);
257 } catch (Exception e
) {
258 Log_OC
.d(TAG
, "Weird exception while closing input stream for " + mOriginalStoragePath
+ " (ignoring)", e
);
263 } catch (Exception e
) {
264 Log_OC
.d(TAG
, "Weird exception while closing output stream for " + expectedPath
+ " (ignoring)", e
);
270 localCopyPassed
= true
;
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());
277 mUploadOperation
= new UploadRemoteFileOperation(mFile
.getStoragePath(), mFile
.getRemotePath(),
278 mFile
.getMimetype());
280 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
281 while (listener
.hasNext()) {
282 mUploadOperation
.addDatatransferProgressListener(listener
.next());
284 result
= mUploadOperation
.execute(client
);
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
);
293 mFile
.setStoragePath(expectedPath
);
294 File fileToMove
= null
;
295 if (temporalFile
!= null
) { // FileUploader.LOCAL_BEHAVIOUR_COPY
296 // ; see where temporalFile was
298 fileToMove
= temporalFile
;
299 } else { // FileUploader.LOCAL_BEHAVIOUR_MOVE
300 fileToMove
= originalFile
;
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
311 // the best option could be show a warning message
314 // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
321 } catch (Exception e
) {
322 // TODO something cleaner with cancellations
323 if (mCancellationRequested
.get()) {
324 result
= new RemoteOperationResult(new OperationCancelledException());
326 result
= new RemoteOperationResult(e
);
330 if (temporalFile
!= null
&& !originalFile
.equals(temporalFile
)) {
331 temporalFile
.delete();
333 if (result
.isSuccess()) {
334 Log_OC
.i(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage());
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
)
344 Log_OC
.e(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage() + complement
, result
.getException());
346 Log_OC
.e(TAG
, "Upload of " + mOriginalStoragePath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage());
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());
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.
379 private String
getAvailableRemotePath(OwnCloudClient wc
, String remotePath
) throws Exception
{
380 boolean check
= existsFile(wc
, remotePath
);
385 int pos
= remotePath
.lastIndexOf(".");
387 String extension
= "";
389 extension
= remotePath
.substring(pos
+ 1);
390 remotePath
= remotePath
.substring(0, pos
);
394 suffix
= " (" + count
+ ")";
396 check
= existsFile(wc
, remotePath
+ suffix
+ "." + extension
);
399 check
= existsFile(wc
, remotePath
+ suffix
);
405 return remotePath
+ suffix
+ "." + extension
;
407 return remotePath
+ suffix
;
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();
417 public void cancel() {
418 mUploadOperation
.cancel();