1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

cleaned up DirectoryLookupService

This commit is contained in:
kay.one 2013-08-04 10:26:37 -07:00
parent 57bb37a8cd
commit a9f1a38280
8 changed files with 83 additions and 103 deletions

View File

@ -1,14 +1,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions; using FluentAssertions;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Api.Directories;
using NzbDrone.Common;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test namespace NzbDrone.Api.Test
{ {
[TestFixture] [TestFixture]
public class DirectoryLookupServiceFixture :TestBase<DirectoryLookupService> public class DirectoryLookupServiceFixture :TestBase<DirectoryLookupService>
@ -50,20 +50,6 @@ private void SetupFolders(string root)
}); });
} }
[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] [Test]
public void should_not_contain_recycling_bin_for_root_of_drive() public void should_not_contain_recycling_bin_for_root_of_drive()
{ {

View File

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

View File

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

View File

@ -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>();
}
}
}
}

View File

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

View File

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

View File

@ -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;
}
}
}

View File

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