- if (!result.isSuccess() && !mCurrentUpload.getRemotePath().equals(file.getRemotePath())) {
- // true when the file was automatically renamed to avoid an overwrite ; yes, this is a bit obscure...
- OCFile newFile = new OCFile(mCurrentUpload.getRemotePath());
- newFile.setCreationTimestamp(file.getCreationTimestamp());
- newFile.setFileLength(file.getFileLength());
- newFile.setMimetype(file.getMimetype());
- newFile.setModificationTimestamp(file.getModificationTimestamp());
- newFile.setLastSyncDateForProperties(file.getLastSyncDateForProperties());
- newFile.setStoragePath(file.getStoragePath());
- newFile.setKeepInSync(file.keepInSync());
- // newFile.setEtag(file.getEtag())
- file = newFile;
+ if (mCurrentUpload.wasRenamed()) {
+ OCFile oldFile = mCurrentUpload.getOldFile();
+ if (!oldFile.fileExists()) {
+ // just a name coincidence
+ file.setStoragePath(oldFile.getStoragePath());
+
+ } else {
+ // conflict resolved with 'Keep both' by the user
+ File localFile = new File(oldFile.getStoragePath());
+ File newLocalFile = new File(FileStorageUtils.getDefaultSavePathFor(mCurrentUpload.getAccount().name, file));
+ boolean renameSuccessed = localFile.renameTo(newLocalFile);
+ if (renameSuccessed) {
+ file.setStoragePath(newLocalFile.getAbsolutePath());
+
+ } else {
+ // poor solution
+ Log.d(TAG, "DAMN IT: local rename failed after uploading a file with a new name already existing both in the remote account and the local database (should be due to a conflict solved with 'keep both'");
+ file.setStoragePath(null);
+ // not so fine:
+ // - local file will be kept there as 'trash' until is download (and overwritten) again from the server;
+ // - user will see as 'not down' a file that was just upload
+ // BUT:
+ // - no loss of data happened
+ // - when the user downloads again the renamed and original file from the server, local file names and contents will be correctly synchronized with names and contents in server
+ }
+ oldFile.setStoragePath(null);
+ mStorageManager.saveFile(oldFile);
+ }