Merge remote-tracking branch 'remotes/upstream/avoidDuplicateFiles' into beta
authortobiasKaminsky <tobias@kaminsky.me>
Fri, 27 Nov 2015 16:47:46 +0000 (17:47 +0100)
committertobiasKaminsky <tobias@kaminsky.me>
Fri, 27 Nov 2015 16:47:46 +0000 (17:47 +0100)
1  2 
src/com/owncloud/android/operations/UploadFileOperation.java

@@@ -26,6 -26,7 +26,7 @@@ import java.io.FileOutputStream
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
+ import java.nio.channels.FileChannel;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.Set;
@@@ -348,17 -349,37 +349,37 @@@ public class UploadFileOperation extend
                      if (!expectedFile.equals(fileToMove)) {
                          File expectedFolder = expectedFile.getParentFile();
                          expectedFolder.mkdirs();
-                         if (!expectedFolder.isDirectory() || !fileToMove.renameTo(expectedFile)) {
-                             mFile.setStoragePath(null); // forget the local file
-                             // by now, treat this as a success; the file was
-                             // uploaded; the user won't like that the local file
-                             // is not linked, but this should be a very rare
-                             // fail;
-                             // the best option could be show a warning message
-                             // (but not a fail)
-                             // result = new
-                             // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
-                             // return result;
+                         if (expectedFolder.isDirectory()){
+                             if (!fileToMove.renameTo(expectedFile)){
+                                 // try to copy and then delete
+                                 expectedFile.createNewFile();
+                                 FileChannel inChannel = new FileInputStream(fileToMove).getChannel();
+                                 FileChannel outChannel = new FileOutputStream(expectedFile).getChannel();
+                                 try {
+                                     inChannel.transferTo(0, inChannel.size(), outChannel);
+                                     fileToMove.delete();
+                                 } catch (Exception e){
+                                     mFile.setStoragePath(null); // forget the local file
+                                     // by now, treat this as a success; the file was
+                                     // uploaded; the user won't like that the local file
+                                     // is not linked, but this should be a very rare
+                                     // fail;
+                                     // the best option could be show a warning message
+                                     // (but not a fail)
+                                     // result = new
+                                     // RemoteOperationResult(ResultCode.LOCAL_STORAGE_NOT_MOVED);
+                                     // return result;
+                                 }
+                                 finally {
+                                     if (inChannel != null) inChannel.close();
+                                     if (outChannel != null) outChannel.close();
+                                 }
+                             }
+                         } else {
+                             mFile.setStoragePath(null);
                          }
                      }
                  }
              if (temporalFile != null && !originalFile.equals(temporalFile)) {
                  temporalFile.delete();
              }
 +            if (result == null){
 +                return new RemoteOperationResult(false, 404, null);
 +            }
              if (result.isSuccess()) {
                  Log_OC.i(TAG, "Upload of " + mOriginalStoragePath + " to " + mRemotePath + ": " +
                          result.getLogMessage());