use of synapticloop.b2.response.B2FileResponse 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.B2FileResponse in project cyberduck by iterate-ch.
the class B2WriteFeature method write.
@Override
public HttpResponseOutputStream<BaseB2Response> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
// Submit store call to background thread
final DelayedHttpEntityCallable<BaseB2Response> command = new DelayedHttpEntityCallable<BaseB2Response>() {
/**
* @return The SHA-1 returned by the server for the uploaded object
*/
@Override
public BaseB2Response call(final AbstractHttpEntity entity) throws BackgroundException {
try {
final Checksum checksum = status.getChecksum();
if (status.isSegment()) {
final B2GetUploadPartUrlResponse uploadUrl = session.getClient().getUploadPartUrl(status.getParameters().get("fileId"));
return session.getClient().uploadLargeFilePart(uploadUrl, status.getPart(), entity, checksum.hash);
} else {
if (null == urls.get()) {
final B2GetUploadUrlResponse uploadUrl = session.getClient().getUploadUrl(fileid.getVersionId(containerService.getContainer(file), new DisabledListProgressListener()));
if (log.isDebugEnabled()) {
log.debug(String.format("Obtained upload URL %s for file %s", uploadUrl, file));
}
urls.set(uploadUrl);
return this.upload(uploadUrl, entity, checksum);
} else {
final B2GetUploadUrlResponse uploadUrl = urls.get();
if (log.isDebugEnabled()) {
log.debug(String.format("Use cached upload URL %s for file %s", uploadUrl, file));
}
try {
return this.upload(uploadUrl, entity, checksum);
} catch (IOException | B2ApiException e) {
// Upload many files to the same upload_url until that URL gives an error
log.warn(String.format("Remove cached upload URL after failure %s", e));
urls.remove();
// Retry
return this.upload(uploadUrl, entity, checksum);
}
}
}
} 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);
}
}
private BaseB2Response upload(final B2GetUploadUrlResponse uploadUrl, final AbstractHttpEntity entity, final Checksum checksum) throws B2ApiException, IOException {
final Map<String, String> fileinfo = new HashMap<>(status.getMetadata());
if (null != status.getTimestamp()) {
fileinfo.put(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS, String.valueOf(status.getTimestamp()));
}
final B2FileResponse response = session.getClient().uploadFile(uploadUrl, containerService.getKey(file), entity, checksum.algorithm == HashAlgorithm.sha1 ? checksum.hash : "do_not_verify", status.getMime(), fileinfo);
fileid.cache(file, response.getFileId());
return response;
}
@Override
public long getContentLength() {
return status.getLength();
}
};
return this.write(file, status, command);
}
use of synapticloop.b2.response.B2FileResponse in project cyberduck by iterate-ch.
the class B2CopyFeature method copy.
@Override
public Path copy(final Path source, final Path target, final TransferStatus status, final ConnectionCallback callback, final StreamListener listener) throws BackgroundException {
try {
final B2FileResponse response = session.getClient().copyFile(fileid.getVersionId(source, new DisabledListProgressListener()), fileid.getVersionId(containerService.getContainer(target), new DisabledListProgressListener()), containerService.getKey(target));
listener.sent(status.getLength());
fileid.cache(target, response.getFileId());
return target.withAttributes(new B2AttributesFinderFeature(session, fileid).toAttributes(response));
} catch (B2ApiException e) {
throw new B2ExceptionMappingService(fileid).map("Cannot copy {0}", e, source);
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
use of synapticloop.b2.response.B2FileResponse in project cyberduck by iterate-ch.
the class DefaultDownloadFeatureTest method testOverwrite.
@Test
public void testOverwrite() throws Exception {
final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final Path test = new B2TouchFeature(session, fileid).touch(new Path(bucket, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus());
final byte[] content = new byte[39864];
new Random().nextBytes(content);
{
final TransferStatus status = new TransferStatus().withLength(content.length);
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(status, status).withLimit(new Long(content.length)).transfer(new ByteArrayInputStream(content), out);
out.close();
test.attributes().setVersionId(((B2FileResponse) out.getStatus()).getFileId());
}
final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
final TransferStatus status = new TransferStatus().withLength(content.length);
new DefaultDownloadFeature(new B2ReadFeature(session, fileid)).download(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback());
final byte[] buffer = new byte[content.length];
final InputStream in = local.getInputStream();
IOUtils.readFully(in, buffer);
in.close();
assertArrayEquals(content, buffer);
new B2DeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
use of synapticloop.b2.response.B2FileResponse in project cyberduck by iterate-ch.
the class B2WriteFeatureTest method testWrite.
@Test
public void testWrite() throws Exception {
final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path test = new Path(bucket, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final TransferStatus status = new TransferStatus();
final byte[] content = RandomUtils.nextBytes(4);
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
status.setTimestamp(1503654614004L);
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final StatusOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
final BaseB2Response response = out.getStatus();
assertTrue(response instanceof B2FileResponse);
assertNotNull(test.attributes().getVersionId());
assertEquals(((B2FileResponse) response).getFileId(), test.attributes().getVersionId());
assertEquals(test.attributes().getVersionId(), fileid.getVersionId(test, new DisabledListProgressListener()));
assertTrue(new B2FindFeature(session, fileid).find(test));
final PathAttributes attributes = new B2ListService(session, fileid).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes();
assertEquals(content.length, attributes.getSize());
final Write.Append append = new B2WriteFeature(session, fileid).append(test, status.withRemote(attributes));
assertFalse(append.append);
assertEquals(content.length, append.size, 0L);
final byte[] buffer = new byte[content.length];
final InputStream in = new B2ReadFeature(session, fileid).read(test, new TransferStatus(), new DisabledConnectionCallback());
IOUtils.readFully(in, buffer);
in.close();
assertArrayEquals(content, buffer);
assertEquals(1503654614004L, new B2AttributesFinderFeature(session, fileid).find(test).getModificationDate());
final byte[] overwriteContent = RandomUtils.nextBytes(5);
final StatusOutputStream<BaseB2Response> overwrite = new B2WriteFeature(session, fileid).write(test, new TransferStatus().exists(true).withLength(overwriteContent.length), new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(overwriteContent), overwrite);
assertNotEquals(((B2FileResponse) response).getFileId(), ((B2FileResponse) overwrite.getStatus()).getFileId());
new B2DeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Aggregations