X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/3113c459092dbb3dea60e50efe61070e38804b09..463b30c46a57df8c298be9d5b196e8e0330a0b73:/src/eu/alefzero/webdav/WebdavClient.java?ds=inline diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 8d2934c1..e248eb84 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -113,8 +113,16 @@ public class WebdavClient extends HttpClient { new EasySSLSocketFactory(), 443)); } + /** + * Downloads a file in remoteFilepath to the local targetPath. + * + * @param remoteFilepath Path to the file in the remote server, URL DECODED. + * @param targetPath Local path to save the downloaded file. + * @return 'True' when the file is successfully downloaded. + */ public boolean downloadFile(String remoteFilepath, File targetPath) { - GetMethod get = new GetMethod(mUri.toString() + remoteFilepath); + boolean ret = false; + GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath)); HttpMethodParams params = get.getParams(); params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit get.setParams(params); @@ -125,35 +133,37 @@ public class WebdavClient extends HttpClient { try { int status = executeMethod(get); Log.e(TAG, "status return: " + status); - if (status != HttpStatus.SC_OK) { - return false; - } - BufferedInputStream bis = new BufferedInputStream( - get.getResponseBodyAsStream()); - FileOutputStream fos = new FileOutputStream(targetPath); - - byte[] bytes = new byte[4096]; - int readResult; - while ((readResult = bis.read(bytes)) != -1) { - if (mDataTransferListener != null) - mDataTransferListener.transferProgress(readResult); - fos.write(bytes, 0, readResult); - } + if (status == HttpStatus.SC_OK) { + targetPath.createNewFile(); + BufferedInputStream bis = new BufferedInputStream( + get.getResponseBodyAsStream()); + FileOutputStream fos = new FileOutputStream(targetPath); - } catch (IOException e) { + byte[] bytes = new byte[4096]; + int readResult; + while ((readResult = bis.read(bytes)) != -1) { + if (mDataTransferListener != null) + mDataTransferListener.transferProgress(readResult); + fos.write(bytes, 0, readResult); + } + + } + ret = true; + } catch (Throwable e) { e.printStackTrace(); - return false; + targetPath.delete(); } - return true; + + return ret; } /** * Deletes a remote file via webdav - * @param remoteFilePath + * @param remoteFilePath Remote file path of the file to delete, in URL DECODED format. * @return */ public boolean deleteFile(String remoteFilePath){ - DavMethod delete = new DeleteMethod(mUri.toString() + remoteFilePath); + DavMethod delete = new DeleteMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilePath)); try { executeMethod(delete); } catch (Throwable e) { @@ -167,6 +177,15 @@ public class WebdavClient extends HttpClient { mDataTransferListener = listener; } + /** + * Creates or update a file in the remote server with the contents of a local file. + * + * + * @param localFile Path to the local file to upload. + * @param remoteTarget Remote path to the file to create or update, URL DECODED + * @param contentType MIME type of the file. + * @return 'True' then the upload was successfully completed + */ public boolean putFile(String localFile, String remoteTarget, String contentType) { boolean result = true; @@ -177,7 +196,7 @@ public class WebdavClient extends HttpClient { FileRequestEntity entity = new FileRequestEntity(f, contentType); entity.setOnDatatransferProgressListener(mDataTransferListener); Log.e("ASD", f.exists() + " " + entity.getContentLength()); - PutMethod put = new PutMethod(mUri.toString() + remoteTarget); + PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget)); HttpMethodParams params = put.getParams(); params.setSoTimeout(0); // that means "infinite timeout"; it's the default value, but let's make it explicit put.setParams(params); @@ -215,9 +234,15 @@ public class WebdavClient extends HttpClient { return returnCode; } + /** + * Creates a remote directory with the received path. + * + * @param path Path of the directory to create, URL DECODED + * @return 'True' when the directory is successfully created + */ public boolean createDirectory(String path) { try { - MkColMethod mkcol = new MkColMethod(mUri.toString() + path); + MkColMethod mkcol = new MkColMethod(mUri.toString() + WebdavUtils.encodePath(path)); int status = executeMethod(mkcol); Log.d(TAG, "Status returned " + status); Log.d(TAG, "uri: " + mkcol.getURI().toString());