2020-12-29 12:55:48 +01:00
|
|
|
using System;
|
2020-05-27 08:20:41 +02:00
|
|
|
using System.Collections.Generic;
|
2019-01-10 18:51:02 +11:00
|
|
|
using System.IO;
|
2019-01-16 01:53:08 +11:00
|
|
|
using System.Linq;
|
2018-12-06 09:54:21 +00:00
|
|
|
using System.Net;
|
2019-04-30 20:47:37 +02:00
|
|
|
using System.Text;
|
2020-11-19 22:17:42 +11:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-05-27 08:20:41 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Net.Http.Headers;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Packaging;
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
using Umbraco.Cms.Core.Services;
|
|
|
|
|
using Umbraco.Cms.Web.Common.ActionsResults;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Attributes;
|
|
|
|
|
using Umbraco.Cms.Web.Common.Authorization;
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2021-06-17 16:56:51 -06:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-08-18 12:01:56 -06:00
|
|
|
using Umbraco.Cms.Infrastructure.Install;
|
2018-12-06 09:50:05 +00:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Web.BackOffice.Controllers
|
2018-12-06 09:50:05 +00:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-01-15 22:08:08 +11:00
|
|
|
/// A controller used for managing packages in the back office
|
2018-12-06 09:50:05 +00:00
|
|
|
/// </summary>
|
2020-06-09 13:01:05 +10:00
|
|
|
[PluginController(Constants.Web.Mvc.BackOfficeApiArea)]
|
2020-11-19 22:17:42 +11:00
|
|
|
[Authorize(Policy = AuthorizationPolicies.SectionAccessPackages)]
|
2018-12-06 09:50:05 +00:00
|
|
|
public class PackageController : UmbracoAuthorizedJsonController
|
|
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
private readonly IPackagingService _packagingService;
|
2020-10-21 16:51:00 +11:00
|
|
|
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
2021-08-18 12:01:56 -06:00
|
|
|
private readonly PackageMigrationRunner _packageMigrationRunner;
|
2021-06-17 16:56:51 -06:00
|
|
|
private readonly ILogger<PackageController> _logger;
|
2020-02-10 11:23:23 +01:00
|
|
|
|
|
|
|
|
public PackageController(
|
2020-05-27 08:20:41 +02:00
|
|
|
IPackagingService packagingService,
|
2021-06-17 16:56:51 -06:00
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor,
|
2021-08-18 12:01:56 -06:00
|
|
|
PackageMigrationRunner packageMigrationRunner,
|
2021-06-17 16:56:51 -06:00
|
|
|
ILogger<PackageController> logger)
|
2020-02-10 11:23:23 +01:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
_packagingService = packagingService ?? throw new ArgumentNullException(nameof(packagingService));
|
2020-09-22 10:01:00 +02:00
|
|
|
_backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
|
2021-08-18 12:01:56 -06:00
|
|
|
_packageMigrationRunner = packageMigrationRunner;
|
2021-06-17 16:56:51 -06:00
|
|
|
_logger = logger;
|
2020-02-10 11:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 17:18:47 +11:00
|
|
|
public IEnumerable<PackageDefinition> GetCreatedPackages()
|
2018-12-06 09:50:05 +00:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
return _packagingService.GetAllCreatedPackages();
|
2018-12-06 09:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 12:55:48 +01:00
|
|
|
public ActionResult<PackageDefinition> GetCreatedPackageById(int id)
|
2018-12-06 09:50:05 +00:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
var package = _packagingService.GetCreatedPackageById(id);
|
2018-12-07 10:22:33 +00:00
|
|
|
if (package == null)
|
2021-01-12 14:00:14 +01:00
|
|
|
return NotFound();
|
2019-01-10 18:51:02 +11:00
|
|
|
|
2019-01-10 17:18:47 +11:00
|
|
|
return package;
|
2018-12-06 09:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-08 13:11:39 -06:00
|
|
|
public PackageDefinition GetEmpty() => new PackageDefinition();
|
2018-12-06 14:41:53 +00:00
|
|
|
|
2019-01-10 17:18:47 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates or updates a package
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-12-29 12:55:48 +01:00
|
|
|
public ActionResult<PackageDefinition> PostSavePackage(PackageDefinition model)
|
2019-01-10 12:44:57 +11:00
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid == false)
|
2021-06-25 10:29:18 -06:00
|
|
|
return ValidationProblem(ModelState);
|
2018-12-06 11:21:28 +00:00
|
|
|
|
2019-01-10 12:44:57 +11:00
|
|
|
//save it
|
2020-05-27 08:20:41 +02:00
|
|
|
if (!_packagingService.SaveCreatedPackage(model))
|
2021-06-25 10:29:18 -06:00
|
|
|
{
|
|
|
|
|
return ValidationProblem(
|
2019-01-10 19:27:25 +11:00
|
|
|
model.Id == default
|
|
|
|
|
? $"A package with the name {model.Name} already exists"
|
2020-05-27 08:20:41 +02:00
|
|
|
: $"The package with id {model.Id} was not found");
|
2021-06-25 10:29:18 -06:00
|
|
|
}
|
2019-01-10 12:44:57 +11:00
|
|
|
|
2020-05-27 08:20:41 +02:00
|
|
|
_packagingService.ExportCreatedPackage(model);
|
2019-01-10 17:18:47 +11:00
|
|
|
|
2019-11-05 12:54:22 +01:00
|
|
|
//the packagePath will be on the model
|
2019-01-10 12:44:57 +11:00
|
|
|
return model;
|
2018-12-06 10:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-06 13:09:34 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes a created package
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="packageId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
2018-12-06 09:54:21 +00:00
|
|
|
[HttpDelete]
|
2020-05-27 08:20:41 +02:00
|
|
|
public IActionResult DeleteCreatedPackage(int packageId)
|
2018-12-06 09:54:21 +00:00
|
|
|
{
|
2022-02-10 10:32:45 +01:00
|
|
|
_packagingService.DeleteCreatedPackage(packageId, _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().Result ?? -1);
|
2018-12-06 09:54:21 +00:00
|
|
|
|
2018-12-06 13:09:34 +00:00
|
|
|
return Ok();
|
2018-12-06 09:54:21 +00:00
|
|
|
}
|
2019-01-10 18:51:02 +11:00
|
|
|
|
2021-06-17 16:56:51 -06:00
|
|
|
[HttpPost]
|
2021-08-18 12:01:56 -06:00
|
|
|
public ActionResult<IEnumerable<InstalledPackage>> RunMigrations([FromQuery]string packageName)
|
2021-06-17 16:56:51 -06:00
|
|
|
{
|
2021-08-18 12:01:56 -06:00
|
|
|
try
|
2021-06-17 16:56:51 -06:00
|
|
|
{
|
2021-08-18 12:01:56 -06:00
|
|
|
_packageMigrationRunner.RunPackageMigrationsIfPending(packageName);
|
|
|
|
|
return _packagingService.GetAllInstalledPackages().ToList();
|
2021-06-17 16:56:51 -06:00
|
|
|
}
|
2021-08-18 12:01:56 -06:00
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Package migration failed on package {Package}", packageName);
|
2021-06-17 16:56:51 -06:00
|
|
|
|
2021-08-18 12:01:56 -06:00
|
|
|
return ValidationErrorResult.CreateNotificationValidationErrorResult(
|
|
|
|
|
$"Package migration failed on package {packageName} with error: {ex.Message}. Check log for full details.");
|
2022-02-10 10:32:45 +01:00
|
|
|
}
|
2021-06-17 16:56:51 -06:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 18:51:02 +11:00
|
|
|
[HttpGet]
|
2022-02-10 10:32:45 +01:00
|
|
|
public IActionResult DownloadCreatedPackage(int id)
|
2019-01-10 18:51:02 +11:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
var package = _packagingService.GetCreatedPackageById(id);
|
2019-01-10 18:51:02 +11:00
|
|
|
if (package == null)
|
2020-05-27 08:20:41 +02:00
|
|
|
return NotFound();
|
2019-01-10 18:51:02 +11:00
|
|
|
|
2021-06-09 15:50:40 +02:00
|
|
|
if (!System.IO.File.Exists(package.PackagePath))
|
2021-06-25 10:29:18 -06:00
|
|
|
return ValidationProblem("No file found for path " + package.PackagePath);
|
2019-01-10 18:51:02 +11:00
|
|
|
|
|
|
|
|
var fileName = Path.GetFileName(package.PackagePath);
|
|
|
|
|
|
2019-04-30 20:47:37 +02:00
|
|
|
var encoding = Encoding.UTF8;
|
|
|
|
|
|
2020-05-27 08:20:41 +02:00
|
|
|
var cd = new System.Net.Mime.ContentDisposition
|
2019-01-10 18:51:02 +11:00
|
|
|
{
|
2021-06-25 10:29:18 -06:00
|
|
|
FileName = WebUtility.UrlEncode(fileName),
|
2020-05-27 08:20:41 +02:00
|
|
|
Inline = false // false = prompt the user for downloading; true = browser to try to show the file inline
|
2019-01-10 18:51:02 +11:00
|
|
|
};
|
2020-05-27 08:20:41 +02:00
|
|
|
Response.Headers.Add("Content-Disposition", cd.ToString());
|
2019-01-10 18:51:02 +11:00
|
|
|
// Set custom header so umbRequestHelper.downloadFile can save the correct filename
|
2020-08-05 08:44:22 +02:00
|
|
|
Response.Headers.Add("x-filename", WebUtility.UrlEncode(fileName));
|
2021-06-09 15:50:40 +02:00
|
|
|
return new FileStreamResult(System.IO.File.OpenRead(package.PackagePath), new MediaTypeHeaderValue("application/octet-stream")
|
2020-05-27 08:20:41 +02:00
|
|
|
{
|
|
|
|
|
Charset = encoding.WebName,
|
|
|
|
|
});
|
2019-01-10 18:51:02 +11:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 12:45:22 -06:00
|
|
|
public ActionResult<InstalledPackage> GetInstalledPackageByName([FromQuery] string packageName)
|
|
|
|
|
{
|
|
|
|
|
InstalledPackage pack = _packagingService.GetInstalledPackageByName(packageName);
|
|
|
|
|
if (pack == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pack;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 01:53:08 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns all installed packages - only shows their latest versions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-06-16 15:34:20 -06:00
|
|
|
public IEnumerable<InstalledPackage> GetInstalled()
|
|
|
|
|
=> _packagingService.GetAllInstalledPackages().ToList();
|
2018-12-06 09:50:05 +00:00
|
|
|
}
|
|
|
|
|
}
|