2018-12-06 09:54:21 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-01-10 18:51:02 +11:00
|
|
|
|
using System.IO;
|
2018-12-06 09:54:21 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Net.Http;
|
2019-01-10 18:51:02 +11:00
|
|
|
|
using System.Net.Http.Headers;
|
2018-12-06 09:50:05 +00:00
|
|
|
|
using System.Web.Http;
|
2019-01-10 18:51:02 +11:00
|
|
|
|
using Umbraco.Core.IO;
|
2019-01-10 12:44:57 +11:00
|
|
|
|
using Umbraco.Core.Models.Packaging;
|
2018-12-06 09:50:05 +00:00
|
|
|
|
using Umbraco.Web.Mvc;
|
2018-12-06 13:54:38 +00:00
|
|
|
|
using Umbraco.Web.WebApi;
|
2018-12-06 09:50:05 +00:00
|
|
|
|
using Umbraco.Web.WebApi.Filters;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <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>
|
|
|
|
|
|
[PluginController("UmbracoApi")]
|
2018-12-06 13:54:38 +00:00
|
|
|
|
[SerializeVersion]
|
2018-12-06 09:50:05 +00:00
|
|
|
|
[UmbracoApplicationAuthorize(Core.Constants.Applications.Packages)]
|
|
|
|
|
|
public class PackageController : UmbracoAuthorizedJsonController
|
|
|
|
|
|
{
|
2019-01-10 17:18:47 +11:00
|
|
|
|
public IEnumerable<PackageDefinition> GetCreatedPackages()
|
2018-12-06 09:50:05 +00:00
|
|
|
|
{
|
2019-01-11 10:35:37 +11:00
|
|
|
|
return Services.PackagingService.GetAllCreatedPackages();
|
2018-12-06 09:50:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-10 12:44:57 +11:00
|
|
|
|
public PackageDefinition GetCreatedPackageById(int id)
|
2018-12-06 09:50:05 +00:00
|
|
|
|
{
|
2019-01-11 10:35:37 +11:00
|
|
|
|
var package = Services.PackagingService.GetCreatedPackageById(id);
|
2018-12-07 10:22:33 +00:00
|
|
|
|
if (package == null)
|
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.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>
|
|
|
|
|
|
public PackageDefinition PostSavePackage(PackageDefinition model)
|
2019-01-10 12:44:57 +11:00
|
|
|
|
{
|
|
|
|
|
|
if (ModelState.IsValid == false)
|
|
|
|
|
|
throw new HttpResponseException(Request.CreateValidationErrorResponse(ModelState));
|
2018-12-06 11:21:28 +00:00
|
|
|
|
|
2019-01-10 12:44:57 +11:00
|
|
|
|
//save it
|
2019-01-11 10:35:37 +11:00
|
|
|
|
if (!Services.PackagingService.SaveCreatedPackage(model))
|
2019-01-10 19:27:25 +11:00
|
|
|
|
throw new HttpResponseException(
|
|
|
|
|
|
Request.CreateNotificationValidationErrorResponse(
|
|
|
|
|
|
model.Id == default
|
|
|
|
|
|
? $"A package with the name {model.Name} already exists"
|
|
|
|
|
|
: $"The package with id {model.Id} was not found"));
|
2019-01-10 12:44:57 +11:00
|
|
|
|
|
2019-01-11 10:35:37 +11:00
|
|
|
|
Services.PackagingService.ExportCreatedPackage(model);
|
2019-01-10 17:18:47 +11: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]
|
2018-12-06 13:09:34 +00:00
|
|
|
|
public IHttpActionResult DeleteCreatedPackage(int packageId)
|
2018-12-06 09:54:21 +00:00
|
|
|
|
{
|
2019-01-11 14:30:04 +11:00
|
|
|
|
Services.PackagingService.DeleteCreatedPackage(packageId, Security.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]
|
|
|
|
|
|
public HttpResponseMessage DownloadCreatedPackage(int id)
|
|
|
|
|
|
{
|
2019-01-11 10:35:37 +11:00
|
|
|
|
var package = Services.PackagingService.GetCreatedPackageById(id);
|
2019-01-10 18:51:02 +11:00
|
|
|
|
if (package == null)
|
|
|
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
|
|
|
|
|
|
|
|
|
|
|
var fullPath = IOHelper.MapPath(package.PackagePath);
|
|
|
|
|
|
if (!File.Exists(fullPath))
|
|
|
|
|
|
return Request.CreateNotificationValidationErrorResponse("No file found for path " + package.PackagePath);
|
|
|
|
|
|
|
|
|
|
|
|
var fileName = Path.GetFileName(package.PackagePath);
|
|
|
|
|
|
|
|
|
|
|
|
var response = new HttpResponseMessage
|
|
|
|
|
|
{
|
|
|
|
|
|
Content = new StreamContent(File.OpenRead(fullPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Headers =
|
|
|
|
|
|
{
|
|
|
|
|
|
ContentDisposition = new ContentDispositionHeaderValue("attachment")
|
|
|
|
|
|
{
|
|
|
|
|
|
FileName = fileName
|
|
|
|
|
|
},
|
|
|
|
|
|
ContentType = new MediaTypeHeaderValue( "application/octet-stream")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Set custom header so umbRequestHelper.downloadFile can save the correct filename
|
|
|
|
|
|
response.Headers.Add("x-filename", fileName);
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-06 09:50:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|