1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.operations
;
22 import java
.io
.IOException
;
24 import org
.apache
.commons
.httpclient
.HttpException
;
25 import org
.apache
.commons
.httpclient
.methods
.PutMethod
;
26 import org
.apache
.http
.HttpStatus
;
28 import com
.owncloud
.android
.operations
.RemoteOperation
;
29 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
31 import eu
.alefzero
.webdav
.FileRequestEntity
;
32 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
33 import eu
.alefzero
.webdav
.WebdavClient
;
34 import eu
.alefzero
.webdav
.WebdavUtils
;
35 import android
.util
.Log
;
36 import android
.webkit
.MimeTypeMap
;
39 * Remote operation performing the upload of a file to an ownCloud server
41 * @author David A. Velasco
43 public class UploadFileOperation
extends RemoteOperation
{
45 private static final String TAG
= UploadFileOperation
.class.getCanonicalName();
47 private String mLocalPath
= null
;
48 private String mRemotePath
= null
;
49 private String mMimeType
= null
;
50 private boolean mIsInstant
= false
;
51 private boolean mForceOverwrite
= false
;
52 private OnDatatransferProgressListener mDataTransferListener
= null
;
55 public String
getLocalPath() {
59 public String
getRemotePath() {
63 public String
getMimeType() {
68 public boolean isInstant() {
73 public boolean getForceOverwrite() {
74 return mForceOverwrite
;
78 public OnDatatransferProgressListener
getDataTransferListener() {
79 return mDataTransferListener
;
83 public UploadFileOperation( String localPath
,
87 boolean forceOverwrite
,
88 OnDatatransferProgressListener dataTransferProgressListener
) {
89 mLocalPath
= localPath
;
90 mRemotePath
= remotePath
;
92 if (mMimeType
== null
) {
94 mMimeType
= MimeTypeMap
.getSingleton()
95 .getMimeTypeFromExtension(
96 localPath
.substring(localPath
.lastIndexOf('.') + 1));
97 } catch (IndexOutOfBoundsException e
) {
98 Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + localPath
);
101 if (mMimeType
== null
) {
102 mMimeType
= "application/octet-stream";
104 mIsInstant
= isInstant
;
105 mForceOverwrite
= forceOverwrite
;
106 mDataTransferListener
= dataTransferProgressListener
;
110 protected RemoteOperationResult
run(WebdavClient client
) {
111 RemoteOperationResult result
= null
;
112 boolean nameCheckPassed
= false
;
114 /// rename the file to upload, if necessary
115 if (!mForceOverwrite
) {
116 mRemotePath
= getAvailableRemotePath(client
, mRemotePath
);
119 /// perform the upload
120 nameCheckPassed
= true
;
121 int status
= uploadFile(client
);
122 result
= new RemoteOperationResult(isSuccess(status
), status
);
123 Log
.i(TAG
, "Upload of " + mLocalPath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage());
125 } catch (Exception e
) {
126 result
= new RemoteOperationResult(e
);
127 Log
.e(TAG
, "Upload of " + mLocalPath
+ " to " + mRemotePath
+ ": " + result
.getLogMessage() + (nameCheckPassed?
"":" (while checking file existence in server)"), e
);
135 public boolean isSuccess(int status
) {
136 return ((status
== HttpStatus
.SC_OK
|| status
== HttpStatus
.SC_CREATED
|| status
== HttpStatus
.SC_NO_CONTENT
));
140 protected int uploadFile(WebdavClient client
) throws HttpException
, IOException
{
142 PutMethod put
= new PutMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
145 File f
= new File(mLocalPath
);
146 FileRequestEntity entity
= new FileRequestEntity(f
, mMimeType
);
147 entity
.setOnDatatransferProgressListener(mDataTransferListener
);
148 put
.setRequestEntity(entity
);
149 status
= client
.executeMethod(put
);
150 client
.exhaustResponse(put
.getResponseBodyAsStream());
153 put
.releaseConnection(); // let the connection available for other methods
159 * Checks if remotePath does not exist in the server and returns it, or adds a suffix to it in order to avoid the server
160 * file is overwritten.
165 private String
getAvailableRemotePath(WebdavClient wc
, String remotePath
) throws Exception
{
166 boolean check
= wc
.existsFile(remotePath
);
171 int pos
= remotePath
.lastIndexOf(".");
173 String extension
= "";
175 extension
= remotePath
.substring(pos
+1);
176 remotePath
= remotePath
.substring(0, pos
);
180 suffix
= " (" + count
+ ")";
182 check
= wc
.existsFile(remotePath
+ suffix
+ "." + extension
);
184 check
= wc
.existsFile(remotePath
+ suffix
);
189 return remotePath
+ suffix
+ "." + extension
;
191 return remotePath
+ suffix
;