Search in sources :

Example 6 with BlobFile

use of com.microsoft.tooling.msservices.model.storage.BlobFile in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method fillGrid.

public void fillGrid() {
    setUIState(true);
    DefaultLoader.getIdeHelper().runInBackground(null, "Loading blobs...", false, true, "Loading blobs...", new Runnable() {

        @Override
        public void run() {
            try {
                if (directoryQueue.peekLast() == null) {
                    directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                }
                blobItems = StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.peekLast());
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        if (!queryTextField.getText().isEmpty()) {
                            for (int i = blobItems.size() - 1; i >= 0; i--) {
                                BlobItem blobItem = blobItems.get(i);
                                if (blobItem instanceof BlobFile && !blobItem.getName().startsWith(queryTextField.getText())) {
                                    blobItems.remove(i);
                                }
                            }
                        }
                        pathLabel.setText(directoryQueue.peekLast().getPath());
                        tableViewer.setInput(blobItems);
                        tableViewer.refresh();
                        setUIState(false);
                    //
                    //                            blobListTable.clearSelection();
                    }
                });
            } catch (AzureCmdException ex) {
                DefaultLoader.getUIHelper().showException("Error querying blob list.", ex, "Error querying blobs", false, true);
            }
        }
    });
}
Also used : BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile)

Example 7 with BlobFile

use of com.microsoft.tooling.msservices.model.storage.BlobFile in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method downloadSelectedFile.

private void downloadSelectedFile(final File targetFile, final boolean open) {
    final BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        Job job = new Job("Downloading blob...") {

            @Override
            protected IStatus run(final IProgressMonitor monitor) {
                monitor.beginTask("Downloading blob...", IProgressMonitor.UNKNOWN);
                try {
                    if (!targetFile.exists()) {
                        if (!targetFile.createNewFile()) {
                            throw new IOException("File not created");
                        }
                    }
                    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile), 65536) {

                        private long runningCount = 0;

                        @Override
                        public synchronized void write(@NotNull byte[] bytes, int i, int i1) throws IOException {
                            super.write(bytes, i, i1);
                            runningCount += i1;
                            double progress = (double) runningCount / fileSelection.getSize();
                            monitor.worked((int) (100 * progress));
                            monitor.subTask(String.format("%s%% downloaded", (int) (progress * 100)));
                        }
                    };
                    try {
                        //                                public void run() {
                        try {
                            StorageClientSDKManager.getManager().downloadBlobFileContent(connectionString, fileSelection, bufferedOutputStream);
                            if (open && targetFile.exists()) {
                                try {
                                    final Process p;
                                    Runtime runtime = Runtime.getRuntime();
                                    p = runtime.exec(new String[] { "open", "-R", targetFile.getName() }, null, targetFile.getParentFile());
                                    InputStream errorStream = p.getErrorStream();
                                    String errResponse = new String(IOUtils.readFully(errorStream, -1, true));
                                    if (p.waitFor() != 0) {
                                        throw new Exception(errResponse);
                                    }
                                } catch (Exception e) {
                                    monitor.setTaskName("Error opening file");
                                    monitor.subTask(e.getMessage());
                                }
                            //                                            Desktop.getDesktop().open(targetFile);
                            }
                        } catch (AzureCmdException e) {
                            Throwable connectionFault = e.getCause().getCause();
                            monitor.setTaskName("Error downloading Blob");
                            monitor.subTask((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : connectionFault.getMessage());
                            return Status.CANCEL_STATUS;
                        }
                    } finally {
                        bufferedOutputStream.close();
                    }
                } catch (IOException e) {
                    DefaultLoader.getUIHelper().showException("Error downloading Blob", e, "Error downloading Blob", false, true);
                    return Status.CANCEL_STATUS;
                } finally {
                    monitor.done();
                }
                return Status.OK_STATUS;
            }
        };
        job.schedule();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) IOException(java.io.IOException) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull) PartInitException(org.eclipse.ui.PartInitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SocketTimeoutException(java.net.SocketTimeoutException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) FileOutputStream(java.io.FileOutputStream) Job(org.eclipse.core.runtime.jobs.Job) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with BlobFile

use of com.microsoft.tooling.msservices.model.storage.BlobFile in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method saveAsSelectedFile.

private void saveAsSelectedFile() {
    BlobFile fileSelection = getFileSelection();
    assert fileSelection != null;
    File file = DefaultLoader.getUIHelper().showFileChooser("Save As");
    if (file != null) {
        downloadSelectedFile(file, false);
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) File(java.io.File)

Example 9 with BlobFile

use of com.microsoft.tooling.msservices.model.storage.BlobFile in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method downloadSelectedFile.

private void downloadSelectedFile(final File targetFile, final boolean open) {
    final BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        ProgressManager.getInstance().run(new Task.Backgroundable(project, "Downloading blob...", true) {

            @Override
            public void run(@NotNull final ProgressIndicator progressIndicator) {
                try {
                    progressIndicator.setIndeterminate(false);
                    if (!targetFile.exists()) {
                        if (!targetFile.createNewFile()) {
                            throw new IOException("File not created");
                        }
                    }
                    final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile), 65536) {

                        private long runningCount = 0;

                        @Override
                        public synchronized void write(@NotNull byte[] bytes, int i, int i1) throws IOException {
                            super.write(bytes, i, i1);
                            runningCount += i1;
                            double progress = (double) runningCount / fileSelection.getSize();
                            progressIndicator.setFraction(progress);
                            progressIndicator.setText2(String.format("%s%% downloaded", (int) (progress * 100)));
                        }
                    };
                    try {
                        Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    StorageClientSDKManager.getManager().downloadBlobFileContent(connectionString, fileSelection, bufferedOutputStream);
                                    if (open && targetFile.exists()) {
                                        Desktop.getDesktop().open(targetFile);
                                    }
                                } catch (AzureCmdException e) {
                                    Throwable connectionFault = e.getCause().getCause();
                                    progressIndicator.setText("Error downloading Blob");
                                    progressIndicator.setText2((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : connectionFault.getMessage());
                                } catch (IOException ex) {
                                    try {
                                        final Process p;
                                        Runtime runtime = Runtime.getRuntime();
                                        p = runtime.exec(new String[] { "open", "-R", targetFile.getName() }, null, targetFile.getParentFile());
                                        InputStream errorStream = p.getErrorStream();
                                        String errResponse = new String(IOUtils.readFully(errorStream, -1, true));
                                        if (p.waitFor() != 0) {
                                            throw new Exception(errResponse);
                                        }
                                    } catch (Exception e) {
                                        progressIndicator.setText("Error openning file");
                                        progressIndicator.setText2(ex.getMessage());
                                    }
                                }
                            }
                        });
                        while (!future.isDone()) {
                            progressIndicator.checkCanceled();
                            if (progressIndicator.isCanceled()) {
                                future.cancel(true);
                            }
                        }
                    } finally {
                        bufferedOutputStream.close();
                    }
                } catch (IOException e) {
                    PluginUtil.displayErrorDialogAndLog(message("errTtl"), "An error occurred while attempting to download Blob.", e);
                }
            }
        });
    }
}
Also used : Task(com.intellij.openapi.progress.Task) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) SocketTimeoutException(java.net.SocketTimeoutException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) SocketTimeoutException(java.net.SocketTimeoutException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Future(java.util.concurrent.Future)

Example 10 with BlobFile

use of com.microsoft.tooling.msservices.model.storage.BlobFile in project azure-tools-for-java by Microsoft.

the class BlobExplorerFileEditor method copyURLSelectedFile.

private void copyURLSelectedFile() {
    BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        StringSelection selection = new StringSelection(fileSelection.getUri());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(selection, selection);
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) Clipboard(java.awt.datatransfer.Clipboard) StringSelection(java.awt.datatransfer.StringSelection)

Aggregations

BlobFile (com.microsoft.tooling.msservices.model.storage.BlobFile)14 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)8 BlobItem (com.microsoft.tooling.msservices.model.storage.BlobItem)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Task (com.intellij.openapi.progress.Task)3 BlobDirectory (com.microsoft.tooling.msservices.model.storage.BlobDirectory)3 SocketTimeoutException (java.net.SocketTimeoutException)3 NotNull (com.microsoft.azuretools.azurecommons.helpers.NotNull)2 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 Job (org.eclipse.core.runtime.jobs.Job)2 PartInitException (org.eclipse.ui.PartInitException)2 CallableSingleArg (com.microsoft.tooling.msservices.helpers.CallableSingleArg)1 Clipboard (java.awt.datatransfer.Clipboard)1 StringSelection (java.awt.datatransfer.StringSelection)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1