diff --git a/src/NzbDrone.Common.Test/PathExtensionFixture.cs b/src/NzbDrone.Common.Test/PathExtensionFixture.cs index 7d9b3497a..c96414d1c 100644 --- a/src/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/src/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -157,6 +157,25 @@ namespace NzbDrone.Common.Test path.GetParentPath().Should().Be(parentPath); } + [TestCase(@"C:\Test\mydir", "Test")] + [TestCase(@"C:\Test\", @"C:\")] + [TestCase(@"C:\", null)] + [TestCase(@"\\server\share", null)] + [TestCase(@"\\server\share\test", @"\\server\share")] + public void path_should_return_parent_name_windows(string path, string parentPath) + { + WindowsOnly(); + path.GetParentName().Should().Be(parentPath); + } + + [TestCase(@"/", null)] + [TestCase(@"/test", "/")] + public void path_should_return_parent_name_mono(string path, string parentPath) + { + PosixOnly(); + path.GetParentName().Should().Be(parentPath); + } + [Test] public void path_should_return_parent_for_oversized_path() { diff --git a/src/NzbDrone.Common/Extensions/PathExtensions.cs b/src/NzbDrone.Common/Extensions/PathExtensions.cs index 32a00f694..bf105d1ec 100644 --- a/src/NzbDrone.Common/Extensions/PathExtensions.cs +++ b/src/NzbDrone.Common/Extensions/PathExtensions.cs @@ -86,9 +86,7 @@ namespace NzbDrone.Common.Extensions public static string GetParentPath(this string childPath) { - var cleanPath = OsInfo.IsWindows - ? PARENT_PATH_END_SLASH_REGEX.Replace(childPath, "") - : childPath.TrimEnd(Path.DirectorySeparatorChar); + var cleanPath = childPath.GetCleanPath(); if (cleanPath.IsNullOrWhiteSpace()) { @@ -98,6 +96,13 @@ namespace NzbDrone.Common.Extensions return Directory.GetParent(cleanPath)?.FullName; } + public static string GetParentName(this string childPath) + { + var cleanPath = childPath.GetCleanPath(); + + return Directory.GetParent(cleanPath)?.Name; + } + public static string GetCleanPath(this string path) { var cleanPath = OsInfo.IsWindows diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/184_remove_invalid_roksbox_metadata_imagesFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/184_remove_invalid_roksbox_metadata_imagesFixture.cs new file mode 100644 index 000000000..a67e6786b --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/184_remove_invalid_roksbox_metadata_imagesFixture.cs @@ -0,0 +1,67 @@ +using System; +using System.Linq; +using Dapper; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class remove_invalid_roksbox_metadata_imagesFixture : MigrationTest + { + [Test] + public void should_remove_incorrect_roksbox_metadata_images() + { + var db = WithDapperMigrationTestDb(c => + { + c.Insert.IntoTable("MetadataFiles").Row(new + { + SeriesId = 1, + Consumer = "RoksboxMetadata", + Type = 5, + RelativePath = @"Season 01\metadata\S01E01.jpg", + LastUpdated = "2023-01-21 00:00:00.000", + SeasonNumber = 1, + EpisodeFileId = 1, + Added = "2023-01-21 00:00:00.000", + Extension = ".jpg" + }); + + c.Insert.IntoTable("MetadataFiles").Row(new + { + SeriesId = 1, + Consumer = "RoksboxMetadata", + Type = 5, + RelativePath = @"Season 01\S01E01.jpg", + LastUpdated = "2023-01-21 00:00:00.000", + SeasonNumber = 1, + EpisodeFileId = 1, + Added = "2023-01-21 00:00:00.000", + Extension = ".jpg" + }); + }); + + var metadataFiles = db.Query("SELECT * FROM MetadataFiles"); + + metadataFiles.Should().HaveCount(1); + metadataFiles.First().RelativePath.Should().NotContain("metadata"); + } + } + + public class MetadataFile184 + { + public int Id { get; set; } + public int SeriesId { get; set; } + public int? EpisodeFileId { get; set; } + public int? SeasonNumber { get; set; } + public string RelativePath { get; set; } + public DateTime Added { get; set; } + public DateTime LastUpdated { get; set; } + public string Extension { get; set; } + public string Hash { get; set; } + public string Consumer { get; set; } + public int Type { get; set; } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/184_remove_invalid_roksbox_metadata_images.cs b/src/NzbDrone.Core/Datastore/Migration/184_remove_invalid_roksbox_metadata_images.cs new file mode 100644 index 000000000..ccb6d1d9e --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/184_remove_invalid_roksbox_metadata_images.cs @@ -0,0 +1,14 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(184)] + public class remove_invalid_roksbox_metadata_images : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.Sql(@"DELETE FROM MetadataFiles WHERE Consumer = 'RoksboxMetadata' AND Type = 5 AND (RelativePath LIKE '%/metadata/%' OR RelativePath LIKE '%\metadata\%')"); + } + } +} diff --git a/src/NzbDrone.Core/Extras/Metadata/Consumers/Roksbox/RoksboxMetadata.cs b/src/NzbDrone.Core/Extras/Metadata/Consumers/Roksbox/RoksboxMetadata.cs index b8e717392..f93243274 100644 --- a/src/NzbDrone.Core/Extras/Metadata/Consumers/Roksbox/RoksboxMetadata.cs +++ b/src/NzbDrone.Core/Extras/Metadata/Consumers/Roksbox/RoksboxMetadata.cs @@ -112,7 +112,8 @@ namespace NzbDrone.Core.Extras.Metadata.Consumers.Roksbox if (extension == ".jpg") { - if (!Path.GetFileNameWithoutExtension(filename).EndsWith("-thumb")) + if (!Path.GetFileNameWithoutExtension(filename).EndsWith("-thumb") && + !path.GetParentName().Equals("metadata", StringComparison.InvariantCultureIgnoreCase)) { metadata.Type = MetadataType.EpisodeImage; return metadata;