From bbe9aa184897cf8cec79ffcab40e95fad0c6f8fc Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Thu, 20 Feb 2020 09:05:42 +0100 Subject: [PATCH] Fix issue with NuCache knowing the web project --- .../Install/FilePermissionDirectoryHelper.cs | 39 +++++++++++++++++++ .../PublishedSnapshotService.cs | 2 +- .../Umbraco.PublishedCache.NuCache.csproj | 1 - .../Install/FilePermissionHelper.cs | 32 +-------------- 4 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 src/Umbraco.Abstractions/Install/FilePermissionDirectoryHelper.cs diff --git a/src/Umbraco.Abstractions/Install/FilePermissionDirectoryHelper.cs b/src/Umbraco.Abstractions/Install/FilePermissionDirectoryHelper.cs new file mode 100644 index 0000000000..bc57083cbd --- /dev/null +++ b/src/Umbraco.Abstractions/Install/FilePermissionDirectoryHelper.cs @@ -0,0 +1,39 @@ +using System; +using System.IO; +using Umbraco.Core.IO; + +namespace Umbraco.Web.Install +{ + public class FilePermissionDirectoryHelper + { + + + // tries to create a file + // if successful, the file is deleted + // creates the directory if needed - does not delete it + public static bool TryCreateDirectory(string dir, IIOHelper ioHelper) + { + try + { + var dirPath = ioHelper.MapPath(dir); + + if (Directory.Exists(dirPath) == false) + Directory.CreateDirectory(dirPath); + + var filePath = dirPath + "/" + CreateRandomFileName() + ".tmp"; + File.WriteAllText(filePath, "This is an Umbraco internal test file. It is safe to delete it."); + File.Delete(filePath); + return true; + } + catch + { + return false; + } + } + + public static string CreateRandomFileName() + { + return "umbraco-test." + Guid.NewGuid().ToString("N").Substring(0, 8); + } + } +} diff --git a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs index e383eb0223..029978347a 100644 --- a/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs +++ b/src/Umbraco.PublishedCache.NuCache/PublishedSnapshotService.cs @@ -364,7 +364,7 @@ namespace Umbraco.Web.PublishedCache.NuCache public override bool EnsureEnvironment(out IEnumerable errors) { // must have app_data and be able to write files into it - var ok = FilePermissionHelper.TryCreateDirectory(GetLocalFilesPath(), _ioHelper); + var ok = FilePermissionDirectoryHelper.TryCreateDirectory(GetLocalFilesPath(), _ioHelper); errors = ok ? Enumerable.Empty() : new[] { "NuCache local files." }; return ok; } diff --git a/src/Umbraco.PublishedCache.NuCache/Umbraco.PublishedCache.NuCache.csproj b/src/Umbraco.PublishedCache.NuCache/Umbraco.PublishedCache.NuCache.csproj index 2001309138..4c369ec2fa 100644 --- a/src/Umbraco.PublishedCache.NuCache/Umbraco.PublishedCache.NuCache.csproj +++ b/src/Umbraco.PublishedCache.NuCache/Umbraco.PublishedCache.NuCache.csproj @@ -13,7 +13,6 @@ - diff --git a/src/Umbraco.Web/Install/FilePermissionHelper.cs b/src/Umbraco.Web/Install/FilePermissionHelper.cs index be7c88729a..be4c2b1dd0 100644 --- a/src/Umbraco.Web/Install/FilePermissionHelper.cs +++ b/src/Umbraco.Web/Install/FilePermissionHelper.cs @@ -141,7 +141,7 @@ namespace Umbraco.Web.Install { try { - var path = _ioHelper.MapPath(dir + "/" + CreateRandomName()); + var path = _ioHelper.MapPath(dir + "/" + FilePermissionDirectoryHelper.CreateRandomFileName()); Directory.CreateDirectory(path); Directory.Delete(path); return true; @@ -152,29 +152,6 @@ namespace Umbraco.Web.Install } } - // tries to create a file - // if successful, the file is deleted - // creates the directory if needed - does not delete it - public static bool TryCreateDirectory(string dir, IIOHelper ioHelper) - { - try - { - var dirPath = ioHelper.MapPath(dir); - - if (Directory.Exists(dirPath) == false) - Directory.CreateDirectory(dirPath); - - var filePath = dirPath + "/" + CreateRandomName() + ".tmp"; - File.WriteAllText(filePath, "This is an Umbraco internal test file. It is safe to delete it."); - File.Delete(filePath); - return true; - } - catch - { - return false; - } - } - // tries to create a file // if successful, the file is deleted // @@ -194,7 +171,7 @@ namespace Umbraco.Web.Install if (canWrite) { - var filePath = dirPath + "/" + CreateRandomName() + ".tmp"; + var filePath = dirPath + "/" + FilePermissionDirectoryHelper.CreateRandomFileName() + ".tmp"; File.WriteAllText(filePath, "This is an Umbraco internal test file. It is safe to delete it."); File.Delete(filePath); return true; @@ -261,10 +238,5 @@ namespace Umbraco.Web.Install return false; } } - - private static string CreateRandomName() - { - return "umbraco-test." + Guid.NewGuid().ToString("N").Substring(0, 8); - } } }