Merge pull request #6900 from nextcloud/fixCheckIfEnoughSpace

Fix npe on computing enough space with non-downloaded folder content
This commit is contained in:
Tobias Kaminsky 2020-09-08 14:38:15 +02:00 committed by GitHub
commit fdeca7c5a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View file

@ -392,9 +392,24 @@ class OCFileListFragmentIT : AbstractOnServerIT() {
assertTrue(sut.checkIfEnoughSpace(200L, ocFile)) assertTrue(sut.checkIfEnoughSpace(200L, ocFile))
ocFile.fileLength = 100 ocFile.fileLength = 100
assertFalse(sut.checkIfEnoughSpace(50L, ocFile))
ocFile.fileLength = 44
assertTrue(sut.checkIfEnoughSpace(50L, ocFile)) assertTrue(sut.checkIfEnoughSpace(50L, ocFile))
ocFile.fileLength = 100 ocFile.fileLength = 100
assertTrue(sut.checkIfEnoughSpace(100L, ocFile)) 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))
}
} }

View file

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