Updates the PackageRepositoryController to be able to return a packages file

This commit is contained in:
Shannon
2017-04-26 17:58:35 +10:00
parent 0bd31c3fe0
commit a0d9c0006d
2 changed files with 51 additions and 13 deletions
@@ -9,8 +9,11 @@ using Umbraco.Core.Cache;
using Umbraco.Web.WebApi;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
using OurUmbraco.Wiki.BusinessLogic;
using Semver;
using Umbraco.Core;
using Umbraco.Web;
namespace OurUmbraco.Repository.Controllers
{
@@ -95,8 +98,9 @@ namespace OurUmbraco.Repository.Controllers
/// </summary>
/// <param name="id"></param>
/// <param name="version">The umbraco version requesting the details, if null than the ZipUrl will be the latest package zip</param>
/// <param name="asFile">pass in true to get the package file otherwise leave blank or set to false to retreive the package details</param>
/// <returns></returns>
public PackageDetails GetDetails(Guid id, string version = null)
public HttpResponseMessage Get(Guid id, string version = null, bool? asFile = false)
{
SemVersion parsed = null;
if (version.IsNullOrWhiteSpace() == false)
@@ -118,29 +122,60 @@ namespace OurUmbraco.Repository.Controllers
var v = new System.Version(parsed.Major, parsed.Minor, parsed.Patch);
return asFile.HasValue && asFile.Value
? GetPackageFile(id, v)
: GetDetails(id, v);
}
private HttpResponseMessage GetDetails(Guid id, System.Version currUmbracoVersion)
{
//return the results, but cache for 1 minute
var key = string.Format("PackageRepositoryController.GetDetails.{0}.{1}", id, v.ToString(3));
var key = string.Format("PackageRepositoryController.GetDetails.{0}.{1}", id, currUmbracoVersion.ToString(3));
var package = ApplicationContext.ApplicationCache.RuntimeCache.GetCacheItem<PackageDetails>
(key,
() =>
{
var details = Service.GetDetails(id, v);
(key,
() =>
{
var details = Service.GetDetails(id, currUmbracoVersion);
if (details.ZipUrl.IsNullOrWhiteSpace())
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "This package is not compatible with the Umbraco version " + version));
if (details.ZipUrl.IsNullOrWhiteSpace())
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "This package is not compatible with the Umbraco version " + currUmbracoVersion));
return details;
},
TimeSpan.FromMinutes(1)); //cache for 1 min
return details;
},
TimeSpan.FromMinutes(1)); //cache for 1 min
if (package == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return package;
return Request.CreateResponse(HttpStatusCode.OK, package);
}
private HttpResponseMessage GetPackageFile(Guid packageId, System.Version currUmbracoVersion)
{
var pckRepoService = new PackageRepositoryService(Umbraco, Members, DatabaseContext);
var details = pckRepoService.GetDetails(packageId, currUmbracoVersion);
if (details == null)
throw new InvalidOperationException("No package found with id " + packageId);
if (details.ZipUrl.IsNullOrWhiteSpace())
throw new InvalidOperationException("This package is not compatible with the Umbraco version " + currUmbracoVersion);
var wf = new WikiFile(details.ZipFileId);
if (wf == null)
throw new InvalidOperationException("Could not find wiki file by id " + details.ZipFileId);
wf.UpdateDownloadCounter(true, true);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(wf.ToByteArray())
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
}
}
@@ -497,6 +497,9 @@ namespace OurUmbraco.Wiki.BusinessLogic
byte[] packageByteArray;
if(File.Exists(path) == false)
throw new InvalidOperationException("The file " + path + " does not exist on the server");
using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read))
{
packageByteArray = new byte[fileStream.Length];