Search in sources :

Example 1 with B2FileResponse

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) AsciiRandomStringService(ch.cyberduck.core.AsciiRandomStringService) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) AttributedList(ch.cyberduck.core.AttributedList) ByteArrayInputStream(java.io.ByteArrayInputStream) SHA1ChecksumCompute(ch.cyberduck.core.io.SHA1ChecksumCompute) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) SimplePathPredicate(ch.cyberduck.core.SimplePathPredicate) HttpResponseOutputStream(ch.cyberduck.core.http.HttpResponseOutputStream) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) B2FileResponse(synapticloop.b2.response.B2FileResponse) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 2 with B2FileResponse

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);
}
Also used : DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) HashMap(java.util.HashMap) B2GetUploadUrlResponse(synapticloop.b2.response.B2GetUploadUrlResponse) B2ApiException(synapticloop.b2.exception.B2ApiException) IOException(java.io.IOException) DelayedHttpEntityCallable(ch.cyberduck.core.http.DelayedHttpEntityCallable) B2GetUploadPartUrlResponse(synapticloop.b2.response.B2GetUploadPartUrlResponse) Checksum(ch.cyberduck.core.io.Checksum) BaseB2Response(synapticloop.b2.response.BaseB2Response) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) B2FileResponse(synapticloop.b2.response.B2FileResponse)

Example 3 with B2FileResponse

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);
    }
}
Also used : DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) B2ApiException(synapticloop.b2.exception.B2ApiException) IOException(java.io.IOException) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) B2FileResponse(synapticloop.b2.response.B2FileResponse)

Example 4 with B2FileResponse

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());
}
Also used : Path(ch.cyberduck.core.Path) Delete(ch.cyberduck.core.features.Delete) B2ReadFeature(ch.cyberduck.core.b2.B2ReadFeature) DisabledStreamListener(ch.cyberduck.core.io.DisabledStreamListener) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) B2VersionIdProvider(ch.cyberduck.core.b2.B2VersionIdProvider) B2WriteFeature(ch.cyberduck.core.b2.B2WriteFeature) Local(ch.cyberduck.core.Local) B2TouchFeature(ch.cyberduck.core.b2.B2TouchFeature) Random(java.util.Random) BandwidthThrottle(ch.cyberduck.core.io.BandwidthThrottle) B2DeleteFeature(ch.cyberduck.core.b2.B2DeleteFeature) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) HttpResponseOutputStream(ch.cyberduck.core.http.HttpResponseOutputStream) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) B2FileResponse(synapticloop.b2.response.B2FileResponse) AbstractB2Test(ch.cyberduck.core.b2.AbstractB2Test) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 5 with B2FileResponse

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());
}
Also used : Write(ch.cyberduck.core.features.Write) Delete(ch.cyberduck.core.features.Delete) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) SHA1ChecksumCompute(ch.cyberduck.core.io.SHA1ChecksumCompute) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) B2FileResponse(synapticloop.b2.response.B2FileResponse) Path(ch.cyberduck.core.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PathAttributes(ch.cyberduck.core.PathAttributes) ByteArrayInputStream(java.io.ByteArrayInputStream) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) BaseB2Response(synapticloop.b2.response.BaseB2Response) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Aggregations

B2FileResponse (synapticloop.b2.response.B2FileResponse)6 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)5 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)4 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)4 Path (ch.cyberduck.core.Path)4 Delete (ch.cyberduck.core.features.Delete)4 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)4 IntegrationTest (ch.cyberduck.test.IntegrationTest)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BaseB2Response (synapticloop.b2.response.BaseB2Response)3 AsciiRandomStringService (ch.cyberduck.core.AsciiRandomStringService)2 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)2 SimplePathPredicate (ch.cyberduck.core.SimplePathPredicate)2 HttpResponseOutputStream (ch.cyberduck.core.http.HttpResponseOutputStream)2 SHA1ChecksumCompute (ch.cyberduck.core.io.SHA1ChecksumCompute)2 StreamCopier (ch.cyberduck.core.io.StreamCopier)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 B2ApiException (synapticloop.b2.exception.B2ApiException)2