X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/eb1c63d3dc5294e2ff7a7d2c32d762fd25286223..a032bdeebc51a6e81d1bd5c558944f96fc55eacb:/src/eu/alefzero/owncloud/files/services/FileUploader.java diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index 68ddafb7..db2461c8 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; @@ -34,6 +37,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe public static final String KEY_REMOTE_FILE = "REMOTE_FILE"; public static final String KEY_ACCOUNT = "ACCOUNT"; public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE"; + public static final String ACCOUNT_NAME = "ACCOUNT_NAME"; public static final int UPLOAD_SINGLE_FILE = 0; public static final int UPLOAD_MULTIPLE_FILES = 1; @@ -99,9 +103,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mRemotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE); } - for (int i = 0; i < mRemotePaths.length; ++i) - mRemotePaths[i] = mRemotePaths[i].replace(' ', '+'); - if (mLocalPaths.length != mRemotePaths.length) { Log.e(TAG, "Remote paths and local paths are not equal!"); return Service.START_NOT_STICKY; @@ -119,9 +120,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe if (mSuccessCounter == mLocalPaths.length) { message = getString(R.string.uploader_upload_succeed); } else { - message = getString(R.string.uploader_upload_failed); - if (mLocalPaths.length > 1) - message += " (" + mSuccessCounter + " / " + mLocalPaths.length + getString(R.string.uploader_files_uploaded_suffix) + ")"; + message = String.format(getString(R.string.uploader_upload_failed), mSuccessCounter, mLocalPaths.length); } Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } @@ -145,6 +144,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mNotificationManager.notify(42, mNotification); WebdavClient wc = new WebdavClient(mAccount, getApplicationContext()); + wc.allowSelfsignedCertificates(); wc.setDataTransferProgressListener(this); for (int i = 0; i < mLocalPaths.length; ++i) { @@ -158,7 +158,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe for (int i = 0; i < mLocalPaths.length; ++i) { - String mimeType; + String mimeType = null; try { mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension( @@ -166,11 +166,13 @@ public class FileUploader extends Service implements OnDatatransferProgressListe .lastIndexOf('.') + 1)); } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]); - mimeType = "application/octet-stream"; } + if (mimeType == null) + 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); @@ -185,6 +187,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe Intent end = new Intent(UPLOAD_FINISH_MESSAGE); end.putExtra(EXTRA_PARENT_DIR_ID, parentDirId); + end.putExtra(ACCOUNT_NAME, mAccount.name); sendBroadcast(end); } @@ -193,6 +196,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;