From 954a79639fc5cf15b758a0e3ec4d7298a7c55e15 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 29 Aug 2013 23:39:41 -0700 Subject: [PATCH] moveepisode is less side-effecty! --- .../ImportApprovedEpisodesFixture.cs | 3 +-- .../RenameEpisodeFileServiceFixture.cs | 3 +-- NzbDrone.Core/History/HistoryService.cs | 3 ++- .../MediaFiles/EpisodeFileMovingService.cs | 27 +++++++------------ .../EpisodeImport/ImportApprovedEpisodes.cs | 6 ++--- .../MediaFiles/UpgradeMediaFileService.cs | 4 +-- 6 files changed, 19 insertions(+), 27 deletions(-) diff --git a/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs b/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs index 9e34a5882..a7d13ff0c 100644 --- a/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/ImportApprovedEpisodesFixture.cs @@ -53,8 +53,7 @@ namespace NzbDrone.Core.Test.MediaFileTests } Mocker.GetMock() - .Setup(s => s.UpgradeEpisodeFile(It.IsAny(), It.IsAny())) - .Returns(new EpisodeFile()); + .Setup(s => s.UpgradeEpisodeFile(It.IsAny(), It.IsAny())); } [Test] diff --git a/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs b/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs index dbdf35b56..80b4dc855 100644 --- a/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs @@ -60,8 +60,7 @@ namespace NzbDrone.Core.Test.MediaFileTests private void GivenMovedFiles() { Mocker.GetMock() - .Setup(s => s.MoveEpisodeFile(It.IsAny(), _series)) - .Returns(_episodeFiles.First()); + .Setup(s => s.MoveEpisodeFile(It.IsAny(), _series)); } [Test] diff --git a/NzbDrone.Core/History/HistoryService.cs b/NzbDrone.Core/History/HistoryService.cs index 349d81c08..c0752eb2a 100644 --- a/NzbDrone.Core/History/HistoryService.cs +++ b/NzbDrone.Core/History/HistoryService.cs @@ -93,7 +93,8 @@ namespace NzbDrone.Core.History EpisodeId = episode.Id }; - history.Data.Add("FileId", message.ImportedEpisode.Id.ToString()); + //Won't have a value since we publish this event before saving to DB. + //history.Data.Add("FileId", message.ImportedEpisode.Id.ToString()); history.Data.Add("DroppedPath", message.DroppedEpisode.Path); history.Data.Add("ImportedPath", message.ImportedEpisode.Path); diff --git a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs index e46bf3b26..e7a03d22a 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs @@ -13,8 +13,8 @@ namespace NzbDrone.Core.MediaFiles { public interface IMoveEpisodeFiles { - EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series); - EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); + string MoveEpisodeFile(EpisodeFile episodeFile, Series series); + string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); } public class MoveEpisodeFiles : IMoveEpisodeFiles @@ -38,27 +38,24 @@ namespace NzbDrone.Core.MediaFiles _logger = logger; } - public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series) + public string MoveEpisodeFile(EpisodeFile episodeFile, Series series) { 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)); + var filePath = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path)); - return MoveFile(episodeFile, destinationFilename); + return filePath; } - public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode) + public string MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode) { var newFileName = _buildFileNames.BuildFilename(localEpisode.Episodes, localEpisode.Series, episodeFile); - var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path)); - episodeFile = MoveFile(episodeFile, destinationFilename); - - _messageAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode)); - - return episodeFile; + var filePath = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path)); + MoveFile(episodeFile, filePath); + return filePath; } - private EpisodeFile MoveFile(EpisodeFile episodeFile, string destinationFilename) + private void MoveFile(EpisodeFile episodeFile, string destinationFilename) { if (!_diskProvider.FileExists(episodeFile.Path)) { @@ -85,10 +82,6 @@ namespace NzbDrone.Core.MediaFiles _logger.Debug("Unable to apply folder permissions to: ", destinationFilename); _logger.TraceException(ex.Message, ex); } - - episodeFile.Path = destinationFilename; - - return episodeFile; } } } \ No newline at end of file diff --git a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs index 539bb8ccd..087f29e68 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport { List Import(List decisions, bool newDownloads = false); } - + public class ImportApprovedEpisodes : IImportApprovedEpisodes { private readonly IUpgradeMediaFiles _episodeFileUpgrader; @@ -68,10 +68,10 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport if (newDownload) { - episodeFile = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode); + episodeFile.Path = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode); _messageAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile)); + _messageAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode)); } - _mediaFileService.Add(episodeFile); imported.Add(importDecision); diff --git a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index c272e9197..75ee6869e 100644 --- a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -7,7 +7,7 @@ namespace NzbDrone.Core.MediaFiles { public interface IUpgradeMediaFiles { - EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); + string UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode); } public class UpgradeMediaFileService : IUpgradeMediaFiles @@ -31,7 +31,7 @@ namespace NzbDrone.Core.MediaFiles _logger = logger; } - public EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode) + public string UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode) { var existingFiles = localEpisode.Episodes .Where(e => e.EpisodeFileId > 0)