use of ch.cyberduck.core.transfer.UploadTransfer in project cyberduck by iterate-ch.
the class CryptoB2SingleTransferWorkerTest method testUpload.
@Test
public void testUpload() throws Exception {
final Path home = new Path("/test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path dir1 = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Local localDirectory1 = new Local(System.getProperty("java.io.tmpdir"), new AlphanumericRandomStringService().random());
new DefaultLocalDirectoryFeature().mkdir(localDirectory1);
final byte[] content = RandomUtils.nextBytes(62768);
final Path file1 = new Path(dir1, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final Local localFile1 = new Local(localDirectory1, file1.getName());
final OutputStream out1 = localFile1.getOutputStream(false);
IOUtils.write(content, out1);
out1.close();
final Path file2 = new Path(dir1, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final Local localFile2 = new Local(localDirectory1, file2.getName());
final OutputStream out2 = localFile2.getOutputStream(false);
IOUtils.write(content, out2);
out2.close();
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final Transfer t = new UploadTransfer(new Host(new TestProtocol()), Collections.singletonList(new TransferItem(dir1, localDirectory1)), new NullFilter<>());
assertTrue(new SingleTransferWorker(session, session, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledLoginCallback(), new DisabledNotificationService()) {
}.run(session));
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
assertTrue(new CryptoFindFeature(session, new B2FindFeature(session, fileid), cryptomator).find(dir1));
assertEquals(content.length, new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(file1).getSize());
{
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(file1, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
}
assertEquals(content.length, new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(file2).getSize());
{
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(file1, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
}
cryptomator.getFeature(session, Delete.class, new B2DeleteFeature(session, fileid)).delete(Arrays.asList(file1, file2, dir1, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
localFile1.delete();
localFile2.delete();
localDirectory1.delete();
}
use of ch.cyberduck.core.transfer.UploadTransfer 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.UploadTransfer in project cyberduck by iterate-ch.
the class SingleTransferWorkerTest method testUploadPrepareOverrideRootDoesNotExist.
@Test
public void testUploadPrepareOverrideRootDoesNotExist() throws Exception {
final Path child = new Path("/t/c", EnumSet.of(Path.Type.file));
final Path root = new Path("/t", EnumSet.of(Path.Type.directory)) {
@Override
public Path getParent() {
return new Path("/", EnumSet.of(Path.Type.directory));
}
};
final NullLocal local = new NullLocal("l") {
@Override
public AttributedList<Local> list() {
AttributedList<Local> l = new AttributedList<Local>();
l.add(new NullLocal(this.getAbsolute(), "c") {
@Override
public boolean exists() {
return true;
}
});
return l;
}
@Override
public boolean exists() {
return true;
}
};
final Transfer t = new UploadTransfer(new Host(new TestProtocol()), root, local) {
@Override
public void transfer(final Session<?> source, final Session<?> destination, final Path file, Local local, final TransferOptions options, final TransferStatus overall, final TransferStatus segment, final ConnectionCallback connectionCallback, final ProgressListener listener, final StreamListener streamListener) {
//
}
};
final NullSession session = new NullSession(new Host(new TestProtocol()));
final SingleTransferWorker worker = new SingleTransferWorker(session, session, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledLoginCallback(), new DisabledNotificationService()) {
@Override
public Future<TransferStatus> transfer(final TransferItem item, final TransferAction action) throws BackgroundException {
if (item.remote.equals(root)) {
assertTrue(this.getCache().isCached(new TransferItem(root, local)));
}
super.transfer(new TransferItem(item.remote, new NullLocal("l") {
@Override
public AttributedList<Local> list() {
AttributedList<Local> l = new AttributedList<Local>();
l.add(new NullLocal(this.getAbsolute(), "c"));
return l;
}
}), action);
assertFalse(this.getCache().isCached(new TransferItem(child, local)));
return null;
}
};
worker.run(session);
assertFalse(worker.getCache().isCached(new TransferItem(child, local)));
}
use of ch.cyberduck.core.transfer.UploadTransfer in project cyberduck by iterate-ch.
the class ConcurrentTransferWorkerTest method testAwait.
@Test
public void testAwait() throws Exception {
final Host host = new Host(new TestProtocol(), "localhost", new Credentials("u", "p"));
final Transfer transfer = new UploadTransfer(host, new Path("/t", EnumSet.of(Path.Type.directory)), new NullLocal("l"));
final LoginConnectionService connection = new TestLoginConnectionService();
final ConcurrentTransferWorker worker = new ConcurrentTransferWorker(new DefaultSessionPool(connection, new DisabledX509TrustManager(), new DefaultX509KeyManager(), new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), host), SessionPool.DISCONNECTED, transfer, new TransferOptions(), new TransferSpeedometer(transfer), new DisabledTransferPrompt(), new DisabledTransferErrorCallback(), new DisabledLoginCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledNotificationService());
int workers = 1000;
final CountDownLatch entry = new CountDownLatch(workers);
for (int i = 0; i < workers; i++) {
worker.submit(new TransferWorker.TransferCallable() {
@Override
public TransferStatus call() {
entry.countDown();
return new TransferStatus().complete();
}
});
}
worker.await();
assertEquals(0, entry.getCount());
worker.cleanup(true);
}
use of ch.cyberduck.core.transfer.UploadTransfer in project cyberduck by iterate-ch.
the class ConcurrentTransferWorkerTest method testDoubleRelease.
@Test
public void testDoubleRelease() throws Exception {
final Host host = new Host(new TestProtocol(), "test.cyberduck.ch");
final Transfer t = new UploadTransfer(host, new Path("/t", EnumSet.of(Path.Type.directory)), new NullLocal("l"));
final LoginConnectionService connection = new TestLoginConnectionService();
final DefaultSessionPool pool = new DefaultSessionPool(connection, new DisabledX509TrustManager(), new DefaultX509KeyManager(), new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), host);
final ConcurrentTransferWorker worker = new ConcurrentTransferWorker(pool, SessionPool.DISCONNECTED, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt(), new DisabledTransferErrorCallback(), new DisabledConnectionCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledNotificationService());
final Session<?> session = worker.borrow(ConcurrentTransferWorker.Connection.source);
worker.release(session, ConcurrentTransferWorker.Connection.source, null);
worker.release(session, ConcurrentTransferWorker.Connection.source, null);
}
Aggregations