Merge pull request #2742 from nathanwoulfe/temp-U4-6946

U4 6946 - Existing template file is overwritten when creating a new template in the backoffice
This commit is contained in:
Sebastiaan Janssen
2018-07-13 11:12:16 +02:00
committed by GitHub
4 changed files with 75 additions and 7 deletions

View File

@@ -291,6 +291,14 @@ namespace Umbraco.Core.Services
{"ContentTypeAlias", contentTypeAlias},
};
// check that the template hasn't been created on disk before creating the content type
// if it exists, set the new template content to the existing file content
string content = GetViewContent(contentTypeAlias);
if (content.IsNullOrWhiteSpace() == false)
{
template.Content = content;
}
using (var uow = UowProvider.GetUnitOfWork())
{
var saveEventArgs = new SaveEventArgs<ITemplate>(template, true, evtMsgs, additionalData);
@@ -1045,6 +1053,19 @@ namespace Umbraco.Core.Services
: Attempt<string>.Fail();
}
internal Attempt<string> TryGetViewPath(string fileName)
{
if (fileName.EndsWith(".cshtml") == false)
{
fileName += ".cshtml";
}
string viewPath = IOHelper.MapPath(SystemDirectories.MvcViews + "/" + fileName);
return System.IO.File.Exists(viewPath)
? Attempt<string>.Succeed(viewPath)
: Attempt<string>.Fail();
}
private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType, IUnitOfWork uow)
{
switch (partialViewType)
@@ -1076,6 +1097,23 @@ namespace Umbraco.Core.Services
return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro);
}
public string GetViewContent(string filename)
{
if (filename.IsNullOrWhiteSpace())
throw new ArgumentNullException(nameof(filename));
Attempt<string> viewAttempt = TryGetViewPath(filename);
if (viewAttempt.Success == false)
{
return string.Empty;
}
using (var view = new StreamReader(System.IO.File.OpenRead(viewAttempt.Result)))
{
return view.ReadToEnd().Trim();
}
}
private string GetPartialViewMacroSnippetContent(string snippetName, PartialViewType partialViewType)
{
if (snippetName.IsNullOrWhiteSpace())
@@ -1265,4 +1303,4 @@ namespace Umbraco.Core.Services
#endregion
}
}
}

View File

@@ -427,5 +427,12 @@ namespace Umbraco.Core.Services
/// <param name="filepath">The filesystem path to the partial view.</param>
/// <returns>The size of the partial view.</returns>
long GetPartialViewFileSize(string filepath);
/// <summary>
/// Gets the content of a view.
/// </summary>
/// <param name="filename">The name of the view.</param>
/// <returns></returns>
string GetViewContent(string filename);
}
}
}