Skip to content

Commit

Permalink
Fix #15621.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Apr 28, 2024
1 parent 67e6dab commit 842f2ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import ch.cyberduck.core.Host;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.features.Home;
import ch.cyberduck.core.preferences.HostPreferences;
import ch.cyberduck.core.shared.AbstractHomeFeature;
import ch.cyberduck.core.shared.DefaultPathHomeFeature;
import ch.cyberduck.core.shared.DelegatingHomeFeature;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand All @@ -29,36 +33,51 @@
public class NextcloudHomeFeature extends AbstractHomeFeature {
private static final Logger log = LogManager.getLogger(NextcloudHomeFeature.class);

private final Home delegate;
private final Host bookmark;
private final String root;

public NextcloudHomeFeature(final Host bookmark) {
this(bookmark, new HostPreferences(bookmark).getProperty("nextcloud.root.default"));
this(new DefaultPathHomeFeature(bookmark), bookmark);
}

public NextcloudHomeFeature(final Home delegate, final Host bookmark) {
this(delegate, bookmark, new HostPreferences(bookmark).getProperty("nextcloud.root.default"));
}

/**
* @param root WebDAV root
*/
public NextcloudHomeFeature(final Host bookmark, final String root) {
public NextcloudHomeFeature(final Home delegate, final Host bookmark, final String root) {
this.delegate = delegate;
this.bookmark = bookmark;
this.root = root;
}

@Override
public Path find() {
public Path find() throws BackgroundException {
return this.find(Context.files);
}

public Path find(final Context files) {
String username = bookmark.getCredentials().getUsername();
public Path find(final Context files) throws BackgroundException {
final String username = bookmark.getCredentials().getUsername();
if(StringUtils.isBlank(username)) {
if(log.isWarnEnabled()) {
log.warn(String.format("Missing username for %s", bookmark));
}
return null;
return delegate.find();
}
// Custom path setting
final Path workdir;
final Path defaultpath = new DelegatingHomeFeature(delegate).find();
if(!defaultpath.isRoot() && StringUtils.isNotBlank(StringUtils.removeStart(defaultpath.getAbsolute(), root))) {
workdir = new Path(new Path(String.format("%s/%s/%s", root, files.name(), username), EnumSet.of(Path.Type.directory)),
StringUtils.removeStart(defaultpath.getAbsolute(), root), EnumSet.of(Path.Type.directory));
}
else {
workdir = new Path(new Path(String.format("%s/%s", root, files.name()), EnumSet.of(Path.Type.directory)),
username, EnumSet.of(Path.Type.directory));
}
final Path workdir = new Path(new Path(String.format("%s/%s", root, files.name()), EnumSet.of(Path.Type.directory)),
username, EnumSet.of(Path.Type.directory));
if(log.isDebugEnabled()) {
log.debug(String.format("Use home directory %s", workdir));
}
Expand All @@ -67,6 +86,7 @@ public Path find(final Context files) {

public enum Context {
files,
versions
versions,
meta
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,17 @@ public void testFind() throws Exception {
assertNull(feature.find());
bookmark.setCredentials(new Credentials("u"));
assertEquals(new Path("/remote.php/dav/files/u", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/remote.php/dav/");
assertEquals(new Path("/remote.php/dav/files/u", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/remote.php/dav");
assertEquals(new Path("/remote.php/dav/files/u", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/remote.php/dav/d");
assertEquals(new Path("/remote.php/dav/files/u/d", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/remote.php/dav/d/");
assertEquals(new Path("/remote.php/dav/files/u/d", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/d");
assertEquals(new Path("/remote.php/dav/files/u/d", EnumSet.of(Path.Type.directory)), feature.find());
bookmark.setDefaultPath("/d/");
assertEquals(new Path("/remote.php/dav/files/u/d", EnumSet.of(Path.Type.directory)), feature.find());
}
}

0 comments on commit 842f2ec

Please sign in to comment.