2017-01-09 22:54:28 +01:00
|
|
|
using AutoMapper;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
using Umbraco.Core.Models;
|
2017-01-16 16:50:22 +01:00
|
|
|
using Umbraco.Core.Services;
|
2017-01-09 22:54:28 +01:00
|
|
|
using Umbraco.Web.Models.ContentEditing;
|
|
|
|
|
using Umbraco.Web.Mvc;
|
|
|
|
|
using Umbraco.Web.WebApi;
|
2017-01-16 16:50:22 +01:00
|
|
|
using Umbraco.Web.WebApi.Filters;
|
2017-01-09 22:54:28 +01:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Editors
|
|
|
|
|
{
|
|
|
|
|
[PluginController("UmbracoApi")]
|
2017-01-16 16:50:22 +01:00
|
|
|
[PrefixlessBodyModelValidator]
|
|
|
|
|
[UmbracoApplicationAuthorizeAttribute(Core.Constants.Applications.Settings)]
|
|
|
|
|
public class CodeFileController : BackOfficeNotificationsController
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
|
|
|
|
|
2017-01-16 16:50:22 +01:00
|
|
|
[ValidationFilter]
|
2017-01-09 22:54:28 +01:00
|
|
|
public HttpResponseMessage PostCreate(string type, CodeFileDisplay display)
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
case Core.Constants.Trees.PartialViews:
|
2017-01-09 22:54:28 +01:00
|
|
|
var view = new PartialView(display.VirtualPath);
|
2017-01-16 16:50:22 +01:00
|
|
|
var result = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id);
|
|
|
|
|
return result.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(result.Exception.Message);
|
|
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
2017-01-09 22:54:28 +01:00
|
|
|
var viewMacro = new PartialView(display.VirtualPath);
|
|
|
|
|
var resultMacro = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id);
|
2017-01-16 16:50:22 +01:00
|
|
|
return resultMacro.Success == true ? Request.CreateResponse(HttpStatusCode.OK) : Request.CreateNotificationValidationErrorResponse(resultMacro.Exception.Message);
|
|
|
|
|
case Core.Constants.Trees.Scripts:
|
2017-01-09 22:54:28 +01:00
|
|
|
var script = new Script(display.VirtualPath);
|
|
|
|
|
Services.FileService.SaveScript(script, Security.CurrentUser.Id);
|
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK);
|
|
|
|
|
default:
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CodeFileDisplay GetByPath(string type, string virtualPath)
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(type) == false && string.IsNullOrWhiteSpace(virtualPath) == false)
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
2017-01-19 10:41:08 +01:00
|
|
|
virtualPath = System.Web.HttpUtility.UrlDecode(virtualPath);
|
2017-01-16 16:50:22 +01:00
|
|
|
if (type == Core.Constants.Trees.PartialViews)
|
|
|
|
|
{
|
2017-01-09 22:54:28 +01:00
|
|
|
var view = Services.FileService.GetPartialView(virtualPath);
|
2017-01-16 16:50:22 +01:00
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
var display = Mapper.Map<IPartialView, CodeFileDisplay>(view);
|
|
|
|
|
display.FileType = Core.Constants.Trees.PartialViews;
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (type == Core.Constants.Trees.PartialViewMacros)
|
|
|
|
|
{
|
2017-01-09 22:54:28 +01:00
|
|
|
var viewMacro = Services.FileService.GetPartialViewMacro(virtualPath);
|
2017-01-16 16:50:22 +01:00
|
|
|
if (viewMacro != null)
|
|
|
|
|
{
|
|
|
|
|
var display = Mapper.Map<IPartialView, CodeFileDisplay>(viewMacro);
|
|
|
|
|
display.FileType = Core.Constants.Trees.PartialViewMacros;
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (type == Core.Constants.Trees.Scripts)
|
|
|
|
|
{
|
2017-01-09 22:54:28 +01:00
|
|
|
var script = Services.FileService.GetScriptByName(virtualPath);
|
2017-01-16 16:50:22 +01:00
|
|
|
if (script != null)
|
|
|
|
|
{
|
|
|
|
|
var display = Mapper.Map<Script, CodeFileDisplay>(script);
|
|
|
|
|
display.FileType = Core.Constants.Trees.Scripts;
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
2017-01-16 16:50:22 +01:00
|
|
|
[HttpPost]
|
2017-01-09 22:54:28 +01:00
|
|
|
public HttpResponseMessage Delete(string type, string virtualPath)
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
if (string.IsNullOrWhiteSpace(type) == false && string.IsNullOrWhiteSpace(virtualPath) == false)
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
case Core.Constants.Trees.PartialViews:
|
|
|
|
|
if (Services.FileService.DeletePartialView(virtualPath, Security.CurrentUser.Id))
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.OK);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View found with the specified path");
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
break;
|
2017-01-16 16:50:22 +01:00
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
|
|
|
|
if (Services.FileService.DeletePartialViewMacro(virtualPath, Security.CurrentUser.Id))
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.OK);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View Macro found with the specified path");
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
break;
|
2017-01-16 16:50:22 +01:00
|
|
|
case Core.Constants.Trees.Scripts:
|
|
|
|
|
if (Services.FileService.GetScriptByName(virtualPath) != null)
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
Services.FileService.DeleteScript(virtualPath, Security.CurrentUser.Id);
|
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Script found with the specified path");
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CodeFileDisplay PostSave(CodeFileDisplay display)
|
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid == false)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (display == null && string.IsNullOrWhiteSpace(display.FileType) == true)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (display.FileType == Core.Constants.Trees.PartialViews)
|
|
|
|
|
{
|
|
|
|
|
var view = Services.FileService.GetPartialView(display.VirtualPath);
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
2017-01-19 10:41:08 +01:00
|
|
|
// might need to find the path
|
|
|
|
|
var orgPath = view.OriginalPath.Substring(0, view.OriginalPath.IndexOf(view.Name));
|
|
|
|
|
view.Path = orgPath + display.Name;
|
|
|
|
|
|
2017-01-16 16:50:22 +01:00
|
|
|
view.Content = display.Content;
|
|
|
|
|
var result = Services.FileService.SavePartialView(view, Security.CurrentUser.Id);
|
|
|
|
|
if (result.Success == true)
|
|
|
|
|
{
|
|
|
|
|
return Mapper.Map(view, display);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
display.AddErrorNotification(
|
|
|
|
|
Services.TextService.Localize("speechBubbles/partialViewErrorHeader"),
|
|
|
|
|
Services.TextService.Localize("speechBubbles/partialViewErrorText"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (display.FileType == Core.Constants.Trees.PartialViewMacros)
|
|
|
|
|
{
|
|
|
|
|
var viewMacro = Services.FileService.GetPartialViewMacro(display.VirtualPath);
|
|
|
|
|
if (viewMacro != null)
|
|
|
|
|
{
|
|
|
|
|
viewMacro.Content = display.Content;
|
2017-01-19 09:49:27 +01:00
|
|
|
viewMacro.Path = display.Name;
|
2017-01-16 16:50:22 +01:00
|
|
|
var result = Services.FileService.SavePartialViewMacro(viewMacro, Security.CurrentUser.Id);
|
|
|
|
|
if (result.Success == false)
|
|
|
|
|
{
|
|
|
|
|
display.AddErrorNotification(
|
2017-01-19 09:49:27 +01:00
|
|
|
Services.TextService.Localize("speechBubbles/macroPartialViewErrorHeader"),
|
|
|
|
|
Services.TextService.Localize("speechBubbles/macroPartialViewErrorText"));
|
2017-01-16 16:50:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (display.FileType == Core.Constants.Trees.Scripts)
|
|
|
|
|
{
|
|
|
|
|
var script = Services.FileService.GetScriptByName(display.VirtualPath);
|
|
|
|
|
if (script != null)
|
|
|
|
|
{
|
|
|
|
|
script.Content = display.Content;
|
2017-01-19 09:49:27 +01:00
|
|
|
script.Path = display.Name;
|
2017-01-16 16:50:22 +01:00
|
|
|
Services.FileService.SaveScript(script, Security.CurrentUser.Id);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
return display;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|