Fixed: Not deleting episode files during upgrade when root folder is missing

This commit is contained in:
Mark McDowall 2017-10-22 10:09:53 -07:00
parent a9e1aee295
commit 12065948ca
No known key found for this signature in database
GPG Key ID: D4CEFA9A718052E0
2 changed files with 37 additions and 8 deletions

View File

@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
@ -7,6 +7,7 @@ using Moq;
using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
@ -33,12 +34,16 @@ namespace NzbDrone.Core.Test.MediaFiles
.Build();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
.Setup(c => c.FolderExists(Directory.GetParent(_localEpisode.Series.Path).FullName))
.Returns(true);
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetParentFolder(It.IsAny<string>()))
.Returns<string>(c => Path.GetDirectoryName(c));
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetParentFolder(It.IsAny<string>()))
.Returns<string>(c => Path.GetDirectoryName(c));
}
private void GivenSingleEpisodeWithSingleEpisodeFile()
@ -175,5 +180,19 @@ namespace NzbDrone.Core.Test.MediaFiles
Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode).OldFiles.Count.Should().Be(2);
}
[Test]
public void should_throw_if_there_are_existing_episode_files_and_the_root_folder_is_missing()
{
GivenSingleEpisodeWithSingleEpisodeFile();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FolderExists(Directory.GetParent(_localEpisode.Series.Path).FullName))
.Returns(false);
Assert.Throws<RootFolderNotFoundException>(() => Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode));
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, DeleteMediaFileReason.Upgrade), Times.Never());
}
}
}

View File

@ -1,8 +1,9 @@
using System.IO;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles
@ -39,13 +40,22 @@ namespace NzbDrone.Core.MediaFiles
var existingFiles = localEpisode.Episodes
.Where(e => e.EpisodeFileId > 0)
.Select(e => e.EpisodeFile.Value)
.GroupBy(e => e.Id);
.GroupBy(e => e.Id)
.ToList();
var rootFolder = _diskProvider.GetParentFolder(localEpisode.Series.Path);
// If there are existing episode files and the root folder is missing, throw, so the old file isn't left behind during the import process.
if (existingFiles.Any() && !_diskProvider.FolderExists(rootFolder))
{
throw new RootFolderNotFoundException($"Root folder '{rootFolder}' was not found.");
}
foreach (var existingFile in existingFiles)
{
var file = existingFile.First();
var episodeFilePath = Path.Combine(localEpisode.Series.Path, file.RelativePath);
var subfolder = _diskProvider.GetParentFolder(localEpisode.Series.Path).GetRelativePath(_diskProvider.GetParentFolder(episodeFilePath));
var subfolder = rootFolder.GetRelativePath(_diskProvider.GetParentFolder(episodeFilePath));
if (_diskProvider.FileExists(episodeFilePath))
{