+ Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files");
+
+ mSuccessCounter = 0;
+
+ for (int i = 0; i < mLocalPaths.length; ++i) {
+
+ String mimeType = null;
+ try {
+ mimeType = MimeTypeMap.getSingleton()
+ .getMimeTypeFromExtension(
+ mLocalPaths[i].substring(mLocalPaths[i]
+ .lastIndexOf('.') + 1));
+ } catch (IndexOutOfBoundsException e) {
+ Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]);
+ }
+ if (mimeType == null)
+ mimeType = "application/octet-stream";
+
+ mCurrentIndexUpload = i;
+ 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);
+ new_file.setFileLength(new File(mLocalPaths[i]).length());
+ new_file.setModificationTimestamp(System.currentTimeMillis());
+ new_file.setLastSyncDate(0);
+ new_file.setStoragePath(mLocalPaths[i]);
+ File f = new File(mRemotePaths[i]);
+ long parentDirId = storageManager.getFileByPath(f.getParent().endsWith("/")?f.getParent():f.getParent()+"/").getFileId();
+ new_file.setParentId(parentDirId);
+ storageManager.saveFile(new_file);
+
+ Intent end = new Intent(UPLOAD_FINISH_MESSAGE);
+ end.putExtra(EXTRA_PARENT_DIR_ID, parentDirId);
+ end.putExtra(ACCOUNT_NAME, mAccount.name);
+ sendBroadcast(end);
+ }
+
+ }
+ mNotificationManager.cancel(42);
+ 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;
+ int percent = (int)(100*((double)mSendData)/((double)mTotalDataToSend));
+ if (percent != mPreviousPercent) {
+ String text = String.format("%d%% Uploading %s file", percent, new File(mLocalPaths[mCurrentIndexUpload]).getName());
+ mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false);
+ mNotification.contentView.setTextViewText(R.id.status_text, text);
+ mNotificationManager.notify(42, mNotification);
+ }
+ mPreviousPercent = percent;