mirror of https://github.com/Sonarr/Sonarr
DiskProvider.Move now overwrites existing folder, Update some Diskprovider to use .NET 4 calls.
This commit is contained in:
parent
c1b5f2ebb6
commit
d97a1d068c
|
@ -13,6 +13,7 @@ namespace NzbDrone.Common.Test
|
||||||
{
|
{
|
||||||
DirectoryInfo BinFolder;
|
DirectoryInfo BinFolder;
|
||||||
DirectoryInfo BinFolderCopy;
|
DirectoryInfo BinFolderCopy;
|
||||||
|
DirectoryInfo BinFolderMove;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
|
@ -20,11 +21,17 @@ namespace NzbDrone.Common.Test
|
||||||
var binRoot = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent;
|
var binRoot = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent;
|
||||||
BinFolder = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin"));
|
BinFolder = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin"));
|
||||||
BinFolderCopy = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_copy"));
|
BinFolderCopy = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_copy"));
|
||||||
|
BinFolderMove = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_move"));
|
||||||
|
|
||||||
if (BinFolderCopy.Exists)
|
if (BinFolderCopy.Exists)
|
||||||
{
|
{
|
||||||
BinFolderCopy.Delete(true);
|
BinFolderCopy.Delete(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (BinFolderMove.Exists)
|
||||||
|
{
|
||||||
|
BinFolderMove.Delete(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -56,6 +63,22 @@ namespace NzbDrone.Common.Test
|
||||||
VerifyCopy();
|
VerifyCopy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void MoveFolder_should_overright_existing_folder()
|
||||||
|
{
|
||||||
|
var diskProvider = new DiskProvider();
|
||||||
|
|
||||||
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName);
|
||||||
|
diskProvider.CopyDirectory(BinFolder.FullName, BinFolderMove.FullName);
|
||||||
|
VerifyCopy();
|
||||||
|
|
||||||
|
//Act
|
||||||
|
diskProvider.MoveDirectory(BinFolderCopy.FullName, BinFolderMove.FullName);
|
||||||
|
|
||||||
|
//Assert
|
||||||
|
VerifyMove();
|
||||||
|
}
|
||||||
|
|
||||||
private void VerifyCopy()
|
private void VerifyCopy()
|
||||||
{
|
{
|
||||||
BinFolder.Refresh();
|
BinFolder.Refresh();
|
||||||
|
@ -66,5 +89,19 @@ namespace NzbDrone.Common.Test
|
||||||
|
|
||||||
BinFolderCopy.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories());
|
BinFolderCopy.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void VerifyMove()
|
||||||
|
{
|
||||||
|
BinFolder.Refresh();
|
||||||
|
BinFolderCopy.Refresh();
|
||||||
|
BinFolderMove.Refresh();
|
||||||
|
|
||||||
|
BinFolderCopy.Exists.Should().BeFalse();
|
||||||
|
|
||||||
|
BinFolderMove.GetFiles("*.*", SearchOption.AllDirectories)
|
||||||
|
.Should().HaveSameCount(BinFolder.GetFiles("*.*", SearchOption.AllDirectories));
|
||||||
|
|
||||||
|
BinFolderMove.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,12 @@ namespace NzbDrone.Common
|
||||||
{
|
{
|
||||||
public class DiskProvider
|
public class DiskProvider
|
||||||
{
|
{
|
||||||
|
enum TransferAction
|
||||||
|
{
|
||||||
|
Copy,
|
||||||
|
Move
|
||||||
|
}
|
||||||
|
|
||||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
|
||||||
[return: MarshalAs(UnmanagedType.Bool)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
||||||
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
|
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
|
||||||
|
@ -28,14 +34,14 @@ namespace NzbDrone.Common
|
||||||
return File.Exists(path);
|
return File.Exists(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string[] GetDirectories(string path)
|
public virtual IEnumerable<string> GetDirectories(string path)
|
||||||
{
|
{
|
||||||
return Directory.GetDirectories(path);
|
return Directory.EnumerateDirectories(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string[] GetFiles(string path, SearchOption searchOption)
|
public virtual IEnumerable<string> GetFiles(string path, SearchOption searchOption)
|
||||||
{
|
{
|
||||||
return Directory.GetFiles(path, "*.*", searchOption);
|
return Directory.EnumerateFiles(path, "*.*", searchOption);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual long GetDirectorySize(string path)
|
public virtual long GetDirectorySize(string path)
|
||||||
|
@ -57,7 +63,27 @@ namespace NzbDrone.Common
|
||||||
|
|
||||||
public virtual void CopyDirectory(string source, string target)
|
public virtual void CopyDirectory(string source, string target)
|
||||||
{
|
{
|
||||||
Logger.Trace("Copying {0} -> {1}", source, target);
|
TransferDirectory(source, target, TransferAction.Copy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void MoveDirectory(string source, string destination)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TransferDirectory(source, destination, TransferAction.Move);
|
||||||
|
Directory.Delete(source, true);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
e.Data.Add("Source", source);
|
||||||
|
e.Data.Add("Destination", destination);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TransferDirectory(string source, string target, TransferAction transferAction)
|
||||||
|
{
|
||||||
|
Logger.Trace("{0} {1} -> {2}", transferAction, source, target);
|
||||||
|
|
||||||
var sourceFolder = new DirectoryInfo(source);
|
var sourceFolder = new DirectoryInfo(source);
|
||||||
var targetFolder = new DirectoryInfo(target);
|
var targetFolder = new DirectoryInfo(target);
|
||||||
|
@ -69,13 +95,31 @@ namespace NzbDrone.Common
|
||||||
|
|
||||||
foreach (var subDir in sourceFolder.GetDirectories())
|
foreach (var subDir in sourceFolder.GetDirectories())
|
||||||
{
|
{
|
||||||
CopyDirectory(subDir.FullName, Path.Combine(target, subDir.Name));
|
TransferDirectory(subDir.FullName, Path.Combine(target, subDir.Name), transferAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var file in sourceFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly))
|
foreach (var file in sourceFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly))
|
||||||
{
|
{
|
||||||
var destFile = Path.Combine(target, file.Name);
|
var destFile = Path.Combine(target, file.Name);
|
||||||
file.CopyTo(destFile, true);
|
|
||||||
|
switch (transferAction)
|
||||||
|
{
|
||||||
|
case TransferAction.Copy:
|
||||||
|
{
|
||||||
|
file.CopyTo(destFile, true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TransferAction.Move:
|
||||||
|
{
|
||||||
|
if (FileExists(destFile))
|
||||||
|
{
|
||||||
|
File.Delete(destFile);
|
||||||
|
}
|
||||||
|
file.MoveTo(destFile);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,21 +145,7 @@ namespace NzbDrone.Common
|
||||||
|
|
||||||
public virtual IEnumerable<FileInfo> GetFileInfos(string path, string pattern, SearchOption searchOption)
|
public virtual IEnumerable<FileInfo> GetFileInfos(string path, string pattern, SearchOption searchOption)
|
||||||
{
|
{
|
||||||
return new DirectoryInfo(path).GetFiles(pattern, searchOption);
|
return new DirectoryInfo(path).EnumerateFiles(pattern, searchOption);
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void MoveDirectory(string source, string destination)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Directory.Move(source, destination);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
e.Data.Add("Source", source);
|
|
||||||
e.Data.Add("Destination", destination);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void InheritFolderPermissions(string filename)
|
public virtual void InheritFolderPermissions(string filename)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Web.Mvc;
|
using System.Collections.Generic;
|
||||||
|
using System.Web.Mvc;
|
||||||
using NzbDrone.Common;
|
using NzbDrone.Common;
|
||||||
|
|
||||||
namespace NzbDrone.Web.Controllers
|
namespace NzbDrone.Web.Controllers
|
||||||
|
@ -27,7 +28,7 @@ namespace NzbDrone.Web.Controllers
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public JsonResult GetDirectories(string term)
|
public JsonResult GetDirectories(string term)
|
||||||
{
|
{
|
||||||
string[] dirs = null;
|
IEnumerable<string> dirs = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//Windows (Including UNC)
|
//Windows (Including UNC)
|
||||||
|
|
Loading…
Reference in New Issue