Moved all logic of PartialViewTasks and PartialViewMacroTasks to a base class as the only difference was 3 variables

Added events: PartialView(Macro): Create, Save and Delete
Added events: Template: Save (there's already a legacy event, this is a new one to transition to)
PartialViewMacro.ascx.cs was adding ".cshtml" just so that it could strip it of again, crazy
This commit is contained in:
Sebastiaan Janssen
2014-08-14 17:20:25 +02:00
parent 0aff4b4d05
commit 1ccee3f305
6 changed files with 375 additions and 378 deletions

View File

@@ -2,6 +2,7 @@
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Umbraco.Core.Events;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Web.Macros;
@@ -16,55 +17,64 @@ using Template = umbraco.cms.businesslogic.template.Template;
namespace Umbraco.Web.WebServices
{
/// <summary>
/// A REST controller used to save files such as templates, partial views, macro files, etc...
/// </summary>
/// <remarks>
/// This isn't fully implemented yet but we should migrate all of the logic in the umbraco.presentation.webservices.codeEditorSave
/// over to this controller.
/// </remarks>
public class SaveFileController : UmbracoAuthorizedController
{
/// A REST controller used to save files such as templates, partial views, macro files, etc...
/// </summary>
/// <remarks>
/// This isn't fully implemented yet but we should migrate all of the logic in the umbraco.presentation.webservices.codeEditorSave
/// over to this controller.
/// </remarks>
public class SaveFileController : UmbracoAuthorizedController
{
/// <summary>
/// Saves a partial view for a partial view macr
/// </summary>
/// <param name="filename"></param>
/// <param name="oldName"></param>
/// <param name="contents"></param>
/// <returns></returns>
[HttpPost]
public JsonResult SavePartialView(string filename, string oldName, string contents)
{
var folderPath = SystemDirectories.MvcViews.EnsureEndsWith('/');// +"/Partials/";
/// <summary>
/// Saves a partial view for a partial view macr
/// </summary>
/// <param name="filename"></param>
/// <param name="oldName"></param>
/// <param name="contents"></param>
/// <returns></returns>
[HttpPost]
public JsonResult SavePartialView(string filename, string oldName, string contents)
{
var folderPath = SystemDirectories.MvcViews.EnsureEndsWith('/');// +"/Partials/";
// validate file
IOHelper.ValidateEditPath(IOHelper.MapPath(folderPath + filename), folderPath);
// validate extension
IOHelper.ValidateFileExtension(IOHelper.MapPath(folderPath + filename), new[] { "cshtml" }.ToList());
//TODO: Validate using the macro engine
var engine = MacroEngineFactory.GetEngine(PartialViewMacroEngine.EngineName);
//engine.Validate(...)
// validate file
IOHelper.ValidateEditPath(IOHelper.MapPath(folderPath + filename), folderPath);
// validate extension
IOHelper.ValidateFileExtension(IOHelper.MapPath(folderPath + filename), new[] { "cshtml" }.ToList());
var val = contents;
var saveOldPath = oldName.StartsWith("~/") ? IOHelper.MapPath(oldName) : IOHelper.MapPath(folderPath + oldName);
var savePath = filename.StartsWith("~/") ? IOHelper.MapPath(filename) : IOHelper.MapPath(folderPath + filename);
//TODO: Validate using the macro engine
var engine = MacroEngineFactory.GetEngine(PartialViewMacroEngine.EngineName);
//engine.Validate(...)
//Directory check.. only allow files in script dir and below to be edited
if (!savePath.StartsWith(IOHelper.MapPath(folderPath)))
{
return Failed(
ui.Text("speechBubbles", "partialViewErrorText"), ui.Text("speechBubbles", "partialViewErrorHeader"),
//pass in a new exception ... this will also append the the message
new ArgumentException("Illegal path: " + savePath));
}
var val = contents;
var saveOldPath = oldName.StartsWith("~/") ? IOHelper.MapPath(oldName) : IOHelper.MapPath(folderPath + oldName);
var savePath = filename.StartsWith("~/") ? IOHelper.MapPath(filename) : IOHelper.MapPath(folderPath + filename);
//deletes the old file
if (savePath != saveOldPath)
{
if (System.IO.File.Exists(saveOldPath))
System.IO.File.Delete(saveOldPath);
}
//Directory check.. only allow files in script dir and below to be edited
if (!savePath.StartsWith(IOHelper.MapPath(folderPath)))
{
return Failed(
ui.Text("speechBubbles", "partialViewErrorText"), ui.Text("speechBubbles", "partialViewErrorHeader"),
//pass in a new exception ... this will also append the the message
new ArgumentException("Illegal path: " + savePath));
}
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<string>(savePath), this))
{
return Failed(
ui.Text("speechBubbles", "partialViewErrorText"), ui.Text("speechBubbles", "partialViewErrorHeader"),
//pass in a new exception ... this will also append the the message
new ArgumentException("Save was cancelled by an event handler " + savePath));
}
//deletes the old file
if (savePath != saveOldPath)
{
if (System.IO.File.Exists(saveOldPath))
System.IO.File.Delete(saveOldPath);
}
//NOTE: I've left the below here just for informational purposes. If we save a file this way, then the UTF8
// BOM mucks everything up, strangely, if we use WriteAllText everything is ok!
@@ -76,31 +86,33 @@ namespace Umbraco.Web.WebServices
System.IO.File.WriteAllText(savePath, val, Encoding.UTF8);
return Success(ui.Text("speechBubbles", "partialViewSavedText"), ui.Text("speechBubbles", "partialViewSavedHeader"));
}
Saved.RaiseEvent(new SaveEventArgs<string>(savePath, false), this);
/// <summary>
/// Saves a template
/// </summary>
/// <param name="templateName"></param>
/// <param name="templateAlias"></param>
/// <param name="templateContents"></param>
/// <param name="templateId"></param>
/// <param name="masterTemplateId"></param>
/// <returns></returns>
[HttpPost]
public JsonResult SaveTemplate(string templateName, string templateAlias, string templateContents, int templateId, int masterTemplateId)
{
Template t;
bool pathChanged = false;
try
{
t = new Template(templateId)
{
Text = templateName,
Alias = templateAlias,
Design = templateContents
};
return Success(ui.Text("speechBubbles", "partialViewSavedText"), ui.Text("speechBubbles", "partialViewSavedHeader"));
}
/// <summary>
/// Saves a template
/// </summary>
/// <param name="templateName"></param>
/// <param name="templateAlias"></param>
/// <param name="templateContents"></param>
/// <param name="templateId"></param>
/// <param name="masterTemplateId"></param>
/// <returns></returns>
[HttpPost]
public JsonResult SaveTemplate(string templateName, string templateAlias, string templateContents, int templateId, int masterTemplateId)
{
Template t;
bool pathChanged = false;
try
{
t = new Template(templateId)
{
Text = templateName,
Alias = templateAlias,
Design = templateContents
};
//check if the master page has changed
if (t.MasterTemplate != masterTemplateId)
@@ -108,16 +120,23 @@ namespace Umbraco.Web.WebServices
pathChanged = true;
t.MasterTemplate = masterTemplateId;
}
}
catch (ArgumentException ex)
{
//the template does not exist
return Failed("Template does not exist", ui.Text("speechBubbles", "templateErrorHeader"), ex);
}
}
catch (ArgumentException ex)
{
//the template does not exist
return Failed("Template does not exist", ui.Text("speechBubbles", "templateErrorHeader"), ex);
}
try
{
t.Save();
try
{
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<string>(t.TemplateFilePath), this))
{
return Failed(ui.Text("speechBubbles", "templateErrorText"), ui.Text("speechBubbles", "templateErrorHeader"),
//pass in a new exception ... this will also append the the message
new ArgumentException("Save was cancelled by an event handler: " + t.TemplateFilePath));
}
t.Save();
//ensure the correct path is synced as the parent might have been changed
// http://issues.umbraco.org/issue/U4-2300
@@ -128,14 +147,16 @@ namespace Umbraco.Web.WebServices
}
var syncPath = "-1,init," + t.Path.Replace("-1,", "");
return Success(ui.Text("speechBubbles", "templateSavedText"), ui.Text("speechBubbles", "templateSavedHeader"),
new {path = syncPath});
}
catch (Exception ex)
{
return Failed(ui.Text("speechBubbles", "templateErrorText"), ui.Text("speechBubbles", "templateErrorHeader"), ex);
}
}
Saved.RaiseEvent(new SaveEventArgs<string>(t.TemplateFilePath, false), this);
return Success(ui.Text("speechBubbles", "templateSavedText"), ui.Text("speechBubbles", "templateSavedHeader"),
new { path = syncPath });
}
catch (Exception ex)
{
return Failed(ui.Text("speechBubbles", "templateErrorText"), ui.Text("speechBubbles", "templateErrorHeader"), ex);
}
}
/// <summary>
/// Returns a successful message
@@ -151,27 +172,36 @@ namespace Umbraco.Web.WebServices
d["message"] = message;
d["header"] = header;
return Json(d);
}
return Json(d);
}
/// <summary>
/// Returns a failed message
/// </summary>
/// <param name="message">The message to display in the speach bubble</param>
/// <param name="header">The header to display in the speach bubble</param>
/// <param name="exception">The exception if there was one</param>
/// <returns></returns>
private JsonResult Failed(string message, string header, Exception exception = null)
{
if (exception != null)
LogHelper.Error<SaveFileController>("An error occurred saving a file. " + message, exception);
return Json(new
{
success = false,
header = header,
message = message + (exception == null ? "" : (exception.Message + ". Check log for details."))
});
}
/// <summary>
/// Returns a failed message
/// </summary>
/// <param name="message">The message to display in the speach bubble</param>
/// <param name="header">The header to display in the speach bubble</param>
/// <param name="exception">The exception if there was one</param>
/// <returns></returns>
private JsonResult Failed(string message, string header, Exception exception = null)
{
if (exception != null)
LogHelper.Error<SaveFileController>("An error occurred saving a file. " + message, exception);
return Json(new
{
success = false,
header = header,
message = message + (exception == null ? "" : (exception.Message + ". Check log for details."))
});
}
}
/// <summary>
/// Occurs before Create
/// </summary>
internal static event TypedEventHandler<SaveFileController, SaveEventArgs<string>> Saving;
/// <summary>
/// Occurs after Create
/// </summary>
internal static event TypedEventHandler<SaveFileController, SaveEventArgs<string>> Saved;
}
}