use of ch.cyberduck.core.transfer.CopyTransfer in project cyberduck by iterate-ch.
the class TransferDictionary method deserialize.
public <T> Transfer deserialize(final T serialized) {
final Deserializer dict = factory.create(serialized);
final Object hostObj = dict.objectForKey("Host");
if (null == hostObj) {
log.warn("Missing host in transfer");
return null;
}
final Host host = new HostDictionary(protocols, factory).deserialize(hostObj);
if (null == host) {
log.warn("Invalid host in transfer");
return null;
}
host.setWorkdir(null);
final List<T> itemsObj = dict.listForKey("Items");
final List<TransferItem> roots = new ArrayList<TransferItem>();
if (itemsObj != null) {
for (T rootDict : itemsObj) {
final TransferItem item = new TransferItemDictionary(factory).deserialize(rootDict);
if (null == item) {
log.warn("Invalid item in transfer");
continue;
}
roots.add(item);
}
}
// Legacy
final List<T> rootsObj = dict.listForKey("Roots");
if (rootsObj != null) {
for (T rootDict : rootsObj) {
final Path remote = new PathDictionary(factory).deserialize(rootDict);
if (null == remote) {
log.warn("Invalid remote in transfer");
continue;
}
final TransferItem item = new TransferItem(remote);
// Legacy
final String localObjDeprecated = factory.create(rootDict).stringForKey("Local");
if (localObjDeprecated != null) {
Local local = LocalFactory.get(localObjDeprecated);
item.setLocal(local);
}
final Object localObj = factory.create(rootDict).objectForKey("Local Dictionary");
if (localObj != null) {
Local local = new LocalDictionary(factory).deserialize(localObj);
if (null == local) {
log.warn("Invalid local in transfer item");
continue;
}
item.setLocal(local);
}
roots.add(item);
}
}
if (roots.isEmpty()) {
log.warn("No files in transfer");
return null;
}
final Transfer transfer;
Transfer.Type type = null;
final String kindObj = dict.stringForKey("Kind");
if (kindObj != null) {
// Legacy
type = Transfer.Type.values()[Integer.parseInt(kindObj)];
}
final String typeObj = dict.stringForKey("Type");
if (typeObj != null) {
type = Transfer.Type.valueOf(typeObj);
}
if (null == type) {
log.warn("Missing transfer type");
return null;
}
switch(type) {
case download:
case upload:
case sync:
// Verify we have valid items
for (TransferItem item : roots) {
if (null == item.remote) {
log.warn(String.format("Missing remote in transfer item %s", item));
return null;
}
if (null == item.local) {
log.warn(String.format("Missing local in transfer item %s", item));
return null;
}
}
}
switch(type) {
case download:
transfer = new DownloadTransfer(host, roots);
break;
case upload:
transfer = new UploadTransfer(host, roots);
break;
case sync:
final String actionObj = dict.stringForKey("Action");
if (null == actionObj) {
transfer = new SyncTransfer(host, roots.iterator().next());
} else {
transfer = new SyncTransfer(host, roots.iterator().next(), TransferAction.forName(actionObj));
}
break;
case copy:
Object destinationObj = dict.objectForKey("Destination");
if (null == destinationObj) {
log.warn("Missing destination for copy transfer");
return null;
}
final List<T> destinations = dict.listForKey("Destinations");
if (destinations.isEmpty()) {
log.warn("No destinations in copy transfer");
return null;
}
if (roots.size() == destinations.size()) {
final Map<Path, Path> files = new HashMap<Path, Path>();
for (int i = 0; i < roots.size(); i++) {
final Path target = new PathDictionary(factory).deserialize(destinations.get(i));
if (null == target) {
continue;
}
files.put(roots.get(i).remote, target);
}
final Host target = new HostDictionary(protocols, factory).deserialize(destinationObj);
if (null == target) {
log.warn("Missing target host in copy transfer");
return null;
}
transfer = new CopyTransfer(host, target, files);
} else {
log.warn("Invalid file mapping for copy transfer");
return null;
}
break;
default:
log.warn(String.format("Unknown transfer type %s", kindObj));
return null;
}
final Object uuidObj = dict.stringForKey("UUID");
if (uuidObj != null) {
transfer.setUuid(uuidObj.toString());
}
final Object sizeObj = dict.stringForKey("Size");
if (sizeObj != null) {
transfer.setSize((long) Double.parseDouble(sizeObj.toString()));
}
final Object timestampObj = dict.stringForKey("Timestamp");
if (timestampObj != null) {
transfer.setTimestamp(new Date(Long.parseLong(timestampObj.toString())));
}
final Object currentObj = dict.stringForKey("Current");
if (currentObj != null) {
transfer.setTransferred((long) Double.parseDouble(currentObj.toString()));
}
final Object bandwidthObj = dict.stringForKey("Bandwidth");
if (bandwidthObj != null) {
transfer.getBandwidth().setRate(Float.parseFloat(bandwidthObj.toString()));
}
return transfer;
}
use of ch.cyberduck.core.transfer.CopyTransfer in project cyberduck by iterate-ch.
the class TransferBackgroundActionTest method testCopyBetweenHosts.
@Test
public void testCopyBetweenHosts() throws Exception {
final Session session = new NullTransferSession(new Host(new TestProtocol(), "test.cyberduck.ch"));
final Session destination = new NullTransferSession(new Host(new TestProtocol(), "test.cyberduck.ch"));
final Path directory = new Path("/home/jenkins/transfer", EnumSet.of(Path.Type.directory));
final Path test = new Path(directory, "test", EnumSet.of(Path.Type.file));
test.attributes().setSize(0L);
final Path copy = new Path(new Path("/transfer", EnumSet.of(Path.Type.directory)), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final Transfer t = new CopyTransfer(session.getHost(), destination.getHost(), Collections.singletonMap(test, copy)) {
@Override
public TransferAction action(final Session<?> source, final Session<?> destination, final boolean resumeRequested, final boolean reloadRequested, final TransferPrompt prompt, final ListProgressListener listener) {
return TransferAction.overwrite;
}
};
final AbstractController controller = new AbstractController() {
@Override
public void invoke(final MainAction runnable, final boolean wait) {
runnable.run();
}
};
final AtomicBoolean start = new AtomicBoolean();
final AtomicBoolean stop = new AtomicBoolean();
final TransferBackgroundAction action = new TransferBackgroundAction(controller, new StatelessSessionPool(new TestLoginConnectionService(), session, new DisabledTranscriptListener(), new DefaultVaultRegistry(new DisabledPasswordCallback())), new StatelessSessionPool(new TestLoginConnectionService(), destination, new DisabledTranscriptListener(), new DefaultVaultRegistry(new DisabledPasswordCallback())), new TransferListener() {
@Override
public void transferDidStart(final Transfer transfer) {
assertEquals(t, transfer);
start.set(true);
}
@Override
public void transferDidStop(final Transfer transfer) {
assertEquals(t, transfer);
stop.set(true);
}
@Override
public void transferDidProgress(final Transfer transfer, final TransferProgress status) {
//
}
}, t, new TransferOptions());
action.prepare();
action.call();
action.finish();
assertFalse(action.hasFailed());
assertTrue(start.get());
assertTrue(stop.get());
assertTrue(t.isComplete());
assertNotNull(t.getTimestamp());
}
use of ch.cyberduck.core.transfer.CopyTransfer in project cyberduck by iterate-ch.
the class BrowserController method paste.
@Action
public void paste(final ID sender) {
if (pasteboard.isEmpty()) {
NSPasteboard pboard = NSPasteboard.generalPasteboard();
this.upload(pboard);
} else {
final Map<Path, Path> files = new HashMap<>();
final Path parent = workdir;
for (final Path next : pasteboard) {
Path renamed = new Path(parent, next.getName(), next.getType(), new PathAttributes(next.attributes()));
files.put(next, renamed);
}
pasteboard.clear();
if (pasteboard.isCut()) {
new MoveController(this).rename(files);
}
if (pasteboard.isCopy()) {
new OverwriteController(BrowserController.this).overwrite(new ArrayList<>(files.values()), new DefaultMainAction() {
@Override
public void run() {
transfer(new CopyTransfer(pool.getHost(), pool.getHost(), files), new ArrayList<>(files.values()), true);
}
});
}
}
}
use of ch.cyberduck.core.transfer.CopyTransfer in project cyberduck by iterate-ch.
the class BrowserTableDataSource method acceptDrop.
/**
* @param view Table
* @param destination A directory or null to mount an URL
* @param info Dragging pasteboard
* @return True if accepted
*/
public boolean acceptDrop(final NSTableView view, final Path destination, final NSDraggingInfo info) {
if (log.isDebugEnabled()) {
log.debug(String.format("Accept drop for destination %s", destination));
}
if (info.draggingPasteboard().availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.URLPboardType)) != null) {
final NSObject o = info.draggingPasteboard().propertyListForType(NSPasteboard.URLPboardType);
// Mount .webloc URLs dragged to browser window
if (o != null) {
if (o.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(o, NSArray.class);
for (int i = 0; i < elements.count().intValue(); i++) {
if (Scheme.isURL(elements.objectAtIndex(new NSUInteger(i)).toString())) {
try {
controller.mount(HostParser.parse(elements.objectAtIndex(new NSUInteger(i)).toString()));
} catch (HostParserException e) {
log.warn(e);
continue;
}
return true;
}
}
}
}
}
if (controller.isMounted()) {
if (info.draggingPasteboard().availableTypeFromArray(NSArray.arrayWithObject(NSPasteboard.FilenamesPboardType)) != null) {
final NSObject o = info.draggingPasteboard().propertyListForType(NSPasteboard.FilenamesPboardType);
// A file drag has been received by another application; upload to the dragged directory
if (o != null) {
if (o.isKindOfClass(NSArray.CLASS)) {
final NSArray elements = Rococoa.cast(o, NSArray.class);
final List<TransferItem> roots = new ArrayList<TransferItem>();
for (int i = 0; i < elements.count().intValue(); i++) {
final Local local = LocalFactory.get(elements.objectAtIndex(new NSUInteger(i)).toString());
roots.add(new TransferItem(new Path(destination, local.getName(), local.isDirectory() ? EnumSet.of(Path.Type.directory) : EnumSet.of(Path.Type.file)), local));
}
controller.transfer(new UploadTransfer(controller.getSession().getHost(), roots));
return true;
}
}
return false;
}
final List<PathPasteboard> pasteboards = PathPasteboardFactory.allPasteboards();
for (PathPasteboard pasteboard : pasteboards) {
// A file dragged within the browser has been received
if (pasteboard.isEmpty()) {
continue;
}
final Map<Path, Path> files = new HashMap<Path, Path>();
for (Path next : pasteboard) {
final Path renamed = new Path(destination, next.getName(), next.getType(), new PathAttributes(next.attributes()).withVersionId(null));
files.put(next, renamed);
}
if (pasteboard.getBookmark().compareTo(controller.getSession().getHost()) != 0) {
// Drag to browser windows with different session or explicit copy requested by user.
final Host target = controller.getSession().getHost();
controller.transfer(new CopyTransfer(pasteboard.getBookmark(), target, files), new ArrayList<Path>(files.values()), false);
} else if (info.draggingSourceOperationMask().intValue() == NSDraggingInfo.NSDragOperationCopy.intValue()) {
// The file should be copied
new CopyController(controller).copy(files);
} else {
// The file should be renamed
new MoveController(controller).rename(files);
}
pasteboard.clear();
}
return true;
}
return false;
}
use of ch.cyberduck.core.transfer.CopyTransfer in project cyberduck by iterate-ch.
the class TransferBackgroundActionTest method testDuplicate.
@Test
public void testDuplicate() throws Exception {
final Host host = new Host(new TestProtocol(), "test.cyberduck.ch") {
@Override
public Credentials getCredentials() {
return new Credentials(System.getProperties().getProperty("sftp.user"), System.getProperties().getProperty("sftp.password"));
}
};
final Path directory = new Path("/home/jenkins/transfer", EnumSet.of(Path.Type.directory));
final Path test = new Path(directory, "test", EnumSet.of(Path.Type.file));
test.attributes().setSize(0L);
final Path copy = new Path(directory, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final CopyTransfer t = new CopyTransfer(host, host, Collections.singletonMap(test, copy)) {
@Override
public TransferAction action(final Session<?> source, final Session<?> destination, final boolean resumeRequested, final boolean reloadRequested, final TransferPrompt prompt, final ListProgressListener listener) {
return TransferAction.overwrite;
}
};
final AbstractController controller = new AbstractController() {
@Override
public void invoke(final MainAction runnable, final boolean wait) {
runnable.run();
}
};
final AtomicBoolean start = new AtomicBoolean();
final AtomicBoolean stop = new AtomicBoolean();
final Session session = new NullTransferSession(host);
final Session destination = new NullTransferSession(host);
final TransferBackgroundAction action = new TransferBackgroundAction(controller, new StatelessSessionPool(new TestLoginConnectionService(), session, new DisabledTranscriptListener(), new DefaultVaultRegistry(new DisabledPasswordCallback())), new StatelessSessionPool(new TestLoginConnectionService(), destination, new DisabledTranscriptListener(), new DefaultVaultRegistry(new DisabledPasswordCallback())), new TransferListener() {
@Override
public void transferDidStart(final Transfer transfer) {
assertEquals(t, transfer);
start.set(true);
}
@Override
public void transferDidStop(final Transfer transfer) {
assertEquals(t, transfer);
stop.set(true);
}
@Override
public void transferDidProgress(final Transfer transfer, final TransferProgress status) {
//
}
}, t, new TransferOptions());
action.prepare();
action.call();
assertTrue(destination.isConnected());
action.finish();
assertFalse(action.hasFailed());
assertTrue(start.get());
assertTrue(stop.get());
assertTrue(t.isComplete());
assertNotNull(t.getTimestamp());
}
Aggregations