From: David A. Velasco Date: Mon, 23 Jul 2012 08:13:50 +0000 (+0200) Subject: Rename file to upload to avoid overwrites in server X-Git-Tag: oc-android-1.4.3~252 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/3a34911f3b39e2ed2188c09a7ac609c24ff74386?ds=inline Rename file to upload to avoid overwrites in server --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 6eeed8f3..7c958290 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.172B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index 57605960..9f480fc0 100644 --- a/src/eu/alefzero/owncloud/files/services/FileUploader.java +++ b/src/eu/alefzero/owncloud/files/services/FileUploader.java @@ -2,6 +2,9 @@ package eu.alefzero.owncloud.files.services; import java.io.File; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.HeadMethod; + import android.accounts.Account; import android.accounts.AccountManager; import android.app.Notification; @@ -168,7 +171,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mimeType = "application/octet-stream"; mCurrentIndexUpload = i; - if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) { + mRemotePaths[i] = getAvailableRemotePath(wc, mRemotePaths[i]); + if (mRemotePaths[i] != null && wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) { mSuccessCounter++; OCFile new_file = new OCFile(mRemotePaths[i]); new_file.setMimetype(mimeType); @@ -191,6 +195,46 @@ public class FileUploader extends Service implements OnDatatransferProgressListe run(); } + /** + * Checks if remotePath does not exist in the server and returns it, or adds a suffix to it in order to avoid the server + * file is overwritten. + * + * @param string + * @return + */ + private String getAvailableRemotePath(WebdavClient wc, String remotePath) { + Boolean check = wc.existsFile(remotePath); + if (check == null) { // null means fail + return null; + } else if (!check) { + return remotePath; + } + + int pos = remotePath.lastIndexOf("."); + String suffix = ""; + String extension = ""; + if (pos >= 0) { + extension = remotePath.substring(pos+1); + remotePath = remotePath.substring(0, pos); + } + int count = 2; + while (check != null && check) { + suffix = " (" + count + ")"; + if (pos >= 0) + check = wc.existsFile(remotePath + suffix + "." + extension); + else + check = wc.existsFile(remotePath + suffix); + count++; + } + if (check == null) { + return null; + } else if (pos >=0) { + return remotePath + suffix + "." + extension; + } else { + return remotePath + suffix; + } + } + @Override public void transferProgress(long progressRate) { mSendData += progressRate; diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 939a22d3..bd58fcc2 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -252,6 +252,23 @@ Log.e("ASD", ""+username); } return true; } + + + /** + * Check if a file exists in the OC server + * + * @return 'Boolean.TRUE' if the file exists; 'Boolean.FALSE' it doesn't exist; NULL if couldn't be checked + */ + public Boolean existsFile(String path) { + try { + HeadMethod head = new HeadMethod(mUri.toString() + WebdavUtils.encodePath(path)); + int status = executeMethod(head); + return (status == HttpStatus.SC_OK); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } /**