Skip to content

Commit 1b1d9c2

Browse files
Merge pull request #16374 from nextcloud/fix/npe-sync-folder
check(synchronize-folder-operation): local folder npe
2 parents 21bb51e + f699bb9 commit 1b1d9c2

2 files changed

Lines changed: 37 additions & 18 deletions

File tree

app/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.ArrayList;
3838
import java.util.List;
3939
import java.util.Map;
40+
import java.util.Optional;
4041
import java.util.Vector;
4142
import java.util.concurrent.atomic.AtomicBoolean;
4243

@@ -126,6 +127,10 @@ protected RemoteOperationResult run(OwnCloudClient client) {
126127
try {
127128
// get locally cached information about folder
128129
mLocalFolder = getStorageManager().getFileByPath(mRemotePath);
130+
if (mLocalFolder == null) {
131+
Log_OC.e(TAG, "Local folder is null, cannot run synchronize folder operation, remote path: " + mRemotePath);
132+
return new RemoteOperationResult<>(ResultCode.FILE_NOT_FOUND);
133+
}
129134

130135
result = checkForChanges(client);
131136

@@ -163,18 +168,17 @@ private RemoteOperationResult checkForChanges(OwnCloudClient client) throws Oper
163168

164169
// remote request
165170
ReadFileRemoteOperation operation = new ReadFileRemoteOperation(mRemotePath);
166-
RemoteOperationResult result = operation.execute(client);
167-
if (result.isSuccess()) {
168-
OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0));
171+
var result = operation.execute(client);
172+
if (result.isSuccess() && result.getData().get(0) instanceof RemoteFile remoteFile) {
173+
OCFile remoteFolder = FileStorageUtils.fillOCFile(remoteFile);
169174

170175
// check if remote and local folder are different
171176
mRemoteFolderChanged = !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag()));
172177

173-
result = new RemoteOperationResult(ResultCode.OK);
178+
result = new RemoteOperationResult<>(ResultCode.OK);
174179

175180
Log_OC.i(TAG, "Checked " + user.getAccountName() + mRemotePath + " : " +
176-
(mRemoteFolderChanged ? "changed" : "not changed"));
177-
181+
(mRemoteFolderChanged ? "changed" : "not changed"));
178182
} else {
179183
// check failed
180184
if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
@@ -549,12 +553,20 @@ public void cancel() {
549553
mCancellationRequested.set(true);
550554
}
551555

552-
public String getFolderPath() {
556+
public Optional<String> getFolderNameFromPath() {
557+
if (mLocalFolder == null) {
558+
return Optional.empty();
559+
}
560+
553561
String path = mLocalFolder.getStoragePath();
554562
if (!TextUtils.isEmpty(path)) {
555-
return path;
563+
File folder = new File(path);
564+
return Optional.of(folder.getName());
556565
}
557-
return FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mLocalFolder);
566+
567+
String filepath = FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), mLocalFolder);
568+
File folder = new File(filepath);
569+
return Optional.of(folder.getName());
558570
}
559571

560572
private void startSyncFolderOperation(String path){

app/src/main/java/com/owncloud/android/utils/ErrorMessageAdapter.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.io.File;
3737
import java.net.SocketTimeoutException;
38+
import java.util.Optional;
3839

3940
import androidx.annotation.NonNull;
4041
import androidx.annotation.Nullable;
@@ -160,10 +161,13 @@ String getMessageForSynchronizeFolderOperation(
160161
Resources res) {
161162

162163
if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND) {
163-
return String.format(
164-
res.getString(R.string.sync_current_folder_was_removed),
165-
new File(operation.getFolderPath()).getName()
166-
);
164+
Optional<String> folderName = operation.getFolderNameFromPath();
165+
if (folderName.isEmpty()) {
166+
return null;
167+
}
168+
169+
String format = res.getString(R.string.sync_current_folder_was_removed);
170+
return String.format(format, folderName.get());
167171
}
168172
return null;
169173
}
@@ -488,11 +492,14 @@ String getMessageForOperation(RemoteOperation operation, Resources res) {
488492
} else if (operation instanceof MoveFileOperation) {
489493
message = res.getString(R.string.move_file_error);
490494

491-
} else if (operation instanceof SynchronizeFolderOperation) {
492-
String folderPathName = new File(
493-
((SynchronizeFolderOperation) operation).getFolderPath()
494-
).getName();
495-
message = String.format(res.getString(R.string.sync_folder_failed_content), folderPathName);
495+
} else if (operation instanceof SynchronizeFolderOperation synchronizeFolderOperation) {
496+
Optional<String> folderName = synchronizeFolderOperation.getFolderNameFromPath();
497+
if (folderName.isEmpty()) {
498+
return null;
499+
}
500+
501+
String format = res.getString(R.string.sync_folder_failed_content);
502+
message = String.format(format, folderName.get());
496503

497504
} else if (operation instanceof CopyFileOperation) {
498505
message = res.getString(R.string.copy_file_error);

0 commit comments

Comments
 (0)