From d2ac9158c63af82a55bec3df95737d141bf07cf5 Mon Sep 17 00:00:00 2001 From: "Matt@MBP13-PC" Date: Mon, 13 Aug 2012 10:04:31 -0100 Subject: [PATCH] Added IFileSystem Fixed issue with last merge --- src/Umbraco.Core/IO/IFileSystem.cs | 39 ++++ src/Umbraco.Core/IO/PhysicalFileSystem.cs | 182 ++++++++++++++++++ src/Umbraco.Core/Properties/AssemblyInfo.cs | 3 +- src/Umbraco.Core/Umbraco.Core.csproj | 2 + .../IO/AbstractFileSystemTests.cs | 156 +++++++++++++++ .../IO/PhysicalFileSystemTests.cs | 25 +++ src/Umbraco.Tests/Umbraco.Tests.csproj | 2 + .../BasePages/BasePage.cs | 4 +- .../Installer/DefaultInstallerUtility.cs | 1 - 9 files changed, 411 insertions(+), 3 deletions(-) create mode 100644 src/Umbraco.Core/IO/IFileSystem.cs create mode 100644 src/Umbraco.Core/IO/PhysicalFileSystem.cs create mode 100644 src/Umbraco.Tests/IO/AbstractFileSystemTests.cs create mode 100644 src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs diff --git a/src/Umbraco.Core/IO/IFileSystem.cs b/src/Umbraco.Core/IO/IFileSystem.cs new file mode 100644 index 0000000000..3a2c426754 --- /dev/null +++ b/src/Umbraco.Core/IO/IFileSystem.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Umbraco.Core.IO +{ + internal interface IFileSystem + { + void DeleteDirectory(string path); + + void DeleteDirectory(string path, bool recursive); + + IEnumerable GetFiles(string path); + + IEnumerable GetFiles(string path, string filter); + + IEnumerable GetDirectories(string path); + + string GetFullPath(string path); + + string GetUrl(string path); + + void DeleteFile(string path); + + bool FileExists(string path); + + bool DirectoryExists(string path); + + void AddFile(string path, Stream stream); + + void AddFile(string path, Stream stream, bool overrideIfExists); + + Stream OpenFile(string path); + + DateTimeOffset GetLastModified(string path); + + DateTimeOffset GetCreated(string path); + } +} diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs new file mode 100644 index 0000000000..6f7fab4521 --- /dev/null +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace Umbraco.Core.IO +{ + internal class PhysicalFileSystem : IFileSystem + { + private readonly string _rootPath; + private readonly string _rootUrl; + + public PhysicalFileSystem(string rootPath, string rootUrl) + { + if (string.IsNullOrEmpty(rootPath)) + throw new ArgumentException("The argument 'rootPath' cannot be null or empty."); + + if (string.IsNullOrEmpty(rootUrl)) + throw new ArgumentException("The argument 'rootUrl' cannot be null or empty."); + + _rootPath = rootPath; + _rootUrl = rootUrl; + } + + public IEnumerable GetDirectories(string path) + { + path = EnsureTrailingSeparator(GetFullPath(path)); + + try + { + if (Directory.Exists(path)) + return Directory.EnumerateDirectories(path).Select(MakeRelativePath); + } + catch (UnauthorizedAccessException ex) + { } + catch (DirectoryNotFoundException ex) + { } + + return Enumerable.Empty(); + } + + public virtual void DeleteDirectory(string path) + { + DeleteDirectory(path, false); + } + + public void DeleteDirectory(string path, bool recursive) + { + if (!DirectoryExists(path)) + return; + + try + { + Directory.Delete(GetFullPath(path), recursive); + } + catch (DirectoryNotFoundException ex) + { } + } + + public bool DirectoryExists(string path) + { + return Directory.Exists(GetFullPath(path)); + } + + public void AddFile(string path, Stream stream) + { + AddFile(path, stream, true); + } + + public void AddFile(string path, Stream stream, bool overrideIfExists) + { + if (FileExists(path) && !overrideIfExists) + throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", + path)); + + EnsureDirectory(Path.GetDirectoryName(path)); + + using (var destination = (Stream)File.Create(GetFullPath(path))) + stream.CopyTo(destination); + } + + public IEnumerable GetFiles(string path) + { + return GetFiles(path, "*.*"); + } + + public IEnumerable GetFiles(string path, string filter) + { + path = EnsureTrailingSeparator(GetFullPath(path)); + + try + { + if (Directory.Exists(path)) + return Directory.EnumerateFiles(path, filter).Select(MakeRelativePath); + } + catch (UnauthorizedAccessException ex) + { } + catch (DirectoryNotFoundException ex) + { } + + return Enumerable.Empty(); + } + + public Stream OpenFile(string path) + { + return File.OpenRead(GetFullPath(path)); + } + + public void DeleteFile(string path) + { + if (!FileExists(path)) + return; + + try + { + File.Delete(GetFullPath(path)); + } + catch (FileNotFoundException ex) + { } + } + + public bool FileExists(string path) + { + return File.Exists(GetFullPath(path)); + } + + public string GetFullPath(string path) + { + return !path.StartsWith(_rootPath) + ? Path.Combine(_rootPath, path) + : path; + } + + public string GetUrl(string path) + { + return _rootUrl.TrimEnd("/") + "/" + path + .TrimStart(Path.DirectorySeparatorChar) + .Replace(Path.DirectorySeparatorChar, '/'); + } + + public DateTimeOffset GetLastModified(string path) + { + return DirectoryExists(path) + ? new DirectoryInfo(GetFullPath(path)).LastWriteTimeUtc + : new FileInfo(GetFullPath(path)).LastWriteTimeUtc; + } + + public DateTimeOffset GetCreated(string path) + { + return DirectoryExists(path) + ? Directory.GetCreationTimeUtc(GetFullPath(path)) + : File.GetCreationTimeUtc(GetFullPath(path)); + } + + #region Helper Methods + + protected string MakeRelativePath(string fullPath) + { + return fullPath.Substring(_rootPath.Length).TrimStart(new char[1] + { + Path.DirectorySeparatorChar + }); + } + + protected virtual void EnsureDirectory(string path) + { + path = GetFullPath(path); + Directory.CreateDirectory(path); + } + + protected string EnsureTrailingSeparator(string path) + { + if (!path.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) + path = path + Path.DirectorySeparatorChar; + + return path; + } + + #endregion + } +} diff --git a/src/Umbraco.Core/Properties/AssemblyInfo.cs b/src/Umbraco.Core/Properties/AssemblyInfo.cs index 90b7ad63b5..fd937747fc 100644 --- a/src/Umbraco.Core/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Core/Properties/AssemblyInfo.cs @@ -20,4 +20,5 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("cms")] [assembly: InternalsVisibleTo("umbraco")] -[assembly: InternalsVisibleTo("businesslogic")] \ No newline at end of file +[assembly: InternalsVisibleTo("businesslogic")] +[assembly: InternalsVisibleTo("Umbraco.Tests")] \ No newline at end of file diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 265eed5a9e..bae30785d7 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -47,6 +47,8 @@ + + diff --git a/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs b/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs new file mode 100644 index 0000000000..debcd8492b --- /dev/null +++ b/src/Umbraco.Tests/IO/AbstractFileSystemTests.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Umbraco.Core.IO; +using Umbraco.Tests.BusinessLogic; + +namespace Umbraco.Tests.IO +{ + [TestFixture] + internal abstract class AbstractFileSystemTests + { + protected IFileSystem _fileSystem; + + protected AbstractFileSystemTests(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + [Test] + public void Can_Create_And_Delete_Files() + { + _fileSystem.AddFile("test.txt", CreateStream()); + + Assert.IsTrue(_fileSystem.FileExists("test.txt")); + + _fileSystem.DeleteFile("test.txt"); + + Assert.IsFalse(_fileSystem.FileExists("test.txt")); + } + + [Test] + public void Can_Overwrite_File() + { + _fileSystem.AddFile("test/test.txt", CreateStream()); + _fileSystem.AddFile("test/test.txt", CreateStream()); + + var files = _fileSystem.GetFiles("test"); + + Assert.AreEqual(1, files.Count()); + + _fileSystem.DeleteDirectory("test", true); + } + + [Test] + [ExpectedException(typeof(InvalidOperationException))] + public void Cant_Overwrite_File() + { + _fileSystem.AddFile("test.txt", CreateStream()); + _fileSystem.AddFile("test.txt", CreateStream(), false); + + _fileSystem.DeleteFile("test.txt"); + } + + [Test] + public void Can_Get_Files() + { + _fileSystem.AddFile("test/test1.txt", CreateStream()); + _fileSystem.AddFile("test/test2.txt", CreateStream()); + _fileSystem.AddFile("test/test3.txt", CreateStream()); + _fileSystem.AddFile("test/test4.bak", CreateStream()); + + var files = _fileSystem.GetFiles("test"); + + Assert.AreEqual(4, files.Count()); + + files = _fileSystem.GetFiles("test", "*.txt"); + + Assert.AreEqual(3, files.Count()); + + _fileSystem.DeleteDirectory("test", true); + } + + [Test] + public void Can_Read_File() + { + _fileSystem.AddFile("test.txt", CreateStream("hello world")); + + var stream = _fileSystem.OpenFile("test.txt"); + var reader = new StreamReader(stream); + var contents = reader.ReadToEnd(); + reader.Close(); + + Assert.AreEqual("hello world", contents); + + _fileSystem.DeleteFile("test.txt"); + } + + [Test] + public void Can_Get_Directories() + { + _fileSystem.AddFile("test/sub1/test.txt", CreateStream()); + _fileSystem.AddFile("test/sub2/test.txt", CreateStream()); + _fileSystem.AddFile("test/sub3/test.txt", CreateStream()); + + var dirs = _fileSystem.GetDirectories("test"); + + Assert.AreEqual(3, dirs.Count()); + Assert.IsTrue(_fileSystem.DirectoryExists("test/sub1")); + Assert.IsTrue(_fileSystem.DirectoryExists("test/sub2")); + Assert.IsTrue(_fileSystem.DirectoryExists("test/sub3")); + + _fileSystem.DeleteDirectory("test", true); + } + + [Test] + public void Can_Get_File_Dates() + { + _fileSystem.AddFile("test.txt", CreateStream()); + + var created = _fileSystem.GetCreated("test.txt"); + var modified = _fileSystem.GetLastModified("test.txt"); + + Assert.AreEqual(DateTime.Today.Year, created.Year); + Assert.AreEqual(DateTime.Today.Month, created.Month); + Assert.AreEqual(DateTime.Today.Date, created.Date); + + Assert.AreEqual(DateTime.Today.Year, modified.Year); + Assert.AreEqual(DateTime.Today.Month, modified.Month); + Assert.AreEqual(DateTime.Today.Date, modified.Date); + + _fileSystem.DeleteFile("test.txt"); + } + + [Test] + public void Can_Get_File_Url() + { + _fileSystem.AddFile("test.txt", CreateStream()); + + var url = _fileSystem.GetUrl("test.txt"); + + Assert.AreEqual(ConstructUrl("test.txt"), url); + + _fileSystem.DeleteFile("test.txt"); + } + + #region Helper Methods + + protected Stream CreateStream(string contents = null) + { + if(string.IsNullOrEmpty(contents)) + contents = "test"; + + var bytes = Encoding.UTF8.GetBytes(contents); + var stream = new MemoryStream(bytes); + + return stream; + } + + protected abstract string ConstructUrl(string path); + + #endregion + } +} diff --git a/src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs b/src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs new file mode 100644 index 0000000000..dc79f82620 --- /dev/null +++ b/src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Umbraco.Core.IO; +using Umbraco.Tests.BusinessLogic; + +namespace Umbraco.Tests.IO +{ + [TestFixture] + internal class PhysicalFileSystemTests : AbstractFileSystemTests + { + public PhysicalFileSystemTests() + : base(new PhysicalFileSystem(AppDomain.CurrentDomain.BaseDirectory, + "/Media/")) + { } + + protected override string ConstructUrl(string path) + { + return "/Media/" + path; + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 68df4cd172..7b2b2cb75e 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -48,6 +48,8 @@ + + diff --git a/src/umbraco.businesslogic/BasePages/BasePage.cs b/src/umbraco.businesslogic/BasePages/BasePage.cs index 6f239e19f3..ab555a587a 100644 --- a/src/umbraco.businesslogic/BasePages/BasePage.cs +++ b/src/umbraco.businesslogic/BasePages/BasePage.cs @@ -228,6 +228,8 @@ namespace umbraco.BasePages if (StateHelper.Cookies.HasCookies && StateHelper.Cookies.UserContext.HasValue) return StateHelper.Cookies.UserContext.GetValue(); else + { + try { string encTicket = StateHelper.Cookies.UserContext.GetValue(); if (!String.IsNullOrEmpty(encTicket)) @@ -245,7 +247,7 @@ namespace umbraco.BasePages } } - return ""; + return ""; } set { diff --git a/src/umbraco.datalayer/Utility/Installer/DefaultInstallerUtility.cs b/src/umbraco.datalayer/Utility/Installer/DefaultInstallerUtility.cs index 816fc94a8d..882dbe19fa 100644 --- a/src/umbraco.datalayer/Utility/Installer/DefaultInstallerUtility.cs +++ b/src/umbraco.datalayer/Utility/Installer/DefaultInstallerUtility.cs @@ -171,7 +171,6 @@ namespace umbraco.DataLayer.Utility.Installer #endregion #region Protected Methods - protected virtual void NewInstall(string sql) {