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

pull/2164/head
Mark McDowall 7 years ago
parent a9e1aee295
commit 12065948ca
No known key found for this signature in database
GPG Key ID: D4CEFA9A718052E0

@ -1,4 +1,4 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
@ -7,6 +7,7 @@ using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -33,12 +34,16 @@ namespace NzbDrone.Core.Test.MediaFiles
.Build(); .Build();
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>())) .Setup(c => c.FolderExists(Directory.GetParent(_localEpisode.Series.Path).FullName))
.Returns(true); .Returns(true);
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(c => c.GetParentFolder(It.IsAny<string>())) .Setup(c => c.GetParentFolder(It.IsAny<string>()))
.Returns<string>(c => Path.GetDirectoryName(c)); .Returns<string>(c => Path.GetDirectoryName(c));
} }
private void GivenSingleEpisodeWithSingleEpisodeFile() private void GivenSingleEpisodeWithSingleEpisodeFile()
@ -175,5 +180,19 @@ namespace NzbDrone.Core.Test.MediaFiles
Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode).OldFiles.Count.Should().Be(2); 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());
}
} }
} }

@ -1,8 +1,9 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaFiles.EpisodeImport;
using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles namespace NzbDrone.Core.MediaFiles
@ -39,13 +40,22 @@ namespace NzbDrone.Core.MediaFiles
var existingFiles = localEpisode.Episodes var existingFiles = localEpisode.Episodes
.Where(e => e.EpisodeFileId > 0) .Where(e => e.EpisodeFileId > 0)
.Select(e => e.EpisodeFile.Value) .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) foreach (var existingFile in existingFiles)
{ {
var file = existingFile.First(); var file = existingFile.First();
var episodeFilePath = Path.Combine(localEpisode.Series.Path, file.RelativePath); 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)) if (_diskProvider.FileExists(episodeFilePath))
{ {

Loading…
Cancel
Save