Search in sources :

Example 1 with DbxRawClientV2

use of com.dropbox.core.v2.DbxRawClientV2 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 2 with DbxRawClientV2

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

the class DropboxListService method list.

@Override
public AttributedList<Path> list(final Path directory, final ListProgressListener listener) throws BackgroundException {
    try {
        final AttributedList<Path> children = new AttributedList<>();
        ListFolderResult result;
        final DbxRawClientV2 client;
        this.parse(directory, listener, children, result = new DbxUserFilesRequests(session.getClient(directory)).listFolder(containerService.getKey(directory)));
        // If true, then there are more entries available. Pass the cursor to list_folder/continue to retrieve the rest.
        while (result.getHasMore()) {
            this.parse(directory, listener, children, result = new DbxUserFilesRequests(session.getClient(directory)).listFolderContinue(result.getCursor()));
        }
        return children;
    } catch (DbxException e) {
        throw new DropboxExceptionMappingService().map("Listing directory {0} failed", e, directory);
    }
}
Also used : Path(ch.cyberduck.core.Path) DbxUserFilesRequests(com.dropbox.core.v2.files.DbxUserFilesRequests) AttributedList(ch.cyberduck.core.AttributedList) ListFolderResult(com.dropbox.core.v2.files.ListFolderResult) DbxRawClientV2(com.dropbox.core.v2.DbxRawClientV2) DbxException(com.dropbox.core.DbxException)

Aggregations

DbxException (com.dropbox.core.DbxException)2 AttributedList (ch.cyberduck.core.AttributedList)1 Path (ch.cyberduck.core.Path)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 DbxRawClientV2 (com.dropbox.core.v2.DbxRawClientV2)1 DbxUserFilesRequests (com.dropbox.core.v2.files.DbxUserFilesRequests)1 DownloadErrorException (com.dropbox.core.v2.files.DownloadErrorException)1 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)1 DropboxFile (com.goxr3plus.xr3player.controllers.dropbox.DropboxFile)1 ProgressOutputStream (com.goxr3plus.xr3player.controllers.dropbox.ProgressOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Task (javafx.concurrent.Task)1