浏览代码

Merge pull request #6900 from nextcloud/fixCheckIfEnoughSpace

Fix npe on computing enough space with non-downloaded folder content
Tobias Kaminsky 4 年之前
父节点
当前提交
fdeca7c5a2

+ 15 - 0
src/androidTest/java/com/owncloud/android/ui/fragment/OCFileListFragmentIT.kt

@@ -392,9 +392,24 @@ class OCFileListFragmentIT : AbstractOnServerIT() {
         assertTrue(sut.checkIfEnoughSpace(200L, ocFile))
 
         ocFile.fileLength = 100
+        assertFalse(sut.checkIfEnoughSpace(50L, ocFile))
+
+        ocFile.fileLength = 44
         assertTrue(sut.checkIfEnoughSpace(50L, ocFile))
 
         ocFile.fileLength = 100
         assertTrue(sut.checkIfEnoughSpace(100L, ocFile))
     }
+
+    @Test
+    @SuppressWarnings("MagicNumber")
+    fun testEnoughSpaceWithNoLocalFolder() {
+        val sut = OCFileListFragment()
+        val ocFile = OCFile("/test/")
+
+        ocFile.mimeType = "DIR"
+
+        ocFile.fileLength = 100
+        assertTrue(sut.checkIfEnoughSpace(200L, ocFile))
+    }
 }

+ 11 - 1
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -100,6 +100,7 @@ import com.owncloud.android.ui.preview.PreviewTextFileFragment;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.EncryptionUtils;
 import com.owncloud.android.utils.FileSortOrder;
+import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.MimeTypeUtil;
 import com.owncloud.android.utils.ThemeUtils;
 
@@ -1782,13 +1783,22 @@ public class OCFileListFragment extends ExtendedListFragment implements
     public boolean checkIfEnoughSpace(long availableSpaceOnDevice, OCFile file) {
         if (file.isFolder()) {
             // on folders we assume that we only need difference
-            return availableSpaceOnDevice > (file.getFileLength() - new File(file.getStoragePath()).length());
+            return availableSpaceOnDevice > (file.getFileLength() - localFolderSize(file));
         } else {
             // on files complete file must first be stored, then target gets overwritten
             return availableSpaceOnDevice > file.getFileLength();
         }
     }
 
+    private long localFolderSize(OCFile file) {
+        if (file.getStoragePath() == null) {
+            // not yet downloaded anything
+            return 0;
+        } else {
+            return FileStorageUtils.getFolderSize(new File(file.getStoragePath()));
+        }
+    }
+
     private void showSpaceErrorDialog(OCFile file, long availableSpaceOnDevice) {
         SyncFileNotEnoughSpaceDialogFragment dialog =
             SyncFileNotEnoughSpaceDialogFragment.newInstance(file, availableSpaceOnDevice);