Search in sources :

Example 1 with DownloadBuilder

use of com.dropbox.core.v2.files.DownloadBuilder in project cyberduck by iterate-ch.

the class DropboxReadFeature method read.

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
    try {
        final DownloadBuilder builder = new DbxUserFilesRequests(session.getClient(file)).downloadBuilder(containerService.getKey(file));
        if (status.isAppend()) {
            final HttpRange range = HttpRange.withStatus(status);
            builder.range(range.getStart());
        }
        final DbxDownloader<FileMetadata> downloader = builder.start();
        return downloader.getInputStream();
    } catch (DbxException e) {
        throw new DropboxExceptionMappingService().map("Download {0} failed", e, file);
    }
}
Also used : DownloadBuilder(com.dropbox.core.v2.files.DownloadBuilder) DbxUserFilesRequests(com.dropbox.core.v2.files.DbxUserFilesRequests) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxException(com.dropbox.core.DbxException) HttpRange(ch.cyberduck.core.http.HttpRange)

Example 2 with DownloadBuilder

use of com.dropbox.core.v2.files.DownloadBuilder in project XR3Player by goxr3plus.

the class DownloadService method createTask.

@Override
protected Task<Boolean> createTask() {
    return new Task<Boolean>() {

        @Override
        protected Boolean call() throws Exception {
            try {
                // Create the Client
                client = new DbxClientV2(config, dropBoxViewer.getAccessToken());
                // Try to download the File
                downloadFile(client, dropboxFile, localFileAbsolutePath);
                // Show message to the User
                Platform.runLater(() -> AlertTool.showNotification("Download completed", "Completed downloading " + (!dropboxFile.isDirectory() ? "File" : FOLDER) + " :\n[ " + dropboxFile.getMetadata().getName() + " ]", Duration.millis(3000), NotificationType.SIMPLE, JavaFXTool.getFontIcon("fa-dropbox", DropboxViewer.FONT_ICON_COLOR, 64)));
                // Update the progress
                updateProgress(1, 1);
                return true;
            } catch (final Exception ex) {
                ex.printStackTrace();
                // Show message to the User
                Platform.runLater(() -> AlertTool.showNotification("Download Failed", "Failed to download " + (!dropboxFile.isDirectory() ? "File" : FOLDER) + ":\n[ " + dropboxFile.getMetadata().getName() + " ]", Duration.millis(3000), NotificationType.ERROR));
                return false;
            }
        }

        /**
         * Download Dropbox File to Local Computer
         *
         * @param client                Current connected client
         * @param dropboxFile           The file path on the Dropbox cloud server ->
         *                              [/foldername/something.txt] or a Folder [/fuck]
         * @param localFileAbsolutePath The absolute file path of the File on the Local
         *                              File System
         * @throws DbxException
         * @throws DownloadErrorException
         * @throws IOException
         */
        public void downloadFile(final DbxClientV2 client, final DropboxFile dropboxFile, final String localFileAbsolutePath) throws DownloadErrorException, DbxException, IOException {
            final String dropBoxFilePath = dropboxFile.getMetadata().getPathLower();
            // Simple File
            if (!dropboxFile.isDirectory()) {
                // Create DbxDownloader
                downloadFile = client.files().download(dropBoxFilePath);
                try (// FileOutputStream
                FileOutputStream fOut = new FileOutputStream(localFileAbsolutePath);
                    // ProgressOutPutStream
                    ProgressOutputStream output = new ProgressOutputStream(fOut, downloadFile.getResult().getSize(), (long completed, long totalSize) -> {
                        // System.out.println( ( completed * 100 ) / totalSize + " %")
                        updateProgress((completed * 100), totalSize);
                    })) {
                    // FileOutputStream
                    System.out.println("Downloading .... " + dropBoxFilePath);
                    // Add a progress Listener
                    downloadFile.download(output);
                // Fast way...
                // client.files().downloadBuilder(file).download(new
                // FileOutputStream("downloads/" + md.getName()))
                // DbxRawClientV2 rawClient = new
                // DbxRawClientV2(config,dropBoxViewer.getAccessToken())
                // DbxUserFilesRequests r = new DbxUserFilesRequests(client)
                } catch (final Exception ex) {
                    ex.printStackTrace();
                }
            // Directory
            } else {
                // Create DbxDownloader
                downloadFolder = client.files().downloadZip(dropBoxFilePath);
                try (// FileOutputStream
                FileOutputStream fOut = new FileOutputStream(localFileAbsolutePath)) {
                    // FileOutputStream
                    System.out.println("Downloading .... " + dropBoxFilePath);
                    // Add a progress Listener
                    downloadFolder.download(fOut);
                } catch (final Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    };
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) Task(javafx.concurrent.Task) DropboxFile(com.goxr3plus.xr3player.controllers.dropbox.DropboxFile) FileOutputStream(java.io.FileOutputStream) ProgressOutputStream(com.goxr3plus.xr3player.controllers.dropbox.ProgressOutputStream) IOException(java.io.IOException) DownloadErrorException(com.dropbox.core.v2.files.DownloadErrorException) DbxException(com.dropbox.core.DbxException)

Example 3 with DownloadBuilder

use of com.dropbox.core.v2.files.DownloadBuilder in project jbpm-work-items by kiegroup.

the class DropboxWorkitemHandlerTest method setUp.

@Before
public void setUp() {
    try {
        testDoc = new DocumentImpl();
        testDoc.setName("testDoc.txt");
        testDoc.setIdentifier("testDoc");
        testDoc.setLastModified(new Date());
        testDoc.setContent(new String("test doc content").getBytes());
        InputStream testInputStream = IOUtils.toInputStream("test doc content", "UTF-8");
        when(auth.authorize(anyString(), anyString())).thenReturn(client);
        when(client.files()).thenReturn(fileRequests);
        // upload
        when(fileRequests.uploadBuilder(anyString())).thenReturn(uploadBuilder);
        when(uploadBuilder.withMode(any(WriteMode.class))).thenReturn(uploadBuilder);
        when(uploadBuilder.withClientModified(any(Date.class))).thenReturn(uploadBuilder);
        when(uploadBuilder.uploadAndFinish(any(java.io.InputStream.class))).thenReturn(metaData);
        // download
        when(fileRequests.downloadBuilder(anyString())).thenReturn(downloadBuilder);
        when(downloadBuilder.start()).thenReturn(downloader);
        when(downloader.getInputStream()).thenReturn(testInputStream);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) DocumentImpl(org.jbpm.document.service.impl.DocumentImpl) Date(java.util.Date) WriteMode(com.dropbox.core.v2.files.WriteMode) WorkItemHandlerRuntimeException(org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException) Before(org.junit.Before)

Example 4 with DownloadBuilder

use of com.dropbox.core.v2.files.DownloadBuilder in project dropbox-sdk-java by dropbox.

the class DbxClientV2IT method testDownloadBuilder.

@Test
public void testDownloadBuilder() throws Exception {
    DbxClientV2 client = ITUtil.newClientV2();
    String now = ITUtil.format(new Date());
    byte[] rtfV1 = ITUtil.toBytes("{\rtf1 sample {\b v1} (" + now + ")}");
    byte[] rtfV2 = ITUtil.toBytes("{\rtf1 sample {\b v2} (" + now + ")}");
    String path = ITUtil.path(getClass(), "/testDownloadBuilder/" + now + ".rtf");
    FileMetadata metadataV1 = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.ADD).withMute(true).uploadAndFinish(new ByteArrayInputStream(rtfV1));
    assertThat(metadataV1.getPathLower()).isEqualTo(path.toLowerCase());
    FileMetadata metadataV2 = client.files().uploadBuilder(path).withAutorename(false).withMode(WriteMode.OVERWRITE).withMute(true).uploadAndFinish(new ByteArrayInputStream(rtfV2));
    assertThat(metadataV2.getPathLower()).isEqualTo(path.toLowerCase());
    // ensure we have separate revisions
    assertThat(metadataV1.getRev()).isNotEqualTo(metadataV2.getRev());
    // now use download builder to set revision and make sure it works properly
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path).withRev(metadataV1.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV1);
    out = new ByteArrayOutputStream();
    client.files().downloadBuilder(path).withRev(metadataV2.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
    // ensure we still keep the non-builder optional route in our generator (for
    // backwards-compatibility)
    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV1.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV1);
    out = new ByteArrayOutputStream();
    client.files().download(path, metadataV2.getRev()).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
    // and ensure we keep the required-only route
    out = new ByteArrayOutputStream();
    client.files().download(path).download(out);
    assertThat(out.toByteArray()).isEqualTo(rtfV2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileMetadata(com.dropbox.core.v2.files.FileMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

DbxException (com.dropbox.core.DbxException)2 FileMetadata (com.dropbox.core.v2.files.FileMetadata)2 Date (java.util.Date)2 HttpRange (ch.cyberduck.core.http.HttpRange)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 DbxUserFilesRequests (com.dropbox.core.v2.files.DbxUserFilesRequests)1 DownloadBuilder (com.dropbox.core.v2.files.DownloadBuilder)1 DownloadErrorException (com.dropbox.core.v2.files.DownloadErrorException)1 WriteMode (com.dropbox.core.v2.files.WriteMode)1 DropboxFile (com.goxr3plus.xr3player.controllers.dropbox.DropboxFile)1 ProgressOutputStream (com.goxr3plus.xr3player.controllers.dropbox.ProgressOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Task (javafx.concurrent.Task)1 WorkItemHandlerRuntimeException (org.jbpm.bpmn2.handler.WorkItemHandlerRuntimeException)1 DocumentImpl (org.jbpm.document.service.impl.DocumentImpl)1 Before (org.junit.Before)1