public async Task <IActionResult> DownloadZip([FromBody] RemoteAgentSettings settings, CancellationToken cancellationToken)
        {
            var appSettingsStream = await CreateAppSettingsFile(settings, cancellationToken);

            var remoteRunStream = await GetRemoteRun(settings.Environment.ToString(),
                                                     settings.RemoteApplicationSettings.PreRelease? "prerelease" : "stable", cancellationToken);

            var memoryStream = new MemoryStream();

            using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                var appSettingsEntry = archive.CreateEntry("appsettings.json");
                await using (var appSettings = appSettingsEntry.Open())
                {
                    await appSettingsStream.CopyToAsync(appSettings, cancellationToken);
                }

                var remoteRunEntry = archive.CreateEntry(remoteRunStream.fileName);
                remoteRunEntry.ExternalAttributes = -2113994752;  // set unix permissions to 777.
                await using (var remoteRunSettings = remoteRunEntry.Open())
                {
                    await remoteRunStream.stream.CopyToAsync(remoteRunSettings, cancellationToken);
                }
            }

            memoryStream.Position = 0;
            return(File(memoryStream, "application/octet-stream", "dexih.remote.zip"));
        }
        private async Task <MemoryStream> CreateAppSettingsFile(RemoteAgentSettings settings, CancellationToken cancellationToken)
        {
            var remoteSettings = new RemoteSettings();

            // copy any settings across.
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.AppSettings);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Network);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Permissions);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Plugins);
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Alerts);
            //TODO these should be copied by the copy properties.
            remoteSettings.Permissions.AllowedHubs  = settings.RemoteApplicationSettings.AllowedHubs;
            remoteSettings.Permissions.AllowedPaths = settings.RemoteApplicationSettings.AllowedPaths;
            settings.RemoteApplicationSettings.CopyProperties(remoteSettings.Privacy);
            remoteSettings.NamingStandards.LoadDefault();

            remoteSettings.AppSettings.UserPrompt = false;
            remoteSettings.AppSettings.WebServer  = (HttpContext.Request.IsHttps ? "https://" : "http://") +
                                                    HttpContext.Request.Host + HttpContext.Request.PathBase.Value;
            remoteSettings.AppSettings.RemoteAgentId = Guid.NewGuid().ToString();

            var user = await _operations.RepositoryManager.GetUserAsync(User, cancellationToken);

            var dbRemoteAgent = new DexihRemoteAgent()
            {
                RemoteAgentId = remoteSettings.AppSettings.RemoteAgentId,
                RestrictIp    = false,
                Name          = remoteSettings.AppSettings.Name,
                UserId        = user.Id
            };

            if (settings.EmbedUserName)
            {
                remoteSettings.AppSettings.UserToken = await _operations.RepositoryManager.GenerateRemoteUserTokenAsync(user, remoteSettings.AppSettings.RemoteAgentId, cancellationToken);

                var hashedToken = SecureHash.CreateHash(remoteSettings.AppSettings.UserToken);
                dbRemoteAgent.HashedToken       = hashedToken;
                remoteSettings.AppSettings.User = user.UserName;
            }

            if (settings.RemoteApplicationSettings.AutoGenerateCertificate)
            {
                remoteSettings.Network.CertificateFilename = "dexih.pfx";
                remoteSettings.Network.CertificatePassword = EncryptString.GenerateRandomKey();
            }

            remoteSettings.Logging.LogLevel.Default   = settings.LogLevel;
            remoteSettings.Logging.LogLevel.System    = settings.LogLevel;
            remoteSettings.Logging.LogLevel.Microsoft = settings.LogLevel;

            await _operations.RepositoryManager.SaveRemoteAgent(user.Id, dbRemoteAgent, cancellationToken);

            var json = JsonSerializer.Serialize(remoteSettings, new JsonSerializerOptions()
            {
                WriteIndented = true
            });
            var appSettingsStream = new MemoryStream(Encoding.ASCII.GetBytes(json));

            return(appSettingsStream);
        }
        public async Task <IActionResult> DownloadAppSettings([FromBody] RemoteAgentSettings settings, CancellationToken cancellationToken)
        {
            var appsettingsStream = await CreateAppSettingsFile(settings, cancellationToken);

            return(File(appsettingsStream, "application/json", "appsettings.json"));
        }