public async Task <IActionResult> Execute(int id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Export))
            {
                return(Unauthorized());
            }

            var deploymentPlan = await _session.GetAsync <DeploymentPlan>(id);

            if (deploymentPlan == null)
            {
                return(NotFound());
            }

            string archiveFileName;
            var    filename = deploymentPlan.Name.ToSafeName() + ".zip";

            using (var fileBuilder = new TemporaryFileBuilder())
            {
                archiveFileName = PathExtensions.Combine(Path.GetTempPath(), filename);

                var deploymentPlanResult = new DeploymentPlanResult(fileBuilder);
                await _deploymentManager.ExecuteDeploymentPlanAsync(deploymentPlan, deploymentPlanResult);

                ZipFile.CreateFromDirectory(fileBuilder.Folder, archiveFileName);
            }

            return(new PhysicalFileResult(archiveFileName, "application/zip")
            {
                FileDownloadName = filename
            });
        }
Пример #2
0
        public async Task <IActionResult> Execute(int id)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Export))
            {
                return(Forbid());
            }

            var deploymentPlan = await _session.GetAsync <DeploymentPlan>(id);

            if (deploymentPlan == null)
            {
                return(NotFound());
            }

            string archiveFileName;
            var    filename = deploymentPlan.Name.ToSafeName() + ".zip";

            using (var fileBuilder = new TemporaryFileBuilder())
            {
                archiveFileName = fileBuilder.Folder + ".zip";

                var recipeDescriptor         = new RecipeDescriptor();
                var recipeFileDeploymentStep = deploymentPlan.DeploymentSteps.FirstOrDefault(ds => ds.Name == nameof(RecipeFileDeploymentStep)) as RecipeFileDeploymentStep;

                if (recipeFileDeploymentStep != null)
                {
                    recipeDescriptor.Name          = recipeFileDeploymentStep.RecipeName;
                    recipeDescriptor.DisplayName   = recipeFileDeploymentStep.DisplayName;
                    recipeDescriptor.Description   = recipeFileDeploymentStep.Description;
                    recipeDescriptor.Author        = recipeFileDeploymentStep.Author;
                    recipeDescriptor.WebSite       = recipeFileDeploymentStep.WebSite;
                    recipeDescriptor.Version       = recipeFileDeploymentStep.Version;
                    recipeDescriptor.IsSetupRecipe = recipeFileDeploymentStep.IsSetupRecipe;
                    recipeDescriptor.Categories    = (recipeFileDeploymentStep.Categories ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries);
                    recipeDescriptor.Tags          = (recipeFileDeploymentStep.Tags ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries);
                }

                var deploymentPlanResult = new DeploymentPlanResult(fileBuilder, recipeDescriptor);
                await _deploymentManager.ExecuteDeploymentPlanAsync(deploymentPlan, deploymentPlanResult);

                ZipFile.CreateFromDirectory(fileBuilder.Folder, archiveFileName);
            }

            return(new PhysicalFileResult(archiveFileName, "application/zip")
            {
                FileDownloadName = filename
            });
        }
        public async Task <IActionResult> Execute(int id, string remoteInstanceId, string returnUrl)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Export))
            {
                return(Forbid());
            }

            var deploymentPlan = await _session.GetAsync <DeploymentPlan>(id);

            if (deploymentPlan == null)
            {
                return(NotFound());
            }

            var remoteInstance = await _service.GetRemoteInstanceAsync(remoteInstanceId);

            if (remoteInstance == null)
            {
                return(NotFound());
            }

            string archiveFileName;
            var    filename = deploymentPlan.Name.ToSafeName() + ".zip";

            using (var fileBuilder = new TemporaryFileBuilder())
            {
                archiveFileName = PathExtensions.Combine(Path.GetTempPath(), filename);

                var deploymentPlanResult = new DeploymentPlanResult(fileBuilder, new RecipeDescriptor());
                await _deploymentManager.ExecuteDeploymentPlanAsync(deploymentPlan, deploymentPlanResult);

                if (System.IO.File.Exists(archiveFileName))
                {
                    System.IO.File.Delete(archiveFileName);
                }

                ZipFile.CreateFromDirectory(fileBuilder.Folder, archiveFileName);
            }

            HttpResponseMessage response;

            try
            {
                using (var requestContent = new MultipartFormDataContent())
                {
                    requestContent.Add(new StreamContent(
                                           new FileStream(archiveFileName,
                                                          FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 1, FileOptions.Asynchronous | FileOptions.SequentialScan)
                                           ),
                                       nameof(ImportViewModel.Content), Path.GetFileName(archiveFileName));
                    requestContent.Add(new StringContent(remoteInstance.ClientName), nameof(ImportViewModel.ClientName));
                    requestContent.Add(new StringContent(remoteInstance.ApiKey), nameof(ImportViewModel.ApiKey));

                    response = await _httpClient.PostAsync(remoteInstance.Url, requestContent);
                }

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    _notifier.Success(H["Deployment executed successfully."]);
                }
                else
                {
                    _notifier.Error(H["An error occurred while sending the deployment to the remote instance: \"{0} ({1})\"", response.ReasonPhrase, (int)response.StatusCode]);
                }
            }
            finally
            {
                System.IO.File.Delete(archiveFileName);
            }

            if (!string.IsNullOrEmpty(returnUrl))
            {
                return(LocalRedirect(returnUrl));
            }

            return(RedirectToAction("Display", "DeploymentPlan", new { area = "OrchardCore.Deployment", id }));
        }
        public async Task <IActionResult> Execute(int id, string remoteInstanceId)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.Export))
            {
                return(Unauthorized());
            }

            var deploymentPlan = await _session.GetAsync <DeploymentPlan>(id);

            if (deploymentPlan == null)
            {
                return(NotFound());
            }

            var remoteInstance = await _service.GetRemoteInstanceAsync(remoteInstanceId);

            if (remoteInstance == null)
            {
                return(NotFound());
            }

            string archiveFileName;
            var    filename = deploymentPlan.Name.ToSafeName() + ".zip";

            using (var fileBuilder = new TemporaryFileBuilder())
            {
                archiveFileName = Path.Combine(Path.GetTempPath(), filename);

                var deploymentPlanResult = new DeploymentPlanResult(fileBuilder);
                await _deploymentManager.ExecuteDeploymentPlanAsync(deploymentPlan, deploymentPlanResult);

                if (System.IO.File.Exists(archiveFileName))
                {
                    System.IO.File.Delete(archiveFileName);
                }

                ZipFile.CreateFromDirectory(fileBuilder.Folder, archiveFileName);
            }

            HttpResponseMessage response;

            try
            {
                var archiveContainer = new ArchiveContainer();
                archiveContainer.ClientName    = remoteInstance.ClientName;
                archiveContainer.ApiKey        = remoteInstance.ApiKey;
                archiveContainer.ArchiveBase64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(archiveFileName));

                using (var httpClient = new HttpClient())
                {
                    var content = new StringContent(JsonConvert.SerializeObject(archiveContainer));
                    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
                    response = await httpClient.PostAsync(remoteInstance.Url, content);
                }

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    _notifier.Success(H["Deployment executed successfully."]);
                }
                else
                {
                    _notifier.Error(H["An error occured while sending the deployment to the remote instance: \"{0} ({1})\"", response.ReasonPhrase, (int)response.StatusCode]);
                }
            }
            finally
            {
                System.IO.File.Delete(archiveFileName);
            }

            return(RedirectToAction("Display", "DeploymentPlan", new { area = "Orchard.Deployment", id }));
        }