https://dev.azure.com/umbraco/D-Team%20Tracker/_workitems/edit/6587 - Moved PackageController and added VersionConverter on AngularJsonMediaTypeFormatter, instead of having single attribute only used for PackageController
This commit is contained in:
@@ -1,65 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Semver;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Hosting;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Mapping;
|
||||
using Umbraco.Core.Models.Packaging;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Services;
|
||||
using Umbraco.Core.Strings;
|
||||
using Umbraco.Web.Mvc;
|
||||
using Umbraco.Web.Routing;
|
||||
using Umbraco.Web.WebApi;
|
||||
using Umbraco.Web.WebApi.Filters;
|
||||
using Umbraco.Web.BackOffice.Filters;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
using Umbraco.Web.Common.Exceptions;
|
||||
using Umbraco.Web.Editors;
|
||||
|
||||
namespace Umbraco.Web.Editors
|
||||
namespace Umbraco.Web.BackOffice.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// A controller used for managing packages in the back office
|
||||
/// </summary>
|
||||
[PluginController("UmbracoApi")]
|
||||
[SerializeVersion]
|
||||
[UmbracoApplicationAuthorize(Core.Constants.Applications.Packages)]
|
||||
[TypeFilter(typeof(UmbracoApplicationAuthorizeAttribute), Arguments = new object[]{new string[]{Constants.Applications.Packages}})]
|
||||
public class PackageController : UmbracoAuthorizedJsonController
|
||||
{
|
||||
private readonly IIOHelper _ioHelper;
|
||||
private readonly IHostingEnvironment _hostingEnvironment;
|
||||
private readonly IPackagingService _packagingService;
|
||||
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
|
||||
|
||||
public PackageController(
|
||||
IGlobalSettings globalSettings,
|
||||
IUmbracoContextAccessor umbracoContextAccessor,
|
||||
ISqlContext sqlContext,
|
||||
ServiceContext services,
|
||||
AppCaches appCaches,
|
||||
IProfilingLogger logger,
|
||||
IRuntimeState runtimeState,
|
||||
IShortStringHelper shortStringHelper,
|
||||
UmbracoMapper umbracoMapper,
|
||||
IIOHelper ioHelper,
|
||||
IPublishedUrlProvider publishedUrlProvider)
|
||||
: base(globalSettings, umbracoContextAccessor, sqlContext, services, appCaches, logger, runtimeState, shortStringHelper, umbracoMapper, publishedUrlProvider)
|
||||
IHostingEnvironment hostingEnvironment,
|
||||
IPackagingService packagingService,
|
||||
IUmbracoContextAccessor umbracoContextAccessor)
|
||||
{
|
||||
_ioHelper = ioHelper;
|
||||
_hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||
_packagingService = packagingService ?? throw new ArgumentNullException(nameof(packagingService));
|
||||
_umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor));
|
||||
}
|
||||
|
||||
public IEnumerable<PackageDefinition> GetCreatedPackages()
|
||||
{
|
||||
return Services.PackagingService.GetAllCreatedPackages();
|
||||
return _packagingService.GetAllCreatedPackages();
|
||||
}
|
||||
|
||||
public PackageDefinition GetCreatedPackageById(int id)
|
||||
{
|
||||
var package = Services.PackagingService.GetCreatedPackageById(id);
|
||||
var package = _packagingService.GetCreatedPackageById(id);
|
||||
if (package == null)
|
||||
throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
|
||||
@@ -79,17 +68,16 @@ namespace Umbraco.Web.Editors
|
||||
public PackageDefinition PostSavePackage(PackageDefinition model)
|
||||
{
|
||||
if (ModelState.IsValid == false)
|
||||
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
||||
throw HttpResponseException.CreateValidationErrorResponse(ModelState);
|
||||
|
||||
//save it
|
||||
if (!Services.PackagingService.SaveCreatedPackage(model))
|
||||
throw new HttpResponseException(
|
||||
Request.CreateNotificationValidationErrorResponse(
|
||||
if (!_packagingService.SaveCreatedPackage(model))
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse(
|
||||
model.Id == default
|
||||
? $"A package with the name {model.Name} already exists"
|
||||
: $"The package with id {model.Id} was not found"));
|
||||
: $"The package with id {model.Id} was not found");
|
||||
|
||||
Services.PackagingService.ExportCreatedPackage(model);
|
||||
_packagingService.ExportCreatedPackage(model);
|
||||
|
||||
//the packagePath will be on the model
|
||||
return model;
|
||||
@@ -102,55 +90,48 @@ namespace Umbraco.Web.Editors
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
[HttpDelete]
|
||||
public IHttpActionResult DeleteCreatedPackage(int packageId)
|
||||
public IActionResult DeleteCreatedPackage(int packageId)
|
||||
{
|
||||
Services.PackagingService.DeleteCreatedPackage(packageId, Security.GetUserId().ResultOr(0));
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
_packagingService.DeleteCreatedPackage(packageId, umbracoContext.Security.GetUserId().ResultOr(0));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public HttpResponseMessage DownloadCreatedPackage(int id)
|
||||
public IActionResult DownloadCreatedPackage(int id)
|
||||
{
|
||||
var package = Services.PackagingService.GetCreatedPackageById(id);
|
||||
var package = _packagingService.GetCreatedPackageById(id);
|
||||
if (package == null)
|
||||
return Request.CreateResponse(HttpStatusCode.NotFound);
|
||||
return NotFound();
|
||||
|
||||
var fullPath = _ioHelper.MapPath(package.PackagePath);
|
||||
if (!File.Exists(fullPath))
|
||||
return Request.CreateNotificationValidationErrorResponse("No file found for path " + package.PackagePath);
|
||||
var fullPath = _hostingEnvironment.MapPathContentRoot(package.PackagePath);
|
||||
if (!System.IO.File.Exists(fullPath))
|
||||
throw HttpResponseException.CreateNotificationValidationErrorResponse("No file found for path " + package.PackagePath);
|
||||
|
||||
var fileName = Path.GetFileName(package.PackagePath);
|
||||
|
||||
var encoding = Encoding.UTF8;
|
||||
|
||||
var response = new HttpResponseMessage
|
||||
var cd = new System.Net.Mime.ContentDisposition
|
||||
{
|
||||
Content = new StreamContent(File.OpenRead(fullPath))
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
ContentDisposition = new ContentDispositionHeaderValue("attachment")
|
||||
{
|
||||
FileName = HttpUtility.UrlEncode(fileName, encoding)
|
||||
},
|
||||
ContentType = new MediaTypeHeaderValue("application/octet-stream")
|
||||
{
|
||||
CharSet = encoding.WebName
|
||||
}
|
||||
}
|
||||
}
|
||||
FileName = HttpUtility.UrlEncode(fileName, encoding),
|
||||
Inline = false // false = prompt the user for downloading; true = browser to try to show the file inline
|
||||
};
|
||||
|
||||
Response.Headers.Add("Content-Disposition", cd.ToString());
|
||||
// Set custom header so umbRequestHelper.downloadFile can save the correct filename
|
||||
response.Headers.Add("x-filename", HttpUtility.UrlEncode(fileName, encoding));
|
||||
Response.Headers.Add("x-filename", HttpUtility.UrlEncode(fileName, encoding));
|
||||
return new FileStreamResult(System.IO.File.OpenRead(fullPath), new MediaTypeHeaderValue("application/octet-stream")
|
||||
{
|
||||
Charset = encoding.WebName,
|
||||
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public PackageDefinition GetInstalledPackageById(int id)
|
||||
{
|
||||
var pack = Services.PackagingService.GetInstalledPackageById(id);
|
||||
var pack = _packagingService.GetInstalledPackageById(id);
|
||||
if (pack == null) throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
return pack;
|
||||
}
|
||||
@@ -161,7 +142,7 @@ namespace Umbraco.Web.Editors
|
||||
/// <returns></returns>
|
||||
public IEnumerable<PackageDefinition> GetInstalled()
|
||||
{
|
||||
return Services.PackagingService.GetAllInstalledPackages()
|
||||
return _packagingService.GetAllInstalledPackages()
|
||||
.GroupBy(
|
||||
//group by name
|
||||
x => x.Name,
|
||||
@@ -3,6 +3,7 @@ using System.IO;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace Umbraco.Web.Common.Formatters
|
||||
{
|
||||
@@ -19,6 +20,7 @@ namespace Umbraco.Web.Common.Formatters
|
||||
public AngularJsonMediaTypeFormatter(JsonSerializerSettings serializerSettings, ArrayPool<char> charPool, MvcOptions mvcOptions)
|
||||
: base(serializerSettings, charPool, mvcOptions)
|
||||
{
|
||||
serializerSettings.Converters.Add(new VersionConverter());
|
||||
}
|
||||
|
||||
protected override JsonWriter CreateJsonWriter(TextWriter writer)
|
||||
|
||||
Reference in New Issue
Block a user