From c57fb29db407dcfdb1c339fad48cc278c54089a1 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 30 Jul 2013 22:11:54 -0700 Subject: [PATCH] upgrade service only tries to delete existing file if it actually exists. --- .../UpgradeMediaFileServiceFixture.cs | 35 +++++++++++++++++++ .../MediaFiles/RecycleBinProvider.cs | 8 ++--- .../MediaFiles/UpgradeMediaFileService.cs | 14 ++++++-- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs b/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs index 321d7de35..43b24bcc8 100644 --- a/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/UpgradeMediaFileServiceFixture.cs @@ -3,6 +3,7 @@ using FizzWare.NBuilder; using Marr.Data; using Moq; using NUnit.Framework; +using NzbDrone.Common; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Test.Framework; @@ -23,6 +24,11 @@ namespace NzbDrone.Core.Test.MediaFileTests _episodeFile = Builder .CreateNew() .Build(); + + + Mocker.GetMock() + .Setup(c => c.FileExists(It.IsAny())) + .Returns(true); } private void GivenSingleEpisodeWithSingleEpisodeFile() @@ -115,5 +121,34 @@ namespace NzbDrone.Core.Test.MediaFileTests Mocker.GetMock().Verify(v => v.Delete(It.IsAny(), true), Times.Once()); } + + + [Test] + public void should_delete_existing_file_fromdb_if_file_doesnt_exist() + { + GivenSingleEpisodeWithSingleEpisodeFile(); + + Mocker.GetMock() + .Setup(c => c.FileExists(It.IsAny())) + .Returns(false); + + Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode); + + Mocker.GetMock().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, true), Times.Once()); + } + + [Test] + public void should_not_try_to_recyclebin_existing_file_fromdb_if_file_doesnt_exist() + { + GivenSingleEpisodeWithSingleEpisodeFile(); + + Mocker.GetMock() + .Setup(c => c.FileExists(It.IsAny())) + .Returns(false); + + Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode); + + Mocker.GetMock().Verify(v => v.DeleteFile(It.IsAny()), Times.Never()); + } } } diff --git a/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs b/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs index 12d354a11..ff3866b39 100644 --- a/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs +++ b/NzbDrone.Core/MediaFiles/RecycleBinProvider.cs @@ -33,14 +33,14 @@ namespace NzbDrone.Core.MediaFiles public void DeleteFolder(string path) { - logger.Trace("Attempting to send '{0}' to recycling bin", path); + logger.Info("Attempting to send '{0}' to recycling bin", path); var recyclingBin = _configService.RecycleBin; if (String.IsNullOrWhiteSpace(recyclingBin)) { logger.Info("Recycling Bin has not been configured, deleting permanently."); _diskProvider.DeleteFolder(path, true); - logger.Trace("Folder has been permanently deleted: {0}", path); + logger.Debug("Folder has been permanently deleted: {0}", path); } else @@ -57,13 +57,13 @@ namespace NzbDrone.Core.MediaFiles _diskProvider.FileSetLastWriteTimeUtc(file, DateTime.UtcNow); } - logger.Trace("Folder has been moved to the recycling bin: {0}", destination); + logger.Debug("Folder has been moved to the recycling bin: {0}", destination); } } public void DeleteFile(string path) { - logger.Trace("Attempting to send '{0}' to recycling bin", path); + logger.Debug("Attempting to send '{0}' to recycling bin", path); var recyclingBin = _configService.RecycleBin; if (String.IsNullOrWhiteSpace(recyclingBin)) diff --git a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs index b4fcf39df..d5ff9cb64 100644 --- a/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs +++ b/NzbDrone.Core/MediaFiles/UpgradeMediaFileService.cs @@ -1,5 +1,6 @@ using System.Linq; using NLog; +using NzbDrone.Common; using NzbDrone.Core.Parser.Model; namespace NzbDrone.Core.MediaFiles @@ -14,16 +15,19 @@ namespace NzbDrone.Core.MediaFiles private readonly IRecycleBinProvider _recycleBinProvider; private readonly IMediaFileService _mediaFileService; private readonly IMoveEpisodeFiles _episodeFileMover; + private readonly IDiskProvider _diskProvider; private readonly Logger _logger; public UpgradeMediaFileService(IRecycleBinProvider recycleBinProvider, IMediaFileService mediaFileService, IMoveEpisodeFiles episodeFileMover, + IDiskProvider diskProvider, Logger logger) { _recycleBinProvider = recycleBinProvider; _mediaFileService = mediaFileService; _episodeFileMover = episodeFileMover; + _diskProvider = diskProvider; _logger = logger; } @@ -34,12 +38,18 @@ namespace NzbDrone.Core.MediaFiles .Select(e => e.EpisodeFile.Value) .GroupBy(e => e.Id); + + foreach (var existingFile in existingFiles) { var file = existingFile.First(); - _logger.Trace("Removing existing episode file: {0}", file); - _recycleBinProvider.DeleteFile(file.Path); + if (_diskProvider.FileExists(file.Path)) + { + _logger.Trace("Removing existing episode file: {0}", file); + _recycleBinProvider.DeleteFile(file.Path); + } + _mediaFileService.Delete(file, true); }