2017-01-09 22:54:28 +01:00
|
|
|
using AutoMapper;
|
2017-01-30 14:57:08 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-01-09 22:54:28 +01:00
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Web.Http;
|
2017-01-30 14:57:08 +01:00
|
|
|
using Umbraco.Core;
|
2017-02-01 15:46:22 +01:00
|
|
|
using Umbraco.Core.IO;
|
2017-01-09 22:54:28 +01:00
|
|
|
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-19 15:43:29 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to create a brand new file
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
|
|
|
|
|
/// <param name="display"></param>
|
|
|
|
|
/// <returns>Will return a simple 200 if file creation succeeds</returns>
|
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-02-01 12:33:20 +01:00
|
|
|
view.Content = display.Content;
|
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);
|
2017-01-19 15:43:29 +00:00
|
|
|
|
2017-01-16 16:50:22 +01:00
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
2017-01-09 22:54:28 +01:00
|
|
|
var viewMacro = new PartialView(display.VirtualPath);
|
2017-02-01 12:33:20 +01:00
|
|
|
viewMacro.Content = display.Content;
|
2017-01-09 22:54:28 +01:00
|
|
|
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);
|
2017-01-19 15:43:29 +00:00
|
|
|
|
2017-01-16 16:50:22 +01:00
|
|
|
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);
|
2017-01-19 15:43:29 +00:00
|
|
|
|
2017-01-09 22:54:28 +01:00
|
|
|
default:
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 15:43:29 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to get a specific file from disk via the FileService
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
|
|
|
|
|
/// <param name="virtualPath">The filename or urlencoded path of the file to open</param>
|
|
|
|
|
/// <returns>The file and its contents from the virtualPath</returns>
|
2017-01-09 22:54:28 +01:00
|
|
|
public CodeFileDisplay GetByPath(string type, string virtualPath)
|
|
|
|
|
{
|
2017-01-19 15:43:29 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(type) || string.IsNullOrWhiteSpace(virtualPath))
|
2017-01-09 22:54:28 +01:00
|
|
|
{
|
2017-01-19 15:43:29 +00:00
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtualPath = System.Web.HttpUtility.UrlDecode(virtualPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case 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;
|
|
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
case 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;
|
|
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
case 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;
|
|
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2017-01-16 16:50:22 +01:00
|
|
|
|
2017-01-19 15:43:29 +00:00
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-30 14:57:08 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to get a list of available templates/snippets to base a new Partial View og Partial View Macro from
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">This is a string but will be 'partialViews', 'partialViewMacros'</param>
|
|
|
|
|
/// <returns>Returns a list of <see cref="SnippetDisplay"/> if a correct type is sent</returns>
|
|
|
|
|
public IEnumerable<SnippetDisplay> GetSnippets(string type)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(type))
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<string> snippets;
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Core.Constants.Trees.PartialViews:
|
|
|
|
|
snippets = Services.FileService.GetPartialViewSnippetNames(
|
|
|
|
|
//ignore these - (this is taken from the logic in "PartialView.ascx.cs")
|
|
|
|
|
"Gallery",
|
|
|
|
|
"ListChildPagesFromChangeableSource",
|
|
|
|
|
"ListChildPagesOrderedByProperty",
|
|
|
|
|
"ListImagesFromMediaFolder");
|
|
|
|
|
break;
|
|
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
|
|
|
|
snippets = Services.FileService.GetPartialViewSnippetNames();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return snippets.Select(snippet => new SnippetDisplay() {Name = snippet.SplitPascalCasing().ToFirstUpperInvariant(), FileName = snippet});
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 12:33:20 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to scaffold the json object for the editors for 'scripts', 'partialViews', 'partialViewMacros'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
|
|
|
|
|
/// <param name="snippetName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public CodeFileDisplay GetScaffold(string type, string snippetName = null)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(type))
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeFileDisplay codeFileDisplay;
|
|
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case Core.Constants.Trees.PartialViews:
|
|
|
|
|
codeFileDisplay = Mapper.Map<IPartialView, CodeFileDisplay>(new PartialView(string.Empty));
|
|
|
|
|
if (snippetName.IsNullOrWhiteSpace() == false)
|
|
|
|
|
codeFileDisplay.Content = Services.FileService.GetPartialViewSnippetContent(snippetName);
|
|
|
|
|
break;
|
|
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
|
|
|
|
codeFileDisplay = Mapper.Map<IPartialView, CodeFileDisplay>(new PartialView(string.Empty));
|
|
|
|
|
if (snippetName.IsNullOrWhiteSpace() == false)
|
|
|
|
|
codeFileDisplay.Content = Services.FileService.GetPartialViewMacroSnippetContent(snippetName);
|
|
|
|
|
break;
|
|
|
|
|
case Core.Constants.Trees.Scripts:
|
|
|
|
|
codeFileDisplay = Mapper.Map<Script, CodeFileDisplay>(new Script(string.Empty));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Unsupported editortype"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
codeFileDisplay.FileType = type;
|
|
|
|
|
codeFileDisplay.VirtualPath = "-1";
|
|
|
|
|
|
|
|
|
|
return codeFileDisplay;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 15:43:29 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Used to delete a specific file from disk via the FileService
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">This is a string but will be 'scripts' 'partialViews', 'partialViewMacros'</param>
|
|
|
|
|
/// <param name="virtualPath">The filename or urlencoded path of the file to delete</param>
|
|
|
|
|
/// <returns>Will return a simple 200 if file deletion succeeds</returns>
|
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
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View found with the specified path");
|
|
|
|
|
|
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
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Partial View Macro found with the specified path");
|
|
|
|
|
|
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
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Script found with the specified path");
|
|
|
|
|
|
2017-01-09 22:54:28 +01:00
|
|
|
default:
|
2017-01-16 16:50:22 +01:00
|
|
|
return Request.CreateResponse(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
2017-01-16 16:50:22 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-19 15:43:29 +00:00
|
|
|
/// <summary>
|
2017-02-01 15:46:22 +01:00
|
|
|
/// Used to create or update a 'partialview', 'partialviewmacro' or 'script' file
|
2017-01-19 15:43:29 +00:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="display"></param>
|
|
|
|
|
/// <returns>The updated CodeFileDisplay model</returns>
|
2017-01-16 16:50:22 +01:00
|
|
|
public CodeFileDisplay PostSave(CodeFileDisplay display)
|
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid == false)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 15:43:29 +00:00
|
|
|
if (display == null || string.IsNullOrWhiteSpace(display.FileType))
|
2017-01-16 16:50:22 +01:00
|
|
|
{
|
|
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
|
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
|
|
|
|
|
switch (display.FileType)
|
2017-01-16 16:50:22 +01:00
|
|
|
{
|
2017-01-19 15:43:29 +00:00
|
|
|
case Core.Constants.Trees.PartialViews:
|
2017-01-19 10:41:08 +01:00
|
|
|
|
2017-02-01 15:46:22 +01:00
|
|
|
var partialViewResult = CreateOrUpdatePartialView(display);
|
|
|
|
|
if (partialViewResult.Success)
|
|
|
|
|
return Mapper.Map(partialViewResult.Result, display);
|
2017-01-19 15:43:29 +00:00
|
|
|
|
2017-02-01 15:46:22 +01:00
|
|
|
display.AddErrorNotification(
|
|
|
|
|
Services.TextService.Localize("speechBubbles/partialViewErrorHeader"),
|
|
|
|
|
Services.TextService.Localize("speechBubbles/partialViewErrorText"));
|
2017-01-19 15:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Core.Constants.Trees.PartialViewMacros:
|
2017-02-01 15:46:22 +01:00
|
|
|
var partialViewMacroResult = CreateOrUpdatePartialViewMacro(display);
|
|
|
|
|
if (partialViewMacroResult.Success)
|
|
|
|
|
return Mapper.Map(partialViewMacroResult.Result, display);
|
|
|
|
|
|
|
|
|
|
display.AddErrorNotification(
|
2017-01-19 09:49:27 +01:00
|
|
|
Services.TextService.Localize("speechBubbles/macroPartialViewErrorHeader"),
|
|
|
|
|
Services.TextService.Localize("speechBubbles/macroPartialViewErrorText"));
|
2017-01-19 15:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Core.Constants.Trees.Scripts:
|
2017-01-16 16:50:22 +01:00
|
|
|
var script = Services.FileService.GetScriptByName(display.VirtualPath);
|
|
|
|
|
if (script != null)
|
|
|
|
|
{
|
2017-01-19 09:49:27 +01:00
|
|
|
script.Path = display.Name;
|
2017-01-16 16:50:22 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-02-01 15:46:22 +01:00
|
|
|
script = new Script(display.Name);
|
2017-01-16 16:50:22 +01:00
|
|
|
}
|
2017-02-01 15:46:22 +01:00
|
|
|
|
|
|
|
|
script.Content = display.Content;
|
|
|
|
|
|
|
|
|
|
Services.FileService.SaveScript(script, Security.CurrentUser.Id);
|
2017-01-19 15:43:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2017-01-16 16:50:22 +01:00
|
|
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
2017-01-19 15:43:29 +00:00
|
|
|
|
|
|
|
|
return display;
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
2017-02-01 15:46:22 +01:00
|
|
|
|
|
|
|
|
private Attempt<IPartialView> CreateOrUpdatePartialView(CodeFileDisplay display)
|
|
|
|
|
{
|
|
|
|
|
Attempt<IPartialView> partialViewResult;
|
|
|
|
|
var view = Services.FileService.GetPartialView(display.VirtualPath ?? string.Empty);
|
|
|
|
|
if (view != null)
|
|
|
|
|
{
|
|
|
|
|
// might need to find the path
|
|
|
|
|
var orgPath = view.OriginalPath.Substring(0, view.OriginalPath.IndexOf(view.Name));
|
|
|
|
|
view.Path = orgPath + display.Name;
|
|
|
|
|
|
|
|
|
|
view.Content = display.Content;
|
|
|
|
|
partialViewResult = Services.FileService.SavePartialView(view, Security.CurrentUser.Id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var fileName = EnsurePartialViewExtension(display.Name);
|
|
|
|
|
view = new PartialView(fileName);
|
|
|
|
|
view.Content = display.Content;
|
|
|
|
|
partialViewResult = Services.FileService.CreatePartialView(view, display.Snippet, Security.CurrentUser.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return partialViewResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Attempt<IPartialView> CreateOrUpdatePartialViewMacro(CodeFileDisplay display)
|
|
|
|
|
{
|
|
|
|
|
Attempt<IPartialView> partialViewMacroResult;
|
|
|
|
|
var viewMacro = Services.FileService.GetPartialViewMacro(display.VirtualPath);
|
|
|
|
|
if (viewMacro != null)
|
|
|
|
|
{
|
|
|
|
|
viewMacro.Content = display.Content;
|
|
|
|
|
viewMacro.Path = display.Name;
|
|
|
|
|
partialViewMacroResult = Services.FileService.SavePartialViewMacro(viewMacro, Security.CurrentUser.Id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var fileName = EnsurePartialViewExtension(display.Name);
|
|
|
|
|
viewMacro = new PartialView(fileName);
|
|
|
|
|
viewMacro.Content = display.Content;
|
|
|
|
|
partialViewMacroResult = Services.FileService.CreatePartialViewMacro(viewMacro, display.Snippet, Security.CurrentUser.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return partialViewMacroResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string EnsurePartialViewExtension(string value)
|
|
|
|
|
{
|
|
|
|
|
if (value.EndsWith(".cshtml") == false)
|
|
|
|
|
value += ".cshtml";
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2017-01-09 22:54:28 +01:00
|
|
|
}
|
|
|
|
|
}
|