Search in sources :

Example 1 with BaseB2Response

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());
}
Also used : Delete(ch.cyberduck.core.features.Delete) B2ReadFeature(ch.cyberduck.core.b2.B2ReadFeature) CryptoWriteFeature(ch.cyberduck.core.cryptomator.features.CryptoWriteFeature) B2VersionIdProvider(ch.cyberduck.core.b2.B2VersionIdProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) B2AttributesFinderFeature(ch.cyberduck.core.b2.B2AttributesFinderFeature) RotatingNonceGenerator(ch.cyberduck.core.cryptomator.random.RotatingNonceGenerator) B2DeleteFeature(ch.cyberduck.core.b2.B2DeleteFeature) DefaultVaultRegistry(ch.cyberduck.core.vault.DefaultVaultRegistry) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) CryptoReadFeature(ch.cyberduck.core.cryptomator.features.CryptoReadFeature) FileHeader(org.cryptomator.cryptolib.api.FileHeader) Path(ch.cyberduck.core.Path) VaultCredentials(ch.cyberduck.core.vault.VaultCredentials) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PathAttributes(ch.cyberduck.core.PathAttributes) B2WriteFeature(ch.cyberduck.core.b2.B2WriteFeature) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CryptoFindFeature(ch.cyberduck.core.cryptomator.features.CryptoFindFeature) CryptoAttributesFeature(ch.cyberduck.core.cryptomator.features.CryptoAttributesFeature) ByteArrayInputStream(java.io.ByteArrayInputStream) B2FindFeature(ch.cyberduck.core.b2.B2FindFeature) DisabledLoginCallback(ch.cyberduck.core.DisabledLoginCallback) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) BaseB2Response(synapticloop.b2.response.BaseB2Response) DisabledPasswordStore(ch.cyberduck.core.DisabledPasswordStore) DisabledPasswordCallback(ch.cyberduck.core.DisabledPasswordCallback) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) StreamCopier(ch.cyberduck.core.io.StreamCopier) AbstractB2Test(ch.cyberduck.core.b2.AbstractB2Test) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 2 with BaseB2Response

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));
}
Also used : Path(ch.cyberduck.core.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) B2VersionIdProvider(ch.cyberduck.core.b2.B2VersionIdProvider) TransferStatus(ch.cyberduck.core.transfer.TransferStatus) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) B2LargeUploadWriteFeature(ch.cyberduck.core.b2.B2LargeUploadWriteFeature) BaseB2Response(synapticloop.b2.response.BaseB2Response) DisabledConnectionCallback(ch.cyberduck.core.DisabledConnectionCallback) AbstractB2Test(ch.cyberduck.core.b2.AbstractB2Test) Test(org.junit.Test) IntegrationTest(ch.cyberduck.test.IntegrationTest)

Example 3 with BaseB2Response

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);
    }
}
Also used : B2FinishLargeFileResponse(synapticloop.b2.response.B2FinishLargeFileResponse) DisabledListProgressListener(ch.cyberduck.core.DisabledListProgressListener) HashMap(java.util.HashMap) ThreadPool(ch.cyberduck.core.threading.ThreadPool) ArrayList(java.util.ArrayList) Checksum(ch.cyberduck.core.io.Checksum) B2UploadPartResponse(synapticloop.b2.response.B2UploadPartResponse) ExecutionException(java.util.concurrent.ExecutionException) B2FileInfoResponse(synapticloop.b2.response.B2FileInfoResponse) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) B2ApiException(synapticloop.b2.exception.B2ApiException) IOException(java.io.IOException) Future(java.util.concurrent.Future) DefaultExceptionMappingService(ch.cyberduck.core.worker.DefaultExceptionMappingService) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Example 4 with BaseB2Response

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());
}
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 5 with BaseB2Response

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

Aggregations

Path (ch.cyberduck.core.Path)12 TransferStatus (ch.cyberduck.core.transfer.TransferStatus)12 IntegrationTest (ch.cyberduck.test.IntegrationTest)12 Test (org.junit.Test)12 DisabledConnectionCallback (ch.cyberduck.core.DisabledConnectionCallback)11 BaseB2Response (synapticloop.b2.response.BaseB2Response)11 DisabledLoginCallback (ch.cyberduck.core.DisabledLoginCallback)10 Delete (ch.cyberduck.core.features.Delete)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 DisabledListProgressListener (ch.cyberduck.core.DisabledListProgressListener)6 InputStream (java.io.InputStream)6 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)5 B2FileResponse (synapticloop.b2.response.B2FileResponse)5 AbstractB2Test (ch.cyberduck.core.b2.AbstractB2Test)4 B2VersionIdProvider (ch.cyberduck.core.b2.B2VersionIdProvider)4 SHA1ChecksumCompute (ch.cyberduck.core.io.SHA1ChecksumCompute)4 StreamCopier (ch.cyberduck.core.io.StreamCopier)4 Local (ch.cyberduck.core.Local)3 B2DeleteFeature (ch.cyberduck.core.b2.B2DeleteFeature)3 B2WriteFeature (ch.cyberduck.core.b2.B2WriteFeature)3