use of synapticloop.b2.response.BaseB2Response in project cyberduck by iterate-ch.
the class B2WriteFeatureTest method testWrite.
@Test
public void testWrite() throws Exception {
final TransferStatus status = new TransferStatus();
final int length = 1048576;
final byte[] content = RandomUtils.nextBytes(length);
status.setLength(content.length);
final Path home = new Path("/test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final CryptoVault cryptomator = new CryptoVault(new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)));
final Path vault = cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
final Path test = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final CryptoWriteFeature<BaseB2Response> writer = new CryptoWriteFeature<BaseB2Response>(session, new B2WriteFeature(session, fileid), cryptomator);
final FileHeader header = cryptomator.getFileHeaderCryptor().create();
status.setHeader(cryptomator.getFileHeaderCryptor().encryptHeader(header));
status.setNonces(new RotatingNonceGenerator(cryptomator.numberOfChunks(content.length)));
status.setChecksum(writer.checksum(test, status).compute(new ByteArrayInputStream(content), status));
final OutputStream out = writer.write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(status, status).transfer(new ByteArrayInputStream(content), out);
out.close();
assertTrue(new CryptoFindFeature(session, new B2FindFeature(session, fileid), cryptomator).find(test));
final PathAttributes attributes = new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(test);
assertEquals(content.length, attributes.getSize());
assertEquals(content.length, writer.append(test, status.withRemote(attributes)).size, 0L);
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(test, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(status, status).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
cryptomator.getFeature(session, Delete.class, new B2DeleteFeature(session, fileid)).delete(Arrays.asList(test, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of synapticloop.b2.response.BaseB2Response in project cyberduck by iterate-ch.
the class DefaultFindFeatureTest method testFindLargeUpload.
@Test
public void testFindLargeUpload() throws Exception {
final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path file = new Path(bucket, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final StatusOutputStream<BaseB2Response> out = new B2LargeUploadWriteFeature(session, new B2VersionIdProvider(session)).write(file, new TransferStatus(), new DisabledConnectionCallback());
IOUtils.copyLarge(new ByteArrayInputStream(RandomUtils.nextBytes(100)), out);
out.close();
assertTrue(new DefaultFindFeature(session).find(file));
}
use of synapticloop.b2.response.BaseB2Response in project cyberduck by iterate-ch.
the class B2LargeUploadService method upload.
@Override
public BaseB2Response upload(final Path file, final Local local, final BandwidthThrottle throttle, final StreamListener listener, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final ThreadPool pool = ThreadPoolFactory.get("largeupload", concurrency);
try {
// Get the results of the uploads in the order they were submitted
// this is important for building the manifest, and is not a problem in terms of performance
// because we should only continue when all segments have uploaded successfully
final List<B2UploadPartResponse> completed = new ArrayList<>();
final Map<String, String> fileinfo = new HashMap<>(status.getMetadata());
final Checksum checksum = status.getChecksum();
if (Checksum.NONE != checksum) {
switch(checksum.algorithm) {
case sha1:
fileinfo.put(X_BZ_INFO_LARGE_FILE_SHA1, status.getChecksum().hash);
break;
}
}
if (null != status.getTimestamp()) {
fileinfo.put(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS, String.valueOf(status.getTimestamp()));
}
final String fileId;
if (status.isAppend()) {
// Add already completed parts
final B2LargeUploadPartService partService = new B2LargeUploadPartService(session, fileid);
final List<B2FileInfoResponse> uploads = partService.find(file);
if (uploads.isEmpty()) {
fileId = session.getClient().startLargeFileUpload(fileid.getVersionId(containerService.getContainer(file), new DisabledListProgressListener()), containerService.getKey(file), status.getMime(), fileinfo).getFileId();
} else {
fileId = uploads.iterator().next().getFileId();
completed.addAll(partService.list(fileId));
}
} else {
fileId = session.getClient().startLargeFileUpload(fileid.getVersionId(containerService.getContainer(file), new DisabledListProgressListener()), containerService.getKey(file), status.getMime(), fileinfo).getFileId();
}
// Full size of file
final long size = status.getLength() + status.getOffset();
// Submit file segments for concurrent upload
final List<Future<B2UploadPartResponse>> parts = new ArrayList<>();
long remaining = status.getLength();
long offset = 0;
for (int partNumber = 1; remaining > 0; partNumber++) {
boolean skip = false;
if (status.isAppend()) {
if (log.isInfoEnabled()) {
log.info(String.format("Determine if part number %d can be skipped", partNumber));
}
for (B2UploadPartResponse c : completed) {
if (c.getPartNumber().equals(partNumber)) {
if (log.isInfoEnabled()) {
log.info(String.format("Skip completed part number %d", partNumber));
}
skip = true;
offset += c.getContentLength();
break;
}
}
}
if (!skip) {
final long length = Math.min(Math.max((size / B2LargeUploadService.MAXIMUM_UPLOAD_PARTS), partSize), remaining);
// Submit to queue
parts.add(this.submit(pool, file, local, throttle, listener, status, fileId, partNumber, offset, length, callback));
if (log.isDebugEnabled()) {
log.debug(String.format("Part %s submitted with size %d and offset %d", partNumber, length, offset));
}
remaining -= length;
offset += length;
}
}
try {
for (Future<B2UploadPartResponse> f : parts) {
completed.add(f.get());
}
} catch (InterruptedException e) {
log.error("Part upload failed with interrupt failure");
status.setCanceled();
throw new ConnectionCanceledException(e);
} catch (ExecutionException e) {
log.warn(String.format("Part upload failed with execution failure %s", e.getMessage()));
if (e.getCause() instanceof BackgroundException) {
throw (BackgroundException) e.getCause();
}
throw new DefaultExceptionMappingService().map(e.getCause());
}
completed.sort(new Comparator<B2UploadPartResponse>() {
@Override
public int compare(final B2UploadPartResponse o1, final B2UploadPartResponse o2) {
return o1.getPartNumber().compareTo(o2.getPartNumber());
}
});
final List<String> checksums = new ArrayList<>();
for (B2UploadPartResponse part : completed) {
checksums.add(part.getContentSha1());
}
final B2FinishLargeFileResponse response = session.getClient().finishLargeFileUpload(fileId, checksums.toArray(new String[checksums.size()]));
if (log.isInfoEnabled()) {
log.info(String.format("Finished large file upload %s with %d parts", file, completed.size()));
}
fileid.cache(file, response.getFileId());
// Mark parent status as complete
status.withResponse(new B2AttributesFinderFeature(session, fileid).toAttributes(response)).setComplete();
return response;
} catch (B2ApiException e) {
throw new B2ExceptionMappingService(fileid).map("Upload {0} failed", e, file);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map("Upload {0} failed", e, file);
} finally {
pool.shutdown(false);
}
}
use of synapticloop.b2.response.BaseB2Response in project cyberduck by iterate-ch.
the class B2ObjectListServiceTest method testListRevisions.
@Test
public void testListRevisions() throws Exception {
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final Path bucket = new B2DirectoryFeature(session, fileid).mkdir(new Path(String.format("test-%s", new AsciiRandomStringService().random()), EnumSet.of(Path.Type.directory, Path.Type.volume)), new TransferStatus());
final String name = new AsciiRandomStringService().random();
final Path file1 = new Path(bucket, name, EnumSet.of(Path.Type.file));
final Path file2 = new Path(bucket, name, EnumSet.of(Path.Type.file));
{
final byte[] content = RandomUtils.nextBytes(1);
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(file1, status, new DisabledConnectionCallback());
IOUtils.write(content, out);
out.close();
final B2FileResponse resopnse = (B2FileResponse) out.getStatus();
final AttributedList<Path> list = new B2ObjectListService(session, fileid).list(bucket, new DisabledListProgressListener());
file1.attributes().setVersionId(resopnse.getFileId());
assertTrue(list.contains(file1));
assertEquals(Long.valueOf(1L), list.find(new SimplePathPredicate(file1)).attributes().getRevision());
assertEquals(content.length, list.find(new SimplePathPredicate(file1)).attributes().getSize());
assertEquals(bucket, list.find(new SimplePathPredicate(file1)).getParent());
}
// Replace
{
final byte[] content = RandomUtils.nextBytes(1);
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(file2, status, new DisabledConnectionCallback());
IOUtils.write(content, out);
out.close();
final B2FileResponse resopnse = (B2FileResponse) out.getStatus();
final AttributedList<Path> list = new B2ObjectListService(session, fileid).list(bucket, new DisabledListProgressListener());
file2.attributes().setVersionId(resopnse.getFileId());
assertTrue(list.contains(file2));
assertEquals(Long.valueOf(1L), list.get(file2).attributes().getRevision());
assertFalse(list.get(file2).attributes().isDuplicate());
assertTrue(list.contains(file1));
assertEquals(Long.valueOf(2L), list.get(file1).attributes().getRevision());
assertTrue(list.get(file1).attributes().isDuplicate());
assertEquals(bucket, list.get(file1).getParent());
}
new B2DeleteFeature(session, fileid).delete(Arrays.asList(file1, file2), new DisabledLoginCallback(), new Delete.DisabledCallback());
{
final AttributedList<Path> list = new B2ObjectListService(session, fileid).list(bucket, new DisabledListProgressListener());
assertNull(list.find(new SimplePathPredicate(file1)));
assertNull(list.find(new SimplePathPredicate(file2)));
}
new B2DeleteFeature(session, fileid).delete(Collections.singletonList(bucket), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of synapticloop.b2.response.BaseB2Response in project cyberduck by iterate-ch.
the class B2ReadFeatureTest method testReadInterrupt.
@Test
public void testReadInterrupt() throws Exception {
final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path file = new Path(bucket, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final byte[] content = RandomUtils.nextBytes(923);
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(file, status, new DisabledConnectionCallback());
IOUtils.write(content, out);
out.close();
{
// Unknown length in status
// Read a single byte
final InputStream in = new B2ReadFeature(session, fileid).read(file, new TransferStatus(), new DisabledConnectionCallback());
assertNotNull(in.read());
in.close();
}
{
final InputStream in = new B2ReadFeature(session, fileid).read(file, new TransferStatus(), new DisabledConnectionCallback());
assertNotNull(in);
in.close();
}
new B2DeleteFeature(session, fileid).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Aggregations