2012-08-13 10:04:31 -01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2012-08-13 10:23:45 -01:00
|
|
|
|
using System.Web;
|
2012-08-13 10:04:31 -01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.IO
|
|
|
|
|
|
{
|
2012-08-20 10:46:32 -01:00
|
|
|
|
internal class PhysicalFileSystem : IFileSystem
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly string _rootPath;
|
|
|
|
|
|
private readonly string _rootUrl;
|
|
|
|
|
|
|
2012-08-13 10:23:45 -01:00
|
|
|
|
public PhysicalFileSystem(string virtualRoot)
|
|
|
|
|
|
{
|
2012-08-20 09:42:32 -01:00
|
|
|
|
_rootPath = System.Web.Hosting.HostingEnvironment.MapPath(virtualRoot);
|
2012-08-13 10:23:45 -01:00
|
|
|
|
_rootUrl = VirtualPathUtility.ToAbsolute(virtualRoot);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-13 10:04:31 -01:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public IEnumerable<string> GetDirectories(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
path = EnsureTrailingSeparator(GetFullPath(path));
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Directory.Exists(path))
|
2012-08-14 09:11:49 -01:00
|
|
|
|
return Directory.EnumerateDirectories(path).Select(GetRelativePath);
|
2012-08-13 10:04:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
return Enumerable.Empty<string>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public void DeleteDirectory(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
DeleteDirectory(path, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public void DeleteDirectory(string path, bool recursive)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
if (!DirectoryExists(path))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(GetFullPath(path), recursive);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public bool DirectoryExists(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return Directory.Exists(GetFullPath(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public void AddFile(string path, Stream stream)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
AddFile(path, stream, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public void AddFile(string path, Stream stream, bool overrideIfExists)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
if (FileExists(path) && !overrideIfExists)
|
|
|
|
|
|
throw new InvalidOperationException(string.Format("A file at path '{0}' already exists",
|
|
|
|
|
|
path));
|
|
|
|
|
|
|
|
|
|
|
|
EnsureDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
|
2012-08-20 10:51:14 -01:00
|
|
|
|
if (stream.CanSeek)
|
|
|
|
|
|
stream.Seek(0, 0);
|
|
|
|
|
|
|
2012-08-13 10:04:31 -01:00
|
|
|
|
using (var destination = (Stream)File.Create(GetFullPath(path)))
|
|
|
|
|
|
stream.CopyTo(destination);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public IEnumerable<string> GetFiles(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return GetFiles(path, "*.*");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public IEnumerable<string> GetFiles(string path, string filter)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
path = EnsureTrailingSeparator(GetFullPath(path));
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Directory.Exists(path))
|
2012-08-14 09:11:49 -01:00
|
|
|
|
return Directory.EnumerateFiles(path, filter).Select(GetRelativePath);
|
2012-08-13 10:04:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
return Enumerable.Empty<string>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public Stream OpenFile(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return File.OpenRead(GetFullPath(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public void DeleteFile(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
if (!FileExists(path))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(GetFullPath(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
|
|
{ }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public bool FileExists(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return File.Exists(GetFullPath(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public string GetRelativePath(string fullPathOrUrl)
|
2012-08-14 09:11:49 -01:00
|
|
|
|
{
|
|
|
|
|
|
var relativePath = fullPathOrUrl
|
|
|
|
|
|
.TrimStart(_rootUrl)
|
|
|
|
|
|
.Replace('/', Path.DirectorySeparatorChar)
|
|
|
|
|
|
.TrimStart(_rootPath)
|
|
|
|
|
|
.TrimStart(Path.DirectorySeparatorChar);
|
|
|
|
|
|
|
|
|
|
|
|
return relativePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public string GetFullPath(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return !path.StartsWith(_rootPath)
|
|
|
|
|
|
? Path.Combine(_rootPath, path)
|
|
|
|
|
|
: path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public string GetUrl(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return _rootUrl.TrimEnd("/") + "/" + path
|
|
|
|
|
|
.TrimStart(Path.DirectorySeparatorChar)
|
2012-08-21 06:42:01 -01:00
|
|
|
|
.Replace(Path.DirectorySeparatorChar, '/')
|
|
|
|
|
|
.TrimEnd("/");
|
2012-08-13 10:04:31 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public DateTimeOffset GetLastModified(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return DirectoryExists(path)
|
|
|
|
|
|
? new DirectoryInfo(GetFullPath(path)).LastWriteTimeUtc
|
|
|
|
|
|
: new FileInfo(GetFullPath(path)).LastWriteTimeUtc;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-20 10:46:32 -01:00
|
|
|
|
public DateTimeOffset GetCreated(string path)
|
2012-08-13 10:04:31 -01:00
|
|
|
|
{
|
|
|
|
|
|
return DirectoryExists(path)
|
|
|
|
|
|
? Directory.GetCreationTimeUtc(GetFullPath(path))
|
|
|
|
|
|
: File.GetCreationTimeUtc(GetFullPath(path));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Helper Methods
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|