U4-7042 - use IFileService when editing scripts

This commit is contained in:
Stephan
2015-09-03 15:12:37 +02:00
parent b03d7884bb
commit d323b3c5c9
7 changed files with 123 additions and 90 deletions

View File

@@ -12,8 +12,9 @@ using Umbraco.Web.Mvc;
using umbraco;
using umbraco.cms.businesslogic.macro;
using System.Collections.Generic;
using umbraco.cms.helpers;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Template = umbraco.cms.businesslogic.template.Template;
namespace Umbraco.Web.WebServices
@@ -216,6 +217,42 @@ namespace Umbraco.Web.WebServices
}
}
[HttpPost]
public JsonResult SaveScript(string filename, string oldName, string contents)
{
filename = filename.TrimStart(System.IO.Path.DirectorySeparatorChar);
var svce = (FileService) Services.FileService;
var script = svce.GetScriptByName(oldName);
if (script == null)
script = new Script(filename);
else
script.Path = filename;
script.Content = contents;
try
{
if (svce.ValidateScript(script) == false)
return Failed(ui.Text("speechBubbles", "scriptErrorText"), ui.Text("speechBubbles", "scriptErrorHeader"),
new FileSecurityException("File '" + filename + "' is not a valid script file."));
svce.SaveScript(script);
}
catch (Exception e)
{
return Failed(ui.Text("speechBubbles", "scriptErrorText"), ui.Text("speechBubbles", "scriptErrorHeader"), e);
}
return Success(ui.Text("speechBubbles", "scriptSavedText"), ui.Text("speechBubbles", "scriptSavedHeader"),
new
{
path = DeepLink.GetTreePathFromFilePath(script.Path),
name = script.Path,
url = script.VirtualPath,
contents = script.Content
});
}
/// <summary>
/// Returns a successful message
/// </summary>

View File

@@ -41,58 +41,42 @@ namespace umbraco.cms.presentation.settings.scripts
protected MenuButton SaveButton;
private string file;
private string filename;
protected string ScriptTreeSyncPath { get; private set; }
protected int ScriptId { get; private set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
NameTxt.Text = file;
NameTxt.Text = filename;
string path = "";
if (file.StartsWith("~/"))
path = IOHelper.ResolveUrl(file);
else
path = IOHelper.ResolveUrl(SystemDirectories.Scripts + "/" + file);
// get the script, ensure it exists (not null) and validate (because
// the file service ensures that it loads scripts from the proper location
// but does not seem to validate extensions?) - in case of an error,
// throw - that's what we did anyways.
// also scrapping the code that added .cshtml and .vbhtml extensions, and
// ~/Views directory - we're not using editScript.aspx for views anymore.
lttPath.Text = "<a target='_blank' href='" + path + "'>" + path + "</a>";
var svce = ApplicationContext.Current.Services.FileService;
var script = svce.GetScriptByName(filename);
if (script == null) // not found
throw new FileNotFoundException("Could not find file '" + filename + "'.");
var exts = UmbracoConfig.For.UmbracoSettings().Content.ScriptFileTypes.ToList();
if (UmbracoConfig.For.UmbracoSettings().Templates.DefaultRenderingEngine == RenderingEngine.Mvc)
{
exts.Add("cshtml");
exts.Add("vbhtml");
}
var dirs = SystemDirectories.Scripts;
if (UmbracoConfig.For.UmbracoSettings().Templates.DefaultRenderingEngine == RenderingEngine.Mvc)
dirs += "," + SystemDirectories.MvcViews;
// validate file
IOHelper.ValidateEditPath(IOHelper.MapPath(path), dirs.Split(','));
// validate extension
IOHelper.ValidateFileExtension(IOHelper.MapPath(path), exts);
StreamReader SR;
string S;
SR = File.OpenText(IOHelper.MapPath(path));
S = SR.ReadToEnd();
SR.Close();
editorSource.Text = S;
lttPath.Text = "<a id=\"" + lttPath.ClientID + "\" target=\"_blank\" href=\"" + script.VirtualPath + "\">" + script.VirtualPath + "</a>";
editorSource.Text = script.Content;
ScriptTreeSyncPath = DeepLink.GetTreePathFromFilePath(filename);
Panel1.Text = ui.Text("editscript", base.getUser());
pp_name.Text = ui.Text("name", base.getUser());
pp_path.Text = ui.Text("path", base.getUser());
if (!IsPostBack)
if (IsPostBack == false)
{
string sPath = DeepLink.GetTreePathFromFilePath(file);
ClientTools
.SetActiveTreeType(TreeDefinitionCollection.Instance.FindTree<loadScripts>().Tree.Alias)
.SyncTree(sPath, false);
.SyncTree(ScriptTreeSyncPath, false);
}
}
@@ -100,12 +84,12 @@ namespace umbraco.cms.presentation.settings.scripts
{
base.OnInit(e);
file = Request.QueryString["file"].TrimStart('/');
filename = Request.QueryString["file"].TrimStart('/');
//need to change the editor type if it is XML
if (file.EndsWith("xml"))
if (filename.EndsWith("xml"))
editorSource.CodeBase = uicontrols.CodeArea.EditorType.XML;
else if (file.EndsWith("master"))
else if (filename.EndsWith("master"))
editorSource.CodeBase = uicontrols.CodeArea.EditorType.HTML;
@@ -153,7 +137,6 @@ namespace umbraco.cms.presentation.settings.scripts
}
}
protected override void OnPreRender(EventArgs e)
{

View File

@@ -355,6 +355,7 @@ namespace umbraco.presentation.webservices
// return "false";
//}
[Obsolete("This method has been superceded by the REST service /Umbraco/RestServices/SaveFile/SaveScript which is powered by the SaveFileController.")]
[WebMethod]
public string SaveScript(string filename, string oldName, string contents)
{