Merge pull request #14801 from iterate-ch/bugfix/GH-14800

Fix #14800.
This commit is contained in:
David Kocher
2023-06-15 16:12:31 +02:00
committed by GitHub
3 changed files with 59 additions and 14 deletions
@@ -25,6 +25,7 @@ import ch.cyberduck.core.transfer.TransferItem;
import org.apache.commons.cli.CommandLine;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
@@ -35,16 +36,24 @@ public class UploadTransferItemFinder implements TransferItemFinder {
public Set<TransferItem> find(final CommandLine input, final TerminalAction action, final Path remote) {
final Local local = LocalFactory.get(input.getOptionValues(action.name())[1]);
final Set<TransferItem> items = new HashSet<>();
items.add(resolve(remote, local));
// Append local name to remote target
final boolean append = !Arrays.asList(input.getArgs()).isEmpty();
items.add(resolve(remote, local, append));
for(String arg : input.getArgs()) {
items.add(resolve(remote, LocalFactory.get(arg)));
items.add(resolve(remote, LocalFactory.get(arg), append));
}
return items;
}
protected static TransferItem resolve(final Path remote, final Local local) {
protected static TransferItem resolve(final Path remote, final Local local, final boolean append) {
if(local.isDirectory()) {
// Local path resolves to folder
if(remote.isDirectory()) {
if(append) {
return new TransferItem(new Path(remote, local.getName(), EnumSet.of(Path.Type.directory)), local);
}
}
// Append local name to remote target
return new TransferItem(remote, local);
}
// Local path resolves to file
@@ -44,7 +44,7 @@ public class SingleTransferItemFinderTest {
final Set<TransferItem> found = new SingleTransferItemFinder().find(input, TerminalAction.download, new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)));
assertFalse(found.isEmpty());
assertEquals(new TransferItem(new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)), LocalFactory.get(System.getProperty("user.dir") + "/remote")),
found.iterator().next());
found.iterator().next());
}
@Test
@@ -56,8 +56,7 @@ public class SingleTransferItemFinderTest {
final Set<TransferItem> found = new SingleTransferItemFinder().find(input, TerminalAction.download, new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)));
assertFalse(found.isEmpty());
assertEquals(new TransferItem(new Path("/cdn.cyberduck.ch/remote", EnumSet.of(Path.Type.file)), LocalFactory.get(String.format("%s/f", temp))),
found.iterator().next());
found.iterator().next());
}
@Test
@@ -78,7 +77,7 @@ public class SingleTransferItemFinderTest {
final Set<TransferItem> found = new SingleTransferItemFinder().find(input, TerminalAction.upload, new Path("/remote", EnumSet.of(Path.Type.directory)));
assertFalse(found.isEmpty());
assertEquals(new TransferItem(new Path("/remote/f", EnumSet.of(Path.Type.file)), LocalFactory.get(String.format("%s/f", temp))),
found.iterator().next());
found.iterator().next());
}
@Test
@@ -94,6 +93,42 @@ public class SingleTransferItemFinderTest {
assertEquals(new TransferItem(new Path("/remote", EnumSet.of(Path.Type.directory)), temp), iter.next());
}
@Test
public void testUploadFilesWithExpandedGlobToDirectoryTarget() throws Exception {
final CommandLineParser parser = new PosixParser();
final String temp = System.getProperty("java.io.tmpdir");
final CommandLine input = parser.parse(TerminalOptionsBuilder.options(), new String[]{"--upload", "ftps://test.cyberduck.ch/remote/", String.format("%s/f1", temp),
String.format("%s/f2", temp)});
final Set<TransferItem> found = new SingleTransferItemFinder().find(input, TerminalAction.upload, new Path("/remote", EnumSet.of(Path.Type.directory)));
assertFalse(found.isEmpty());
assertEquals(2, found.size());
final Iterator<TransferItem> iter = found.iterator();
assertEquals(new TransferItem(new Path("/remote/f1", EnumSet.of(Path.Type.file)), new Local(temp, "f1")), iter.next());
assertEquals(new TransferItem(new Path("/remote/f2", EnumSet.of(Path.Type.file)), new Local(temp, "f2")), iter.next());
}
@Test
public void testUploadFoldersWithExpandedGlobToDirectoryTarget() throws Exception {
final CommandLineParser parser = new PosixParser();
final String temp = System.getProperty("java.io.tmpdir");
final Local d1 = new Local(String.format("%s/d1", temp));
d1.mkdir();
final Local d2 = new Local(String.format("%s/d2", temp));
d2.mkdir();
final CommandLine input = parser.parse(TerminalOptionsBuilder.options(), new String[]{"--upload", "ftps://test.cyberduck.ch/remote/", String.format("%s/d1", temp),
String.format("%s/d2", temp)});
final Set<TransferItem> found = new SingleTransferItemFinder().find(input, TerminalAction.upload, new Path("/remote", EnumSet.of(Path.Type.directory)));
assertFalse(found.isEmpty());
assertEquals(2, found.size());
final Iterator<TransferItem> iter = found.iterator();
assertEquals(new TransferItem(new Path("/remote/d1", EnumSet.of(Path.Type.directory)), new Local(temp, "d1")), iter.next());
assertEquals(new TransferItem(new Path("/remote/d2", EnumSet.of(Path.Type.directory)), new Local(temp, "d2")), iter.next());
d1.delete();
d2.delete();
}
@Test
public void testUploadDirectoryServerRoot() throws Exception {
final CommandLineParser parser = new PosixParser();
@@ -117,7 +152,7 @@ public class SingleTransferItemFinderTest {
assertFalse(found.isEmpty());
final Iterator<TransferItem> iter = found.iterator();
assertEquals(new TransferItem(new Path("/remote/f", EnumSet.of(Path.Type.file)), LocalFactory.get(String.format("%s/f", temp))),
iter.next());
iter.next());
}
@Test
@@ -130,6 +165,6 @@ public class SingleTransferItemFinderTest {
assertFalse(found.isEmpty());
final Iterator<TransferItem> iter = found.iterator();
assertEquals(new TransferItem(new Path("/remote", EnumSet.of(Path.Type.directory)), LocalFactory.get(String.format("%s/remote", temp))),
iter.next());
iter.next());
}
}
@@ -34,16 +34,17 @@ public class UploadTransferItemFinderTest {
public void testResolveFolderToFolder() {
final Local temp = new TemporarySupportDirectoryFinder().find();
final Path folder = new Path("/d", EnumSet.of(Path.Type.directory));
final TransferItem item = UploadTransferItemFinder.resolve(folder, temp);
assertEquals(folder, item.remote);
assertEquals(temp, item.local);
assertEquals(folder, UploadTransferItemFinder.resolve(folder, temp, false).remote);
assertEquals(temp, UploadTransferItemFinder.resolve(folder, temp, false).local);
assertEquals(new Path(folder, temp.getName(), EnumSet.of(Path.Type.directory)), UploadTransferItemFinder.resolve(folder, temp, true).remote);
assertEquals(temp, UploadTransferItemFinder.resolve(folder, temp, true).local);
}
@Test
public void testResolveFileToFile() {
final Local temp = new FlatTemporaryFileService().create(new AlphanumericRandomStringService().random());
final Path file = new Path("/f", EnumSet.of(Path.Type.file));
final TransferItem item = UploadTransferItemFinder.resolve(file, temp);
final TransferItem item = UploadTransferItemFinder.resolve(file, temp, false);
assertEquals(file, item.remote);
assertEquals(temp, item.local);
}
@@ -52,7 +53,7 @@ public class UploadTransferItemFinderTest {
public void testResolveFileToFolder() {
final Local temp = new FlatTemporaryFileService().create(new AlphanumericRandomStringService().random());
final Path folder = new Path("/d", EnumSet.of(Path.Type.directory));
final TransferItem item = UploadTransferItemFinder.resolve(folder, temp);
final TransferItem item = UploadTransferItemFinder.resolve(folder, temp, false);
assertEquals(new Path("/d/" + temp.getName(), EnumSet.of(Path.Type.file)), item.remote);
assertEquals(temp, item.local);
}