From a9f1a382804c6b637fbea63d0fb28de7332bb19c Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 4 Aug 2013 10:26:37 -0700 Subject: [PATCH] cleaned up DirectoryLookupService --- .../DirectoryLookupServiceFixture.cs | 20 +----- NzbDrone.Api.Test/NzbDrone.Api.Test.csproj | 4 ++ NzbDrone.Api.Test/packages.config | 1 + .../Directories/DirectoryLookupService.cs | 57 +++++++++++++++++ NzbDrone.Api/Directories/DirectoryModule.cs | 11 ++-- NzbDrone.Api/NzbDrone.Api.csproj | 1 + NzbDrone.Common/DirectoryLookupService.cs | 62 ------------------- NzbDrone.Common/EnvironmentInfo/OsInfo.cs | 30 ++++----- 8 files changed, 83 insertions(+), 103 deletions(-) rename {NzbDrone.Common.Test => NzbDrone.Api.Test}/DirectoryLookupServiceFixture.cs (85%) create mode 100644 NzbDrone.Api/Directories/DirectoryLookupService.cs delete mode 100644 NzbDrone.Common/DirectoryLookupService.cs diff --git a/NzbDrone.Common.Test/DirectoryLookupServiceFixture.cs b/NzbDrone.Api.Test/DirectoryLookupServiceFixture.cs similarity index 85% rename from NzbDrone.Common.Test/DirectoryLookupServiceFixture.cs rename to NzbDrone.Api.Test/DirectoryLookupServiceFixture.cs index 53867ce47..2a8bb8911 100644 --- a/NzbDrone.Common.Test/DirectoryLookupServiceFixture.cs +++ b/NzbDrone.Api.Test/DirectoryLookupServiceFixture.cs @@ -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 @@ -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() - .Setup(s => s.GetDirectories(It.IsAny())) - .Returns(_folders.ToArray()); - - Subject.LookupSubDirectories(root).Should() - .HaveCount(_folders.Count); - } - [Test] public void should_not_contain_recycling_bin_for_root_of_drive() { diff --git a/NzbDrone.Api.Test/NzbDrone.Api.Test.csproj b/NzbDrone.Api.Test/NzbDrone.Api.Test.csproj index 463550ed9..7fa8111c1 100644 --- a/NzbDrone.Api.Test/NzbDrone.Api.Test.csproj +++ b/NzbDrone.Api.Test/NzbDrone.Api.Test.csproj @@ -40,6 +40,9 @@ False ..\packages\FluentAssertions.2.0.1\lib\net40\FluentAssertions.dll + + ..\packages\Moq.4.0.10827\lib\NET40\Moq.dll + ..\packages\NUnit.2.6.2\lib\nunit.framework.dll @@ -56,6 +59,7 @@ + diff --git a/NzbDrone.Api.Test/packages.config b/NzbDrone.Api.Test/packages.config index f0a2d165c..139196ed3 100644 --- a/NzbDrone.Api.Test/packages.config +++ b/NzbDrone.Api.Test/packages.config @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/NzbDrone.Api/Directories/DirectoryLookupService.cs b/NzbDrone.Api/Directories/DirectoryLookupService.cs new file mode 100644 index 000000000..38c6b38e4 --- /dev/null +++ b/NzbDrone.Api/Directories/DirectoryLookupService.cs @@ -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 LookupSubDirectories(string query); + } + + public class DirectoryLookupService : IDirectoryLookupService + { + private readonly IDiskProvider _diskProvider; + private readonly HashSet _setToRemove = new HashSet { "$Recycle.Bin", "System Volume Information" }; + + public DirectoryLookupService(IDiskProvider diskProvider) + { + _diskProvider = diskProvider; + } + + public List LookupSubDirectories(string query) + { + var dirs = new List(); + 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 GetSubDirectories(string path) + { + try + { + return _diskProvider.GetDirectories(path).ToList(); + } + catch (DirectoryNotFoundException) + { + return new List(); + + } + catch (ArgumentException) + { + return new List(); + } + } + } +} diff --git a/NzbDrone.Api/Directories/DirectoryModule.cs b/NzbDrone.Api/Directories/DirectoryModule.cs index 5ee387740..8ba29c5a0 100644 --- a/NzbDrone.Api/Directories/DirectoryModule.cs +++ b/NzbDrone.Api/Directories/DirectoryModule.cs @@ -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 @@ private Response GetDirectories() 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(); } diff --git a/NzbDrone.Api/NzbDrone.Api.csproj b/NzbDrone.Api/NzbDrone.Api.csproj index ad7a52ffd..300992cb5 100644 --- a/NzbDrone.Api/NzbDrone.Api.csproj +++ b/NzbDrone.Api/NzbDrone.Api.csproj @@ -86,6 +86,7 @@ + diff --git a/NzbDrone.Common/DirectoryLookupService.cs b/NzbDrone.Common/DirectoryLookupService.cs deleted file mode 100644 index 7d1677005..000000000 --- a/NzbDrone.Common/DirectoryLookupService.cs +++ /dev/null @@ -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 LookupSubDirectories(string query); - } - - public class DirectoryLookupService : IDirectoryLookupService - { - private readonly IDiskProvider _diskProvider; - - public DirectoryLookupService(IDiskProvider diskProvider) - { - _diskProvider = diskProvider; - } - - public List LookupSubDirectories(string query) - { - List 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(); - } - - return dirs; - } - } -} diff --git a/NzbDrone.Common/EnvironmentInfo/OsInfo.cs b/NzbDrone.Common/EnvironmentInfo/OsInfo.cs index 0afbac9de..b30c2c2ee 100644 --- a/NzbDrone.Common/EnvironmentInfo/OsInfo.cs +++ b/NzbDrone.Common/EnvironmentInfo/OsInfo.cs @@ -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 IsLinux + public static bool IsMono { get; private set; } + + public static bool IsLinux { get; private set; } + + public static bool IsWindows { get { - int p = (int)Environment.OSVersion.Platform; - return (p == 4) || (p == 6) || (p == 128); + return !IsLinux; } } }