From 017b5353db544fc5e84d7f3e08ccb4e50f459eeb Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Wed, 18 Jul 2018 14:02:26 +1000 Subject: [PATCH 1/3] remove direct IO operation, replace with exisiting repository method --- src/Umbraco.Core/Services/FileService.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index 4428175fa9..e7560e5636 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; +using System.Text; using System.Text.RegularExpressions; using Umbraco.Core.Events; using Umbraco.Core.IO; @@ -1108,9 +1109,15 @@ namespace Umbraco.Core.Services return string.Empty; } - using (var view = new StreamReader(System.IO.File.OpenRead(viewAttempt.Result))) + using (var uow = UowProvider.GetUnitOfWork()) { - return view.ReadToEnd().Trim(); + var repository = RepositoryFactory.CreateTemplateRepository(uow); + var stream = repository.GetFileContentStream(viewAttempt.Result); + + using (var reader = new StreamReader(stream, Encoding.UTF8, true)) + { + return reader.ReadToEnd().Trim(); + } } } From fe648c8069e83f83f165f8498ea49f86312288b2 Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Wed, 18 Jul 2018 14:27:18 +1000 Subject: [PATCH 2/3] get view content in service methods rather than controller --- src/Umbraco.Core/Services/FileService.cs | 20 +++++++++++++++---- src/Umbraco.Core/Services/IFileService.cs | 7 ------- src/Umbraco.Web/Editors/TemplateController.cs | 9 --------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index e7560e5636..f81110c338 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -295,7 +295,7 @@ namespace Umbraco.Core.Services // 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) + if (content != null) { template.Content = content; } @@ -321,17 +321,29 @@ namespace Umbraco.Core.Services return Attempt.Succeed(new OperationStatus(template, OperationStatusType.Success, evtMsgs)); } + /// + /// Create a new template, setting the content if a view exists in the filesystem + /// + /// + /// + /// + /// + /// public ITemplate CreateTemplateWithIdentity(string name, string content, ITemplate masterTemplate = null, int userId = 0) { + // file might already be on disk, if so grab the content to avoid overwriting var template = new Template(name, name) { - Content = content + Content = GetViewContent(name) ?? content }; + if (masterTemplate != null) { template.SetMasterTemplate(masterTemplate); } + SaveTemplate(template, userId); + return template; } @@ -1098,7 +1110,7 @@ namespace Umbraco.Core.Services return GetPartialViewMacroSnippetContent(snippetName, PartialViewType.PartialViewMacro); } - public string GetViewContent(string filename) + private string GetViewContent(string filename) { if (filename.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(filename)); @@ -1106,7 +1118,7 @@ namespace Umbraco.Core.Services Attempt viewAttempt = TryGetViewPath(filename); if (viewAttempt.Success == false) { - return string.Empty; + return null; } using (var uow = UowProvider.GetUnitOfWork()) diff --git a/src/Umbraco.Core/Services/IFileService.cs b/src/Umbraco.Core/Services/IFileService.cs index 0031562697..b798b2c5ae 100644 --- a/src/Umbraco.Core/Services/IFileService.cs +++ b/src/Umbraco.Core/Services/IFileService.cs @@ -427,12 +427,5 @@ namespace Umbraco.Core.Services /// The filesystem path to the partial view. /// The size of the partial view. long GetPartialViewFileSize(string filepath); - - /// - /// Gets the content of a view. - /// - /// The name of the view. - /// - string GetViewContent(string filename); } } diff --git a/src/Umbraco.Web/Editors/TemplateController.cs b/src/Umbraco.Web/Editors/TemplateController.cs index 132e804a95..100a266dff 100644 --- a/src/Umbraco.Web/Editors/TemplateController.cs +++ b/src/Umbraco.Web/Editors/TemplateController.cs @@ -176,14 +176,6 @@ namespace Umbraco.Web.Editors else { //create - - // file might already be on disk, if so grab the content to avoid overwriting - string content = Services.FileService.GetViewContent(display.Alias); - if (string.IsNullOrEmpty(content) == false) - { - display.Content = content; - } - ITemplate master = null; if (string.IsNullOrEmpty(display.MasterTemplateAlias) == false) { @@ -193,7 +185,6 @@ namespace Umbraco.Web.Editors } var template = Services.FileService.CreateTemplateWithIdentity(display.Name, display.Content, master); - //template = Services.FileService.GetTemplate(template.Id); Mapper.Map(template, display); } From 06f014df617d9c8d8055459c6e0c95c52bab6deb Mon Sep 17 00:00:00 2001 From: Nathan Woulfe Date: Thu, 19 Jul 2018 08:28:07 +1000 Subject: [PATCH 3/3] remove TryGetViewPath as not needed --- src/Umbraco.Core/Services/FileService.cs | 25 ++++++++---------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Core/Services/FileService.cs b/src/Umbraco.Core/Services/FileService.cs index f81110c338..fc1bbf34d2 100644 --- a/src/Umbraco.Core/Services/FileService.cs +++ b/src/Umbraco.Core/Services/FileService.cs @@ -1066,19 +1066,6 @@ namespace Umbraco.Core.Services : Attempt.Fail(); } - internal Attempt TryGetViewPath(string fileName) - { - if (fileName.EndsWith(".cshtml") == false) - { - fileName += ".cshtml"; - } - - string viewPath = IOHelper.MapPath(SystemDirectories.MvcViews + "/" + fileName); - return System.IO.File.Exists(viewPath) - ? Attempt.Succeed(viewPath) - : Attempt.Fail(); - } - private IPartialViewRepository GetPartialViewRepository(PartialViewType partialViewType, IUnitOfWork uow) { switch (partialViewType) @@ -1115,16 +1102,20 @@ namespace Umbraco.Core.Services if (filename.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(filename)); - Attempt viewAttempt = TryGetViewPath(filename); - if (viewAttempt.Success == false) + if (filename.EndsWith(".cshtml") == false) { - return null; + filename = $"{filename}.cshtml"; } using (var uow = UowProvider.GetUnitOfWork()) { var repository = RepositoryFactory.CreateTemplateRepository(uow); - var stream = repository.GetFileContentStream(viewAttempt.Result); + var stream = repository.GetFileContentStream(filename); + + if (stream == null) + { + return null; + } using (var reader = new StreamReader(stream, Encoding.UTF8, true)) {