Search in sources :

Example 1 with IAppService

use of com.microsoft.azure.toolkit.lib.appservice.service.IAppService 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 2 with IAppService

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

the class WebAppBasePropertyViewPresenter method getPublishingProfile.

protected boolean getPublishingProfile(@Nonnull String sid, @Nonnull String webAppId, @Nullable String name, @Nonnull String filePath) throws Exception {
    final ResourceId resourceId = ResourceId.fromString(webAppId);
    final File file = new File(Paths.get(filePath, String.format("%s_%s.PublishSettings", resourceId.name(), System.currentTimeMillis())).toString());
    try {
        file.createNewFile();
    } catch (final IOException e) {
        AzureMessager.getMessager().warning("failed to create publishing profile xml file");
        return false;
    }
    final IAppService resource = getWebAppBase(sid, webAppId, name);
    try (final InputStream inputStream = resource.listPublishingProfileXmlWithSecrets();
        final OutputStream outputStream = new FileOutputStream(file)) {
        IOUtils.copy(inputStream, outputStream);
        return true;
    } catch (final IOException e) {
        AzureMessager.getMessager().warning("failed to get publishing profile xml");
        return false;
    }
}
Also used : IAppService(com.microsoft.azure.toolkit.lib.appservice.service.IAppService) ResourceId(com.azure.resourcemanager.resources.fluentcore.arm.ResourceId) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 3 with IAppService

use of com.microsoft.azure.toolkit.lib.appservice.service.IAppService 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

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