|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.microsoft.tooling.msservices.serviceexplorer.azure.appservice.file.legacy; |
| 7 | + |
| 8 | +import com.microsoft.azure.management.appservice.FunctionApp; |
| 9 | +import com.microsoft.azure.toolkit.lib.appservice.file.AppServiceFileService; |
| 10 | +import com.microsoft.azure.toolkit.lib.appservice.model.AppServiceFileLegacy; |
| 11 | +import com.microsoft.azure.toolkit.lib.appservice.utils.Utils; |
| 12 | +import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation; |
| 13 | +import com.microsoft.azure.toolkit.lib.common.operation.AzureOperationBundle; |
| 14 | +import com.microsoft.azure.toolkit.lib.common.operation.IAzureOperationTitle; |
| 15 | +import com.microsoft.azure.toolkit.lib.common.task.AzureTask; |
| 16 | +import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager; |
| 17 | +import com.microsoft.azuretools.telemetry.AppInsightsConstants; |
| 18 | +import com.microsoft.azuretools.telemetry.TelemetryConstants; |
| 19 | +import com.microsoft.azuretools.telemetry.TelemetryProperties; |
| 20 | +import com.microsoft.azuretools.telemetrywrapper.EventUtil; |
| 21 | +import com.microsoft.tooling.msservices.components.DefaultLoader; |
| 22 | +import com.microsoft.tooling.msservices.serviceexplorer.AzureRefreshableNode; |
| 23 | +import com.microsoft.tooling.msservices.serviceexplorer.Node; |
| 24 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent; |
| 25 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener; |
| 26 | +import com.microsoft.tooling.msservices.serviceexplorer.Sortable; |
| 27 | +import com.microsoft.tooling.msservices.serviceexplorer.azure.webapp.WebAppModule; |
| 28 | +import lombok.extern.java.Log; |
| 29 | +import org.apache.commons.io.FileUtils; |
| 30 | + |
| 31 | +import javax.swing.*; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +@Deprecated |
| 36 | +@Log |
| 37 | +public class AppServiceFileNode extends AzureRefreshableNode implements TelemetryProperties { |
| 38 | + private static final String MODULE_ID = WebAppModule.class.getName(); |
| 39 | + private static final long SIZE_20MB = 20 * 1024 * 1024; |
| 40 | + private final AppServiceFileService fileService; |
| 41 | + private final AppServiceFileLegacy file; |
| 42 | + |
| 43 | + public AppServiceFileNode(final AppServiceFileLegacy file, final Node parent, AppServiceFileService service) { |
| 44 | + super(file.getName(), file.getName(), parent, null); |
| 45 | + this.file = file; |
| 46 | + this.fileService = service; |
| 47 | + if (this.file.getType() != AppServiceFileLegacy.Type.DIRECTORY) { |
| 48 | + this.addDownloadAction(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void addDownloadAction() { |
| 53 | + this.addAction("Download", new NodeActionListener() { |
| 54 | + @Override |
| 55 | + protected void actionPerformed(final NodeActionEvent e) { |
| 56 | + download(); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @AzureOperation(name = "appservice|file.download", params = {"this.file.getName()"}, type = AzureOperation.Type.ACTION) |
| 62 | + private void download() { |
| 63 | + DefaultLoader.getIdeHelper().saveAppServiceFile(file, getProject(), null); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + @AzureOperation(name = "appservice|file.refresh", params = {"this.file.getName()"}, type = AzureOperation.Type.ACTION) |
| 68 | + protected void refreshItems() { |
| 69 | + executeWithTelemetryWrapper(TelemetryConstants.REFRESH_FILE, () -> { |
| 70 | + if (this.file.getType() != AppServiceFileLegacy.Type.DIRECTORY) { |
| 71 | + return; |
| 72 | + } |
| 73 | + this.fileService.getFilesInDirectory(this.file.getPath()).stream() |
| 74 | + .map(file -> new AppServiceFileNode(file, this, fileService)) |
| 75 | + .forEach(this::addChildNode); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @AzureOperation(name = "appservice|file.open", params = {"this.file.getName()"}, type = AzureOperation.Type.ACTION) |
| 80 | + private void open(final Object context) { |
| 81 | + executeWithTelemetryWrapper(TelemetryConstants.OPEN_FILE, () -> DefaultLoader.getIdeHelper().openAppServiceFile(this.file, context)); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onNodeDblClicked(Object context) { |
| 86 | + if (this.file.getType() == AppServiceFileLegacy.Type.DIRECTORY) { |
| 87 | + return; |
| 88 | + } else if (this.file.getSize() > SIZE_20MB) { |
| 89 | + DefaultLoader.getUIHelper().showError("File is too large, please download it first", "File is Too Large"); |
| 90 | + return; |
| 91 | + } |
| 92 | + final Runnable runnable = () -> open(context); |
| 93 | + final IAzureOperationTitle title = AzureOperationBundle.title("appservice|file.get_content", file.getName(), file.getApp().name()); |
| 94 | + AzureTaskManager.getInstance().runInBackground(new AzureTask(this.getProject(), title, false, runnable)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int getPriority() { |
| 99 | + return this.file.getType() == AppServiceFileLegacy.Type.DIRECTORY ? Sortable.HIGH_PRIORITY : Sortable.DEFAULT_PRIORITY; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Icon getIcon() { |
| 104 | + return DefaultLoader.getIdeHelper().getFileTypeIcon(this.file.getName(), this.file.getType() == AppServiceFileLegacy.Type.DIRECTORY); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getToolTip() { |
| 109 | + return file.getType() == AppServiceFileLegacy.Type.DIRECTORY ? |
| 110 | + String.format("Type: %s Date modified: %s", file.getMime(), file.getMtime()) : |
| 111 | + String.format("Type: %s Size: %s Date modified: %s", file.getMime(), FileUtils.byteCountToDisplaySize(file.getSize()), file.getMtime()); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String getServiceName() { |
| 116 | + return file.getApp() instanceof FunctionApp ? TelemetryConstants.FUNCTION : TelemetryConstants.WEBAPP; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Map<String, String> toProperties() { |
| 121 | + return Collections.singletonMap(AppInsightsConstants.SubscriptionId, Utils.getSubscriptionId(file.getApp().id())); |
| 122 | + } |
| 123 | + |
| 124 | + // todo: replace with AzureOperation when custom properties is supported for AzureOperation |
| 125 | + private void executeWithTelemetryWrapper(final String operationName, Runnable runnable) { |
| 126 | + EventUtil.executeWithLog(getServiceName(), operationName, operation -> { |
| 127 | + operation.trackProperty(TelemetryConstants.SUBSCRIPTIONID, Utils.getSubscriptionId(file.getApp().id())); |
| 128 | + runnable.run(); |
| 129 | + }); |
| 130 | + } |
| 131 | +} |
0 commit comments