Added IFileSystem
Fixed issue with last merge
This commit is contained in:
39
src/Umbraco.Core/IO/IFileSystem.cs
Normal file
39
src/Umbraco.Core/IO/IFileSystem.cs
Normal file
@@ -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<string> GetFiles(string path);
|
||||
|
||||
IEnumerable<string> GetFiles(string path, string filter);
|
||||
|
||||
IEnumerable<string> 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);
|
||||
}
|
||||
}
|
||||
182
src/Umbraco.Core/IO/PhysicalFileSystem.cs
Normal file
182
src/Umbraco.Core/IO/PhysicalFileSystem.cs
Normal file
@@ -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<string> 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<string>();
|
||||
}
|
||||
|
||||
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<string> GetFiles(string path)
|
||||
{
|
||||
return GetFiles(path, "*.*");
|
||||
}
|
||||
|
||||
public IEnumerable<string> 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<string>();
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,5 @@ using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("cms")]
|
||||
[assembly: InternalsVisibleTo("umbraco")]
|
||||
[assembly: InternalsVisibleTo("businesslogic")]
|
||||
[assembly: InternalsVisibleTo("businesslogic")]
|
||||
[assembly: InternalsVisibleTo("Umbraco.Tests")]
|
||||
@@ -47,6 +47,8 @@
|
||||
<Compile Include="DelegateEqualityComparer.cs" />
|
||||
<Compile Include="EnumerableExtensions.cs" />
|
||||
<Compile Include="IfExtensions.cs" />
|
||||
<Compile Include="IO\IFileSystem.cs" />
|
||||
<Compile Include="IO\PhysicalFileSystem.cs" />
|
||||
<Compile Include="IThumbnailProvider.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="StringAliasCaseType.cs" />
|
||||
|
||||
156
src/Umbraco.Tests/IO/AbstractFileSystemTests.cs
Normal file
156
src/Umbraco.Tests/IO/AbstractFileSystemTests.cs
Normal file
@@ -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
|
||||
}
|
||||
}
|
||||
25
src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs
Normal file
25
src/Umbraco.Tests/IO/PhysicalFileSystemTests.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,8 @@
|
||||
<Compile Include="BusinessLogic\ApplicationTreeTest.cs" />
|
||||
<Compile Include="BusinessLogic\BaseTest.cs" />
|
||||
<Compile Include="EnumerableExtensionsTests.cs" />
|
||||
<Compile Include="IO\AbstractFileSystemTests.cs" />
|
||||
<Compile Include="IO\PhysicalFileSystemTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="StringExtensionsTests.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -171,7 +171,6 @@ namespace umbraco.DataLayer.Utility.Installer
|
||||
#endregion
|
||||
|
||||
#region Protected Methods
|
||||
|
||||
|
||||
protected virtual void NewInstall(string sql)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user