From 44772c739139306fac9cb443c52ae63bb4b64210 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 12 Aug 2013 19:01:15 -0700 Subject: [PATCH] Throw when unable to move file --- .../RenameEpisodeFileServiceFixture.cs | 4 +++ .../MediaFiles/EpisodeFileMovingService.cs | 7 ++--- .../EpisodeImport/ImportApprovedEpisodes.cs | 7 ----- .../MediaFiles/RenameEpisodeFileService.cs | 27 ++++++++++++------- .../MediaFiles/SameFilenameException.cs | 14 ++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 NzbDrone.Core/MediaFiles/SameFilenameException.cs diff --git a/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs b/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs index 4c76ee406..dbdf35b56 100644 --- a/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/RenameEpisodeFileServiceFixture.cs @@ -80,6 +80,10 @@ namespace NzbDrone.Core.Test.MediaFileTests { GivenEpisodeFiles(); + Mocker.GetMock() + .Setup(s => s.MoveEpisodeFile(It.IsAny(), It.IsAny())) + .Throws(new SameFilenameException("Same file name", "Filename")); + Subject.Execute(new RenameSeriesCommand(_series.Id)); Mocker.GetMock() diff --git a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs index a8dd667be..8eba9e0d4 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs @@ -62,15 +62,12 @@ namespace NzbDrone.Core.MediaFiles { if (!_diskProvider.FileExists(episodeFile.Path)) { - _logger.Error("Episode file path does not exist, {0}", episodeFile.Path); - return null; + throw new FileNotFoundException("Episode file path does not exist", episodeFile.Path); } - //Only rename if existing and new filenames don't match if (DiskProvider.PathEquals(episodeFile.Path, destinationFilename)) { - _logger.Debug("Skipping file rename, source and destination are the same: {0}", episodeFile.Path); - return null; + throw new SameFilenameException("File not moved, source and destination are the same", episodeFile.Path); } _diskProvider.CreateFolder(new FileInfo(destinationFilename).DirectoryName); diff --git a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs index f0c8b05b2..0757b1495 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeImport/ImportApprovedEpisodes.cs @@ -68,13 +68,6 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport if (newDownload) { episodeFile = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode); - - if (episodeFile == null) - { - _logger.Error("Failed to move [{0}], aborting processing", localEpisode); - continue; - } - _messageAggregator.PublishEvent(new EpisodeImportedEvent(episodeFile)); } diff --git a/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs b/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs index 514d34402..f6737a9ef 100644 --- a/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs +++ b/NzbDrone.Core/MediaFiles/RenameEpisodeFileService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using NLog; using NzbDrone.Common.Messaging; @@ -35,18 +36,26 @@ namespace NzbDrone.Core.MediaFiles foreach (var file in episodeFiles) { - var episodeFile = file; - - _logger.Trace("Renaming episode file: {0}", episodeFile); - episodeFile = _episodeFileMover.MoveEpisodeFile(episodeFile, series); - - if (episodeFile != null) + try { + var episodeFile = file; + + _logger.Trace("Renaming episode file: {0}", episodeFile); + episodeFile = _episodeFileMover.MoveEpisodeFile(episodeFile, series); + _mediaFileService.Update(episodeFile); renamed.Add(episodeFile); - } - _logger.Trace("Renamed episode file: {0}", episodeFile); + _logger.Trace("Renamed episode file: {0}", episodeFile); + } + catch (SameFilenameException ex) + { + _logger.Trace("File not renamed, source and destination are the same: {0}", ex.Filename); + } + catch (Exception ex) + { + _logger.ErrorException("Failed to rename file: " + file.Path, ex); + } } if (renamed.Any()) diff --git a/NzbDrone.Core/MediaFiles/SameFilenameException.cs b/NzbDrone.Core/MediaFiles/SameFilenameException.cs new file mode 100644 index 000000000..d80fe4ba1 --- /dev/null +++ b/NzbDrone.Core/MediaFiles/SameFilenameException.cs @@ -0,0 +1,14 @@ +using System; + +namespace NzbDrone.Core.MediaFiles +{ + public class SameFilenameException : Exception + { + public String Filename { get; set; } + + public SameFilenameException(string message, string filename) : base(message) + { + Filename = filename; + } + } +} diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 17bfa17b7..b3302845d 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -234,6 +234,7 @@ +