Search in sources :

Example 1 with AppServiceFile

use of com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method saveAppServiceFile.

@SneakyThrows
@Override
public void saveAppServiceFile(@NotNull AppServiceFile file, @NotNull Object context, @Nullable File dest) {
    final File destFile = Objects.isNull(dest) ? DefaultLoader.getUIHelper().showFileSaver("Download", file.getName()) : dest;
    if (Objects.isNull(destFile)) {
        return;
    }
    final OutputStream output = new FileOutputStream(destFile);
    final Project project = (Project) context;
    final AzureString title = AzureOperationBundle.title("appservice|file.download", file.getName());
    final AzureTask<Void> task = new AzureTask<>(project, title, false, () -> {
        ProgressManager.getInstance().getProgressIndicator().setIndeterminate(true);
        file.getApp().getFileContent(file.getPath()).doOnComplete(() -> notifyDownloadSuccess(file.getName(), destFile, ((Project) context))).doOnTerminate(() -> IOUtils.closeQuietly(output, null)).subscribe(bytes -> {
            try {
                if (bytes != null) {
                    output.write(bytes.array(), 0, bytes.limit());
                }
            } catch (final IOException e) {
                final String error = "failed to write data into local file";
                final String action = "try later";
                throw new AzureToolkitRuntimeException(error, e, action);
            }
        }, IDEHelperImpl::onRxException);
    });
    AzureTaskManager.getInstance().runInModal(task);
}
Also used : Project(com.intellij.openapi.project.Project) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) AppServiceFile(com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile) File(java.io.File) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) SneakyThrows(lombok.SneakyThrows)

Example 2 with AppServiceFile

use of com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method saveFileToAzure.

@AzureOperation(name = "appservice|file.save", params = { "appServiceFile.getName()" }, type = AzureOperation.Type.SERVICE)
private void saveFileToAzure(final AppServiceFile appServiceFile, final String content, final Project project) {
    final AzureString title = AzureOperationBundle.title("appservice|file.save", appServiceFile.getName());
    AzureTaskManager.getInstance().runInBackground(new AzureTask<>(project, title, false, () -> {
        final IAppService appService = appServiceFile.getApp();
        final AppServiceFile target = appService.getFileByPath(appServiceFile.getPath());
        final boolean deleted = target == null;
        final boolean outDated = !deleted && ZonedDateTime.parse(target.getMtime()).isAfter(ZonedDateTime.parse(appServiceFile.getMtime()));
        boolean toSave = true;
        if (deleted) {
            toSave = DefaultLoader.getUIHelper().showYesNoDialog(null, String.format(FILE_HAS_BEEN_DELETED, appServiceFile.getName()), APP_SERVICE_FILE_EDITING, Messages.getQuestionIcon());
        } else if (outDated) {
            toSave = DefaultLoader.getUIHelper().showYesNoDialog(null, String.format(FILE_HAS_BEEN_MODIFIED, appServiceFile.getName()), APP_SERVICE_FILE_EDITING, Messages.getQuestionIcon());
        }
        if (toSave) {
            appService.uploadFileToPath(content, appServiceFile.getPath());
            PluginUtil.showInfoNotification(APP_SERVICE_FILE_EDITING, String.format(FILE_HAS_BEEN_SAVED, appServiceFile.getName()));
        }
    }));
}
Also used : IAppService(com.microsoft.azure.toolkit.lib.appservice.service.IAppService) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) AppServiceFile(com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 3 with AppServiceFile

use of com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile in project azure-tools-for-java by Microsoft.

the class IDEHelperImpl method openAppServiceFile.

@AzureOperation(name = "appservice|file.open", params = { "target.getName()" }, type = AzureOperation.Type.SERVICE)
@SneakyThrows
public void openAppServiceFile(AppServiceFile target, Object context) {
    final IAppService appService = target.getApp();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance((Project) context);
    final VirtualFile virtualFile = getOrCreateVirtualFile(target, fileEditorManager);
    final OutputStream output = virtualFile.getOutputStream(null);
    final AzureString title = AzureOperationBundle.title("appservice|file.open", virtualFile.getName());
    final AzureTask<Void> task = new AzureTask<>(null, title, false, () -> {
        final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
        indicator.setIndeterminate(true);
        indicator.setText2("Checking file existence");
        final AppServiceFile file = appService.getFileByPath(target.getPath());
        if (file == null) {
            final String failureFileDeleted = String.format("Target file (%s) has been deleted", target.getName());
            UIUtil.invokeLaterIfNeeded(() -> Messages.showWarningDialog(failureFileDeleted, "Open File"));
            return;
        }
        indicator.setText2("Loading file content");
        final String failure = String.format("Can not open file (%s). Try downloading it first and open it manually.", virtualFile.getName());
        appService.getFileContent(file.getPath()).doOnComplete(() -> AzureTaskManager.getInstance().runLater(() -> {
            final Consumer<String> contentSaver = content -> saveFileToAzure(target, content, fileEditorManager.getProject());
            if (!openFileInEditor(contentSaver, virtualFile, fileEditorManager)) {
                Messages.showWarningDialog(failure, "Open File");
            }
        }, AzureTask.Modality.NONE)).doAfterTerminate(() -> IOUtils.closeQuietly(output, null)).subscribe(bytes -> {
            try {
                if (bytes != null) {
                    output.write(bytes.array(), 0, bytes.limit());
                }
            } catch (final IOException e) {
                final String error = "failed to load data into editor";
                final String action = "try later or downloading it first";
                throw new AzureToolkitRuntimeException(error, e, action);
            }
        }, IDEHelperImpl::onRxException);
    });
    AzureTaskManager.getInstance().runInModal(task);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) IAppService(com.microsoft.azure.toolkit.lib.appservice.service.IAppService) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Consumer(com.intellij.util.Consumer) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) AzureTask(com.microsoft.azure.toolkit.lib.common.task.AzureTask) AppServiceFile(com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) SneakyThrows(lombok.SneakyThrows)

Aggregations

AppServiceFile (com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFile)3 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 IAppService (com.microsoft.azure.toolkit.lib.appservice.service.IAppService)2 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)2 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)2 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 OutputStream (java.io.OutputStream)2 SneakyThrows (lombok.SneakyThrows)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Project (com.intellij.openapi.project.Project)1 Consumer (com.intellij.util.Consumer)1 File (java.io.File)1