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;
|
2019-01-16 17:25:46 +11:00
|
|
|
using Semver;
|
2020-02-10 11:23:23 +01:00
|
|
|
using Umbraco.Core;
|
2020-05-27 08:20:41 +02:00
|
|
|
using Umbraco.Core.Hosting;
|
2019-01-10 12:44:57 +11:00
|
|
|
using Umbraco.Core.Models.Packaging;
|
2020-09-22 10:01:00 +02:00
|
|
|
using Umbraco.Core.Security;
|
2020-02-10 11:23:23 +01:00
|
|
|
using Umbraco.Core.Services;
|
2020-12-29 12:55:48 +01:00
|
|
|
using Umbraco.Web.Common.ActionsResults;
|
2020-05-27 08:20:41 +02:00
|
|
|
using Umbraco.Web.Common.Attributes;
|
2020-11-20 12:40:29 +11:00
|
|
|
using Umbraco.Web.Common.Authorization;
|
2018-12-06 09:50:05 +00:00
|
|
|
|
2020-05-27 08:20:41 +02:00
|
|
|
namespace Umbraco.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 IHostingEnvironment _hostingEnvironment;
|
|
|
|
|
private readonly IPackagingService _packagingService;
|
2020-10-21 16:51:00 +11:00
|
|
|
private readonly IBackOfficeSecurityAccessor _backofficeSecurityAccessor;
|
2020-02-10 11:23:23 +01:00
|
|
|
|
|
|
|
|
public PackageController(
|
2020-05-27 08:20:41 +02:00
|
|
|
IHostingEnvironment hostingEnvironment,
|
|
|
|
|
IPackagingService packagingService,
|
2020-10-21 16:51:00 +11:00
|
|
|
IBackOfficeSecurityAccessor backofficeSecurityAccessor)
|
2020-02-10 11:23:23 +01:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
|
|
|
|
_packagingService = packagingService ?? throw new ArgumentNullException(nameof(packagingService));
|
2020-09-22 10:01:00 +02:00
|
|
|
_backofficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor));
|
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
|
|
|
}
|
|
|
|
|
|
2019-01-10 17:18:47 +11:00
|
|
|
public PackageDefinition GetEmpty()
|
2018-12-06 10:18:31 +00:00
|
|
|
{
|
2019-01-10 17:18:47 +11:00
|
|
|
return new PackageDefinition();
|
2019-01-10 12:44:57 +11:00
|
|
|
}
|
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)
|
2020-12-29 12:55:48 +01:00
|
|
|
return new ValidationErrorResult(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))
|
2020-12-29 12:55:48 +01:00
|
|
|
return ValidationErrorResult.CreateNotificationValidationErrorResult(
|
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");
|
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
|
|
|
{
|
2020-10-21 16:51:00 +11:00
|
|
|
_packagingService.DeleteCreatedPackage(packageId, _backofficeSecurityAccessor.BackOfficeSecurity.GetUserId().ResultOr(0));
|
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
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-05-27 08:20:41 +02: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
|
|
|
|
2020-12-16 22:26:47 +01:00
|
|
|
var fullPath = _hostingEnvironment.MapPathWebRoot(package.PackagePath);
|
2020-05-27 08:20:41 +02:00
|
|
|
if (!System.IO.File.Exists(fullPath))
|
2020-12-29 12:55:48 +01:00
|
|
|
return ValidationErrorResult.CreateNotificationValidationErrorResult("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
|
|
|
{
|
2020-08-05 08:44:22 +02: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));
|
2020-05-27 08:20:41 +02:00
|
|
|
return new FileStreamResult(System.IO.File.OpenRead(fullPath), new MediaTypeHeaderValue("application/octet-stream")
|
|
|
|
|
{
|
|
|
|
|
Charset = encoding.WebName,
|
|
|
|
|
});
|
2019-01-10 18:51:02 +11:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 12:55:48 +01:00
|
|
|
public ActionResult<PackageDefinition> GetInstalledPackageById(int id)
|
2019-01-16 21:27:14 +11:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
var pack = _packagingService.GetInstalledPackageById(id);
|
2021-01-12 14:00:14 +01:00
|
|
|
if (pack == null) return NotFound();
|
2019-01-16 21:27:14 +11:00
|
|
|
return pack;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 01:53:08 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns all installed packages - only shows their latest versions
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2019-01-16 21:27:14 +11:00
|
|
|
public IEnumerable<PackageDefinition> GetInstalled()
|
2019-01-16 01:53:08 +11:00
|
|
|
{
|
2020-05-27 08:20:41 +02:00
|
|
|
return _packagingService.GetAllInstalledPackages()
|
2019-01-16 01:53:08 +11:00
|
|
|
.GroupBy(
|
|
|
|
|
//group by name
|
|
|
|
|
x => x.Name,
|
|
|
|
|
//select the package with a parsed version
|
2019-01-16 17:25:46 +11:00
|
|
|
pck => SemVersion.TryParse(pck.Version, out var pckVersion)
|
2019-01-16 01:53:08 +11:00
|
|
|
? new { package = pck, version = pckVersion }
|
2019-01-16 17:25:46 +11:00
|
|
|
: new { package = pck, version = new SemVersion(0, 0, 0) })
|
2019-01-16 01:53:08 +11:00
|
|
|
.Select(grouping =>
|
|
|
|
|
{
|
|
|
|
|
//get the max version for the package
|
|
|
|
|
var maxVersion = grouping.Max(x => x.version);
|
|
|
|
|
//only return the first package with this version
|
|
|
|
|
return grouping.First(x => x.version == maxVersion).package;
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2018-12-06 09:50:05 +00:00
|
|
|
}
|
|
|
|
|
}
|