cleaned up DirectoryLookupService

pull/4/head
kay.one 11 years ago
parent 57bb37a8cd
commit a9f1a38280

@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Api.Directories;
using NzbDrone.Common;
using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test
namespace NzbDrone.Api.Test
{
[TestFixture]
public class DirectoryLookupServiceFixture :TestBase<DirectoryLookupService>
@ -50,20 +50,6 @@ namespace NzbDrone.Common.Test
});
}
[Test]
public void should_get_all_folder_for_none_root_path()
{
const string root = @"C:\Test\";
SetupFolders(root);
Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetDirectories(It.IsAny<String>()))
.Returns(_folders.ToArray());
Subject.LookupSubDirectories(root).Should()
.HaveCount(_folders.Count);
}
[Test]
public void should_not_contain_recycling_bin_for_root_of_drive()
{

@ -40,6 +40,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\FluentAssertions.2.0.1\lib\net40\FluentAssertions.dll</HintPath>
</Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
@ -56,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ClientSchemaTests\SchemaBuilderFixture.cs" />
<Compile Include="DirectoryLookupServiceFixture.cs" />
<Compile Include="MappingTests\ResourceMappingFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

@ -4,4 +4,5 @@
<package id="NBuilder" version="3.0.1.1" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
<package id="valueinjecter" version="2.3.3" targetFramework="net40" />
<package id="Moq" version="4.0.10827" />
</packages>

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NzbDrone.Common;
namespace NzbDrone.Api.Directories
{
public interface IDirectoryLookupService
{
List<string> LookupSubDirectories(string query);
}
public class DirectoryLookupService : IDirectoryLookupService
{
private readonly IDiskProvider _diskProvider;
private readonly HashSet<string> _setToRemove = new HashSet<string> { "$Recycle.Bin", "System Volume Information" };
public DirectoryLookupService(IDiskProvider diskProvider)
{
_diskProvider = diskProvider;
}
public List<string> LookupSubDirectories(string query)
{
var dirs = new List<string>();
var lastSeparatorIndex = query.LastIndexOf(Path.DirectorySeparatorChar);
var path = query.Substring(0, lastSeparatorIndex + 1);
if (lastSeparatorIndex != -1)
{
dirs = GetSubDirectories(path);
dirs.RemoveAll(x => _setToRemove.Contains(new DirectoryInfo(x).Name));
}
return dirs;
}
private List<string> GetSubDirectories(string path)
{
try
{
return _diskProvider.GetDirectories(path).ToList();
}
catch (DirectoryNotFoundException)
{
return new List<string>();
}
catch (ArgumentException)
{
return new List<string>();
}
}
}
}

@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Nancy;
using NzbDrone.Api.Extensions;
@ -26,10 +24,9 @@ namespace NzbDrone.Api.Directories
string query = Request.Query.query.Value;
var dirs = _directoryLookupService.LookupSubDirectories(query);
if (dirs == null)
throw new Exception("A valid path was not provided");
var dirs = _directoryLookupService.LookupSubDirectories(query)
.Select(p => p.GetActualCasing())
.ToList();
return dirs.AsResponse();
}

@ -86,6 +86,7 @@
<Compile Include="Commands\CommandResource.cs" />
<Compile Include="Config\NamingConfigResource.cs" />
<Compile Include="Config\NamingModule.cs" />
<Compile Include="Directories\DirectoryLookupService.cs" />
<Compile Include="Directories\DirectoryModule.cs" />
<Compile Include="Episodes\EpisodeModule.cs" />
<Compile Include="Episodes\EpisodeResource.cs" />

@ -1,62 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace NzbDrone.Common
{
public interface IDirectoryLookupService
{
List<String> LookupSubDirectories(string query);
}
public class DirectoryLookupService : IDirectoryLookupService
{
private readonly IDiskProvider _diskProvider;
public DirectoryLookupService(IDiskProvider diskProvider)
{
_diskProvider = diskProvider;
}
public List<String> LookupSubDirectories(string query)
{
List<String> dirs = null;
try
{
//Windows (Including UNC)
var windowsSep = query.LastIndexOf('\\');
if (windowsSep > -1)
{
var path = query.Substring(0, windowsSep + 1);
var dirsList = _diskProvider.GetDirectories(path).ToList();
if (Path.GetPathRoot(path).Equals(path, StringComparison.InvariantCultureIgnoreCase))
{
var setToRemove = _diskProvider.SpecialFolders;
dirsList.RemoveAll(x => setToRemove.Contains(new DirectoryInfo(x.ToLowerInvariant()).Name));
}
dirs = dirsList;
}
//Unix
var index = query.LastIndexOf('/');
if (index > -1)
{
dirs = _diskProvider.GetDirectories(query.Substring(0, index + 1)).ToList();
}
}
catch (Exception)
{
//Swallow the exceptions so proper JSON is returned to the client (Empty results)
return new List<string>();
}
return dirs;
}
}
}

@ -5,31 +5,27 @@ namespace NzbDrone.Common.EnvironmentInfo
public static class OsInfo
{
public static Version Version
static OsInfo()
{
get
{
OperatingSystem os = Environment.OSVersion;
Version version = os.Version;
Version = Environment.OSVersion.Version;
IsMono = Type.GetType("Mono.Runtime") != null;
return version;
}
int platform = (int)Environment.OSVersion.Platform;
IsLinux = (platform == 4) || (platform == 6) || (platform == 128);
}
public static bool IsMono
{
get
{
return Type.GetType("Mono.Runtime") != null;
}
}
public static Version Version { get; private set; }
public static bool IsMono { get; private set; }
public static bool IsLinux { get; private set; }
public static bool IsLinux
public static bool IsWindows
{
get
{
int p = (int)Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
return !IsLinux;
}
}
}

Loading…
Cancel
Save