diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/108_fix_metadata_file_extensionsFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/108_fix_metadata_file_extensionsFixture.cs new file mode 100644 index 000000000..1eb0bd802 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/108_fix_metadata_file_extensionsFixture.cs @@ -0,0 +1,61 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Parser; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class fix_extra_file_extensionsFixture : MigrationTest + { + [Test] + public void should_fix_double_extension() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("SubtitleFiles").Row(new + { + SeriesId = 1, + SeasonNumber = 1, + EpisodeFileId = 1, + RelativePath = "Series.Title.S01E01.en.srt", + Added = "2016-05-30 20:23:02.3725923", + LastUpdated = "2016-05-30 20:23:02.3725923", + Language = Language.English, + Extension = "en.srt" + }); + }); + + var items = db.Query("Select * from SubtitleFiles"); + + items.Should().HaveCount(1); + items.First()["Extension"].Should().Be(".srt"); + } + + [Test] + public void should_fix_extension_missing_a_leading_period() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("ExtraFiles").Row(new + { + SeriesId = 1, + SeasonNumber = 1, + EpisodeFileId = 1, + RelativePath = "Series.Title.S01E01.nfo-orig", + Added = "2016-05-30 20:23:02.3725923", + LastUpdated = "2016-05-30 20:23:02.3725923", + Extension = "nfo-orig" + }); + }); + + var items = db.Query("Select * from ExtraFiles"); + + items.Should().HaveCount(1); + items.First()["Extension"].Should().Be(".nfo-orig"); + } + } + +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 22fcc354f..7dd46c827 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -124,6 +124,7 @@ + diff --git a/src/NzbDrone.Core/Datastore/Migration/108_fix_extra_file_extension.cs b/src/NzbDrone.Core/Datastore/Migration/108_fix_extra_file_extension.cs new file mode 100644 index 000000000..3093ebbda --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/108_fix_extra_file_extension.cs @@ -0,0 +1,52 @@ +using System; +using System.Data; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(108)] + public class fix_extra_file_extension : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(FixExtraFileExtension); + } + + private void FixExtraFileExtension(IDbConnection conn, IDbTransaction tran) + { + FixExtraFileExtensionForTable(conn, tran, "ExtraFiles"); + FixExtraFileExtensionForTable(conn, tran, "SubtitleFiles"); + } + + private void FixExtraFileExtensionForTable(IDbConnection conn, IDbTransaction tran, string table) + { + + using (var cmd = conn.CreateCommand()) + { + cmd.Transaction = tran; + cmd.CommandText = $"SELECT Id, RelativePath FROM {table}"; + + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + var id = reader.GetInt32(0); + var relativePath = reader.GetString(1); + var extension = relativePath.Substring(relativePath.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase)); + + using (var updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = $"UPDATE {table} SET Extension = ? WHERE Id = ?"; + updateCmd.AddParameter(extension); + updateCmd.AddParameter(id); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + } +} diff --git a/src/NzbDrone.Core/Extras/ExtraService.cs b/src/NzbDrone.Core/Extras/ExtraService.cs index 5bebe546d..d35dcef15 100644 --- a/src/NzbDrone.Core/Extras/ExtraService.cs +++ b/src/NzbDrone.Core/Extras/ExtraService.cs @@ -81,7 +81,8 @@ public void ImportExtraFiles(LocalEpisode localEpisode, EpisodeFile episodeFile, { foreach (var extraFileManager in _extraFileManagers) { - var extraFile = extraFileManager.Import(series, episodeFile, matchingFilename, matchingExtension, isReadOnly); + var extension = Path.GetExtension(matchingFilename); + var extraFile = extraFileManager.Import(series, episodeFile, matchingFilename, extension, isReadOnly); if (extraFile != null) { diff --git a/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs b/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs index 6d4450f72..f21e989aa 100644 --- a/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs +++ b/src/NzbDrone.Core/Extras/Files/ExtraFileManager.cs @@ -90,6 +90,7 @@ protected TExtraFile MoveFile(Series series, EpisodeFile episodeFile, TExtraFile filenameBuilder.Append(fileNameSuffix); } + filenameBuilder.Append("."); filenameBuilder.Append(extraFile.Extension); var existingFileName = Path.Combine(series.Path, extraFile.RelativePath); diff --git a/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs b/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs index 78ff805a2..52910a285 100644 --- a/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs +++ b/src/NzbDrone.Core/Extras/Subtitles/SubtitleService.cs @@ -101,24 +101,21 @@ public override ExtraFile Import(Series series, EpisodeFile episodeFile, string private string GetSuffix(Language language, int copy, bool multipleCopies = false) { - var extensionBuilder = new StringBuilder("."); + var suffixBuilder = new StringBuilder(); if (multipleCopies) { - extensionBuilder.Append(copy); - } - - if (multipleCopies && language != Language.Unknown) - { - extensionBuilder.Append("."); + suffixBuilder.Append("."); + suffixBuilder.Append(copy); } if (language != Language.Unknown) { - extensionBuilder.Append(IsoLanguages.Get(language).TwoLetterCode); + suffixBuilder.Append("."); + suffixBuilder.Append(IsoLanguages.Get(language).TwoLetterCode); } - return extensionBuilder.ToString(); + return suffixBuilder.ToString(); } } } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 7c8b4e2b7..f73095586 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -249,6 +249,7 @@ +