From 839a2ac7424cd9a950931dffb8094496570440bb Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Wed, 25 Jun 2014 20:40:53 +0200 Subject: [PATCH] Fixed: NzbDrone running on Windows should no longer fail while getting a very long path from a sabnzbd running on linux. --- .../PathExtensionFixture.cs | 17 +++++++++++++++++ src/NzbDrone.Common/PathExtensions.cs | 16 ++++++++++++++++ .../Download/Clients/Sabnzbd/Sabnzbd.cs | 6 +++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 4b8c355dd..734b15e13 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -136,6 +136,23 @@ namespace NzbDrone.Common.Test parentPath.IsParentPath(childPath).Should().Be(expectedResult); } + [TestCase(@"C:\Test\mydir", @"C:\Test")] + [TestCase(@"C:\Test\", @"C:")] + [TestCase(@"C:\", null)] + public void path_should_return_parent(string path, string parentPath) + { + path.GetParentPath().Should().Be(parentPath); + } + + [Test] + public void path_should_return_parent_for_oversized_path() + { + var path = @"/media/2e168617-f2ae-43fb-b88c-3663af1c8eea/downloads/sabnzbd/nzbdrone/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories"; + var parentPath = @"/media/2e168617-f2ae-43fb-b88c-3663af1c8eea/downloads/sabnzbd/nzbdrone/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing/With.Alot.Of.Nested.Directories/Some.Real.Big.Thing"; + + path.GetParentPath().Should().Be(parentPath); + } + [Test] [Ignore] public void should_not_be_parent_when_it_is_grandparent() diff --git a/src/NzbDrone.Common/PathExtensions.cs b/src/NzbDrone.Common/PathExtensions.cs index 7598f84f8..f57051f61 100644 --- a/src/NzbDrone.Common/PathExtensions.cs +++ b/src/NzbDrone.Common/PathExtensions.cs @@ -53,6 +53,22 @@ namespace NzbDrone.Common return childPath.Substring(parentPath.Length).Trim(Path.DirectorySeparatorChar); } + public static string GetParentPath(this string childPath) + { + var parentPath = childPath.TrimEnd('\\', '/'); + + var index = parentPath.LastIndexOfAny(new[] { '\\', '/' }); + + if (index != -1) + { + return parentPath.Substring(0, index); + } + else + { + return null; + } + } + public static bool IsParentPath(this string parentPath, string childPath) { parentPath = parentPath.TrimEnd(Path.DirectorySeparatorChar); diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs index 5c5a05e31..2ce1bef1f 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs @@ -161,10 +161,10 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd if (!sabHistoryItem.Storage.IsNullOrWhiteSpace()) { - var parent = Directory.GetParent(sabHistoryItem.Storage); - if (parent != null && parent.Name == sabHistoryItem.Title) + var parent = sabHistoryItem.Storage.GetParentPath(); + if (parent != null && Path.GetFileName(parent) == sabHistoryItem.Title) { - historyItem.OutputPath = parent.FullName; + historyItem.OutputPath = parent; } else {