deploy-219 - implement filesystem CanAddPhysical, AddFile from physical path

This commit is contained in:
Stephan
2017-02-09 14:53:03 +01:00
parent 8e5c57eb60
commit 308a61895e
6 changed files with 107 additions and 4 deletions

View File

@@ -109,5 +109,24 @@ namespace Umbraco.Core.IO
var wrapped2 = Wrapped as IFileSystem2;
return wrapped2 == null ? Wrapped.GetSize(path) : wrapped2.GetSize(path);
}
// explicitely implementing - not breaking
bool IFileSystem2.CanAddPhysical
{
get
{
var wrapped2 = Wrapped as IFileSystem2;
return wrapped2 != null && wrapped2.CanAddPhysical;
}
}
// explicitely implementing - not breaking
void IFileSystem2.AddFile(string path, string physicalPath, bool overrideIfExists, bool copy)
{
var wrapped2 = Wrapped as IFileSystem2;
if (wrapped2 == null)
throw new NotSupportedException();
wrapped2.AddFile(path, physicalPath, overrideIfExists, copy);
}
}
}

View File

@@ -44,6 +44,10 @@ namespace Umbraco.Core.IO
{
long GetSize(string path);
bool CanAddPhysical { get; }
void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false);
// TODO: implement these
//
//void CreateDirectory(string path);

View File

@@ -364,6 +364,29 @@ namespace Umbraco.Core.IO
return file.Exists ? file.Length : -1;
}
public bool CanAddPhysical { get { return true; } }
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false)
{
var fullPath = GetFullPath(path);
if (File.Exists(fullPath))
{
if (overrideIfExists == false)
throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path));
File.Delete(fullPath);
}
var directory = Path.GetDirectoryName(fullPath);
if (directory == null) throw new InvalidOperationException("Could not get directory.");
Directory.CreateDirectory(directory); // ensure it exists
if (copy)
File.Copy(physicalPath, fullPath);
else
File.Move(physicalPath, fullPath);
}
#region Helper Methods
protected virtual void EnsureDirectory(string path)

View File

@@ -33,8 +33,16 @@ namespace Umbraco.Core.IO
{
try
{
using (var stream = _sfs.OpenFile(kvp.Key))
_fs.AddFile(kvp.Key, stream, true);
var fs2 = _fs as IFileSystem2;
if (fs2 != null && fs2.CanAddPhysical)
{
fs2.AddFile(kvp.Key, _sfs.GetFullPath(kvp.Key)); // overwrite, move
}
else
{
using (var stream = _sfs.OpenFile(kvp.Key))
_fs.AddFile(kvp.Key, stream, true);
}
}
catch (Exception e)
{
@@ -282,5 +290,36 @@ namespace Umbraco.Core.IO
if (sf.IsDelete || sf.IsDir) throw new InvalidOperationException("Invalid path.");
return _sfs.GetSize(path);
}
public bool CanAddPhysical { get { return true; } }
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false)
{
ShadowNode sf;
var normPath = NormPath(path);
if (Nodes.TryGetValue(normPath, out sf) && sf.IsExist && (sf.IsDir || overrideIfExists == false))
throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path));
var parts = normPath.Split('/');
for (var i = 0; i < parts.Length - 1; i++)
{
var dirPath = string.Join("/", parts.Take(i + 1));
ShadowNode sd;
if (Nodes.TryGetValue(dirPath, out sd))
{
if (sd.IsFile) throw new InvalidOperationException("Invalid path.");
if (sd.IsDelete) Nodes[dirPath] = new ShadowNode(false, true);
}
else
{
if (_fs.DirectoryExists(dirPath)) continue;
if (_fs.FileExists(dirPath)) throw new InvalidOperationException("Invalid path.");
Nodes[dirPath] = new ShadowNode(false, true);
}
}
_sfs.AddFile(path, physicalPath, overrideIfExists, copy);
Nodes[normPath] = new ShadowNode(false, false);
}
}
}

View File

@@ -164,5 +164,22 @@ namespace Umbraco.Core.IO
var filesystem2 = filesystem as IFileSystem2;
return filesystem2 == null ? filesystem.GetSize(path) : filesystem2.GetSize(path);
}
public bool CanAddPhysical
{
get
{
var fileSystem2 = FileSystem as IFileSystem2;
return fileSystem2 != null && fileSystem2.CanAddPhysical;
}
}
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false)
{
var fileSystem2 = FileSystem as IFileSystem2;
if (fileSystem2 == null)
throw new NotSupportedException();
fileSystem2.AddFile(path, physicalPath, overrideIfExists, copy);
}
}
}

View File

@@ -364,7 +364,8 @@ namespace Umbraco.Tests.IO
ss.Complete();
Assert.IsTrue(File.Exists(path + "/ShadowSystem/path/to/some/dir/f1.txt")); // *not* cleaning
// yes we are cleaning now
//Assert.IsTrue(File.Exists(path + "/ShadowSystem/path/to/some/dir/f1.txt")); // *not* cleaning
Assert.IsTrue(File.Exists(path + "/ShadowTests/path/to/some/dir/f1.txt"));
Assert.IsFalse(File.Exists(path + "/ShadowTests/sub/sub/f2.txt"));
}
@@ -558,7 +559,7 @@ namespace Umbraco.Tests.IO
Assert.AreEqual(1, ae.InnerExceptions.Count);
e = ae.InnerExceptions[0];
Assert.IsNotNull(e.InnerException);
Assert.IsInstanceOf<UnauthorizedAccessException>(e.InnerException);
Assert.IsInstanceOf<Exception>(e.InnerException);
}
// still, the rest of the changes has been applied ok