Search in sources :

Example 1 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) {
        final Clipboard cb = new Clipboard(PlatformUI.getWorkbench().getDisplay());
        cb.setContents(new Object[] { fileSelection.getUri() }, new Transfer[] { TextTransfer.getInstance() });
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) Clipboard(org.eclipse.swt.dnd.Clipboard)

Example 2 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(boolean open) {
    String defaultFolder = System.getProperty("user.home") + File.separator + "Downloads";
    BlobFile fileSelection = getFileSelection();
    if (fileSelection != null) {
        downloadSelectedFile(new File(defaultFolder + File.separator + fileSelection.getName()), open);
    }
}
Also used : BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) File(java.io.File)

Example 3 with BlobFile

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

the class BlobExplorerFileEditor method uploadFile.

private void uploadFile(final String path, final File selectedFile) {
    Job job = new Job("Uploading blob...") {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask("Uploading blob...", IProgressMonitor.UNKNOWN);
            try {
                final BlobDirectory blobDirectory = directoryQueue.peekLast();
                final BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(selectedFile));
                monitor.subTask("0% uploaded");
                try {
                    final CallableSingleArg<Void, Long> callable = new CallableSingleArg<Void, Long>() {

                        @Override
                        public Void call(Long uploadedBytes) throws Exception {
                            double progress = ((double) uploadedBytes) / selectedFile.length();
                            monitor.worked((int) (100 * progress));
                            monitor.subTask(String.format("%s%% uploaded", (int) (progress * 100)));
                            return null;
                        }
                    };
                    try {
                        StorageClientSDKManager.getManager().uploadBlobFileContent(connectionString, blobContainer, path, bufferedInputStream, callable, 1024 * 1024, selectedFile.length());
                    } catch (AzureCmdException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            bufferedInputStream.close();
                        } catch (IOException ignored) {
                        }
                    }
                    if (monitor.isCanceled()) {
                        //                                future.cancel(true);
                        bufferedInputStream.close();
                        for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, blobDirectory)) {
                            if (blobItem instanceof BlobFile && blobItem.getPath().equals(path)) {
                                StorageClientSDKManager.getManager().deleteBlobFile(connectionString, (BlobFile) blobItem);
                            }
                        }
                    }
                    try {
                        directoryQueue.clear();
                        directoryQueue.addLast(StorageClientSDKManager.getManager().getRootDirectory(connectionString, blobContainer));
                        for (String pathDir : path.split("/")) {
                            for (BlobItem blobItem : StorageClientSDKManager.getManager().getBlobItems(connectionString, directoryQueue.getLast())) {
                                if (blobItem instanceof BlobDirectory && blobItem.getName().equals(pathDir)) {
                                    directoryQueue.addLast((BlobDirectory) blobItem);
                                }
                            }
                        }
                    } catch (AzureCmdException e) {
                        DefaultLoader.getUIHelper().showException("Error showing new blob", e, "Error showing new blob", false, true);
                    }
                    DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            fillGrid();
                        }
                    });
                } catch (Exception e) {
                    Throwable connectionFault = e.getCause();
                    Throwable realFault = null;
                    if (connectionFault != null) {
                        realFault = connectionFault.getCause();
                    }
                    monitor.setTaskName("Error uploading Blob");
                    String message = realFault == null ? null : realFault.getMessage();
                    if (connectionFault != null && message == null) {
                        message = "Error type " + connectionFault.getClass().getName();
                    }
                    monitor.subTask((connectionFault instanceof SocketTimeoutException) ? "Connection timed out" : message);
                }
            } catch (Exception e) {
                DefaultLoader.getUIHelper().showException("Error uploading Blob", e, "Error uploading Blob", false, true);
                return Status.CANCEL_STATUS;
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    job.schedule();
}
Also used : BlobDirectory(com.microsoft.tooling.msservices.model.storage.BlobDirectory) CallableSingleArg(com.microsoft.tooling.msservices.helpers.CallableSingleArg) BlobFile(com.microsoft.tooling.msservices.model.storage.BlobFile) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PartInitException(org.eclipse.ui.PartInitException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) BlobItem(com.microsoft.tooling.msservices.model.storage.BlobItem) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Job(org.eclipse.core.runtime.jobs.Job)

Example 4 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 5 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)

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