upgrade service only tries to delete existing file if it actually exists.

pull/25/head
kay.one 12 years ago
parent c3b25878d4
commit c57fb29db4

@ -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<EpisodeFile>
.CreateNew()
.Build();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
}
private void GivenSingleEpisodeWithSingleEpisodeFile()
@ -115,5 +121,34 @@ namespace NzbDrone.Core.Test.MediaFileTests
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(It.IsAny<EpisodeFile>(), true), Times.Once());
}
[Test]
public void should_delete_existing_file_fromdb_if_file_doesnt_exist()
{
GivenSingleEpisodeWithSingleEpisodeFile();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(false);
Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
Mocker.GetMock<IMediaFileService>().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<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(false);
Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>()), Times.Never());
}
}
}

@ -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))

@ -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);
if (_diskProvider.FileExists(file.Path))
{
_logger.Trace("Removing existing episode file: {0}", file);
_recycleBinProvider.DeleteFile(file.Path);
}
_mediaFileService.Delete(file, true);
}

Loading…
Cancel
Save