Fixes some of the PhysicalFileSystem issues with paths vs relative paths, this should fix up inconsistencies.

This commit is contained in:
Shannon
2014-10-22 15:52:32 +10:00
parent 4494d8cc91
commit 9ffaa8aabb
2 changed files with 19 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ using System.IO;
namespace Umbraco.Core.IO
{
//TODO: There is no way to create a directory here without creating a file in a directory and then deleting it
//TODO: Should probably implement a rename?
public interface IFileSystem
{
@@ -15,8 +16,7 @@ namespace Umbraco.Core.IO
void DeleteDirectory(string path, bool recursive);
bool DirectoryExists(string path);
void AddFile(string path, Stream stream);
void AddFile(string path, Stream stream, bool overrideIfExists);

View File

@@ -90,15 +90,17 @@ namespace Umbraco.Core.IO
public void AddFile(string path, Stream stream, bool overrideIfExists)
{
var exists = FileExists(path);
var fsRelativePath = GetRelativePath(path);
var exists = FileExists(fsRelativePath);
if (exists && overrideIfExists == false) throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path));
EnsureDirectory(Path.GetDirectoryName(path));
EnsureDirectory(Path.GetDirectoryName(fsRelativePath));
if (stream.CanSeek)
stream.Seek(0, 0);
using (var destination = (Stream)File.Create(GetFullPath(path)))
using (var destination = (Stream)File.Create(GetFullPath(fsRelativePath)))
stream.CopyTo(destination);
}
@@ -109,12 +111,14 @@ namespace Umbraco.Core.IO
public IEnumerable<string> GetFiles(string path, string filter)
{
path = EnsureTrailingSeparator(GetFullPath(path));
var fsRelativePath = GetRelativePath(path);
var fullPath = EnsureTrailingSeparator(GetFullPath(fsRelativePath));
try
{
if (Directory.Exists(path))
return Directory.EnumerateFiles(path, filter).Select(GetRelativePath);
if (Directory.Exists(fullPath))
return Directory.EnumerateFiles(fullPath, filter).Select(GetRelativePath);
}
catch (UnauthorizedAccessException ex)
{
@@ -167,6 +171,12 @@ namespace Umbraco.Core.IO
public string GetFullPath(string path)
{
//if the path starts with a '/' then it's most likely not a FS relative path which is required so convert it
if (path.StartsWith("/"))
{
path = GetRelativePath(path);
}
return !path.StartsWith(RootPath)
? Path.Combine(RootPath, path)
: path;