From e6b9cd7e60339f89b4aeb788d2d535644d890bad Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 7 Aug 2013 22:40:31 -0700 Subject: [PATCH] Episode files are moved to final folder without renaming before deletion and final renaming --- .../MediaFileTests/EpisodeFileMoverFixture.cs | 166 ------------------ .../UpgradeMediaFileServiceFixture.cs | 31 +++- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 - .../MediaFiles/EpisodeFileMovingService.cs | 36 ++-- .../MediaFiles/UpgradeMediaFileService.cs | 2 +- 5 files changed, 40 insertions(+), 196 deletions(-) delete mode 100644 NzbDrone.Core.Test/MediaFileTests/EpisodeFileMoverFixture.cs diff --git a/NzbDrone.Core.Test/MediaFileTests/EpisodeFileMoverFixture.cs b/NzbDrone.Core.Test/MediaFileTests/EpisodeFileMoverFixture.cs deleted file mode 100644 index 3101de684..000000000 --- a/NzbDrone.Core.Test/MediaFileTests/EpisodeFileMoverFixture.cs +++ /dev/null @@ -1,166 +0,0 @@ -using System.IO; -using System.Linq; -using FizzWare.NBuilder; -using FluentAssertions; -using Moq; -using NUnit.Framework; -using NzbDrone.Common; -using NzbDrone.Core.MediaFiles; -using NzbDrone.Core.Organizer; -using NzbDrone.Core.Qualities; -using NzbDrone.Core.Test.Framework; -using NzbDrone.Core.Tv; -using NzbDrone.Test.Common; - -namespace NzbDrone.Core.Test.MediaFileTests -{ - public class EpisodeFileMoverFixture : CoreTest - { - [Test] - public void should_not_move_file_if_source_and_destination_are_the_same_path() - { - var fakeSeries = Builder.CreateNew() - .With(s => s.Id = 5) - .With(s => s.Title = "30 Rock") - .Build(); - - var fakeEpisode = Builder.CreateListOfSize(1) - .All() - .With(e => e.SeriesId = fakeSeries.Id) - .With(e => e.SeasonNumber = 1) - .With(e => e.EpisodeNumber = 1) - .Build().ToList(); - - const string filename = @"30 Rock - S01E01 - TBD"; - var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".avi").AsOsAgnostic(); - - var file = Builder.CreateNew() - .With(f => f.SeriesId = fakeSeries.Id) - .With(f => f.Path = fi) - .Build(); - - Mocker.GetMock() - .Setup(e => e.Get(fakeSeries.Id)) - .Returns(fakeSeries); - - Mocker.GetMock() - .Setup(e => e.GetEpisodesByFileId(file.Id)) - .Returns(fakeEpisode); - - Mocker.GetMock() - .Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny())) - .Returns(filename); - - Mocker.GetMock() - .Setup(e => e.BuildFilePath(It.IsAny(), fakeEpisode.First().SeasonNumber, filename, ".avi")) - .Returns(fi); - - Mocker.GetMock() - .Setup(s => s.FileExists(fi)) - .Returns(true); - - var result = Subject.MoveEpisodeFile(file); - - result.Should().BeNull(); - } - - [Test] - public void should_use_EpisodeFiles_quality() - { - var fakeSeries = Builder.CreateNew() - .With(s => s.Id = 5) - .With(s => s.Title = "30 Rock") - .Build(); - - var fakeEpisode = Builder.CreateListOfSize(1) - .All() - .With(e => e.SeriesId = fakeSeries.Id) - .With(e => e.SeasonNumber = 1) - .With(e => e.EpisodeNumber = 1) - .Build().ToList(); - - const string filename = @"30 Rock - S01E01 - TBD"; - var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".mkv").AsOsAgnostic(); - var currentFilename = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", "30.Rock.S01E01.Test.WED-DL.mkv").AsOsAgnostic(); - const string message = "30 Rock - 1x01 - [WEBDL]"; - - var file = Builder.CreateNew() - .With(f => f.SeriesId = fakeSeries.Id) - .With(f => f.Path = currentFilename) - .With(f => f.Quality = new QualityModel(Quality.WEBDL720p)) - .Build(); - - Mocker.GetMock() - .Setup(e => e.Get(fakeSeries.Id)) - .Returns(fakeSeries); - - Mocker.GetMock() - .Setup(e => e.GetEpisodesByFileId(file.Id)) - .Returns(fakeEpisode); - - Mocker.GetMock() - .Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny())) - .Returns(filename); - - Mocker.GetMock() - .Setup(e => e.BuildFilePath(It.IsAny(), fakeEpisode.First().SeasonNumber, filename, ".mkv")) - .Returns(fi); - - Mocker.GetMock() - .Setup(s => s.FileExists(currentFilename)) - .Returns(true); - - var result = Subject.MoveEpisodeFile(file); - - - } - - [Test] - public void should_log_error_and_return_null_when_source_file_does_not_exists() - { - var fakeSeries = Builder.CreateNew() - .With(s => s.Id = 5) - .With(s => s.Title = "30 Rock") - .Build(); - - var fakeEpisode = Builder.CreateListOfSize(1) - .All() - .With(e => e.SeriesId = fakeSeries.Id) - .With(e => e.SeasonNumber = 1) - .With(e => e.EpisodeNumber = 1) - .Build().ToList(); - - const string filename = @"30 Rock - S01E01 - TBD"; - var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".mkv").AsOsAgnostic(); - var currentFilename = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", "30.Rock.S01E01.Test.WED-DL.mkv").AsOsAgnostic(); - const string message = "30 Rock - 1x01 - [WEBDL]"; - - var file = Builder.CreateNew() - .With(f => f.SeriesId = fakeSeries.Id) - .With(f => f.Path = currentFilename) - .With(f => f.Quality = new QualityModel(Quality.WEBDL720p)) - .Build(); - - Mocker.GetMock() - .Setup(e => e.Get(fakeSeries.Id)) - .Returns(fakeSeries); - - Mocker.GetMock() - .Setup(e => e.GetEpisodesByFileId(file.Id)) - .Returns(fakeEpisode); - - Mocker.GetMock() - .Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny())) - .Returns(filename); - - Mocker.GetMock() - .Setup(e => e.BuildFilePath(It.IsAny(), fakeEpisode.First().SeasonNumber, filename, ".mkv")) - .Returns(fi); - - var result = Subject.MoveEpisodeFile(file); - - result.Should().BeNull(); - ExceptionVerification.ExpectedErrors(1); - } - } -} diff --git a/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs b/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs index 43b24bcc8..f8cfc020e 100644 --- a/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using FizzWare.NBuilder; using Marr.Data; using Moq; @@ -122,7 +123,6 @@ public void should_delete_episode_file_from_database() Mocker.GetMock().Verify(v => v.Delete(It.IsAny(), true), Times.Once()); } - [Test] public void should_delete_existing_file_fromdb_if_file_doesnt_exist() { @@ -138,7 +138,7 @@ public void should_delete_existing_file_fromdb_if_file_doesnt_exist() } [Test] - public void should_not_try_to_recyclebin_existing_file_fromdb_if_file_doesnt_exist() + public void should_not_try_to_recyclebin_existing_file_if_file_doesnt_exist() { GivenSingleEpisodeWithSingleEpisodeFile(); @@ -150,5 +150,30 @@ public void should_not_try_to_recyclebin_existing_file_fromdb_if_file_doesnt_exi Mocker.GetMock().Verify(v => v.DeleteFile(It.IsAny()), Times.Never()); } + + [Test] + public void should_not_delete_file_if_pre_move_fails() + { + GivenSingleEpisodeWithSingleEpisodeFile(); + + Mocker.GetMock() + .Setup(s => s.PreMoveEpisodeFile(It.IsAny(), It.IsAny())) + .Throws(); + + Assert.Throws(() => Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode)); + + Mocker.GetMock().Verify(v => v.DeleteFile(It.IsAny()), Times.Never()); + } + + [Test] + public void should_move_after_premove() + { + GivenSingleEpisodeWithSingleEpisodeFile(); + + Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode); + + Mocker.GetMock().Verify(v => v.PreMoveEpisodeFile(It.IsAny(), It.IsAny()), Times.Once()); + Mocker.GetMock().Verify(v => v.MoveEpisodeFile(It.IsAny(), It.IsAny()), Times.Once()); + } } } diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 63781f0db..569b0ec82 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -138,7 +138,6 @@ - diff --git a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs index 463e0959b..a23670442 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs @@ -13,55 +13,32 @@ namespace NzbDrone.Core.MediaFiles { public interface IMoveEpisodeFiles { - EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile); EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series); EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); + EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); } public class MoveEpisodeFiles : IMoveEpisodeFiles { - private readonly ISeriesRepository _seriesRepository; private readonly IEpisodeService _episodeService; private readonly IBuildFileNames _buildFileNames; - private readonly IMediaFileService _mediaFileService; private readonly IMessageAggregator _messageAggregator; private readonly IDiskProvider _diskProvider; private readonly Logger _logger; - public MoveEpisodeFiles(ISeriesRepository seriesRepository, - IEpisodeService episodeService, + public MoveEpisodeFiles(IEpisodeService episodeService, IBuildFileNames buildFileNames, - IMediaFileService mediaFileService, IMessageAggregator messageAggregator, IDiskProvider diskProvider, Logger logger) { - _seriesRepository = seriesRepository; _episodeService = episodeService; _buildFileNames = buildFileNames; - _mediaFileService = mediaFileService; _messageAggregator = messageAggregator; _diskProvider = diskProvider; _logger = logger; } - public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile) - { - if (episodeFile == null) - throw new ArgumentNullException("episodeFile"); - - var series = _seriesRepository.Get(episodeFile.SeriesId); - var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id); - var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile); - var destinationFilename = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path)); - - episodeFile = MoveFile(episodeFile, destinationFilename); - - _mediaFileService.Update(episodeFile); - - return episodeFile; - } - public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series) { var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id); @@ -82,6 +59,15 @@ public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEp return episodeFile; } + public EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode) + { + var newFileName = Path.GetFileNameWithoutExtension(episodeFile.Path); + var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path)); + episodeFile = MoveFile(episodeFile, destinationFilename); + + return episodeFile; + } + private EpisodeFile MoveFile(EpisodeFile episodeFile, string destinationFilename) { if (!_diskProvider.FileExists(episodeFile.Path)) diff --git a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index d5ff9cb64..480c45457 100644 --- a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -38,7 +38,7 @@ public EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode loca .Select(e => e.EpisodeFile.Value) .GroupBy(e => e.Id); - + episodeFile = _episodeFileMover.PreMoveEpisodeFile(episodeFile, localEpisode); foreach (var existingFile in existingFiles) {