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

Fixes #4066

Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>
pull/4088/head
Qstick 4 years ago
parent 16c03444ab
commit 86b8dd4856

@ -1,9 +1,11 @@
using System.IO;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.MovieImport;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
@ -22,33 +24,41 @@ namespace NzbDrone.Core.Test.MediaFiles
_localMovie = new LocalMovie();
_localMovie.Movie = new Movie
{
Path = @"C:\Test\TV\Series".AsOsAgnostic()
Path = @"C:\Test\Movies\Movie".AsOsAgnostic()
};
_movieFile = Builder<MovieFile>
.CreateNew()
.Build();
.CreateNew()
.Build();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
.Returns(true);
.Setup(c => c.FolderExists(Directory.GetParent(_localMovie.Movie.Path).FullName))
.Returns(true);
Mocker.GetMock<IDiskProvider>()
.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()
private void GivenSingleMovieWithSingleMovieFile()
{
_localMovie.Movie.MovieFileId = 1;
_localMovie.Movie.MovieFile =
new MovieFile
{
Id = 1,
RelativePath = @"Season 01\30.rock.s01e01.avi",
RelativePath = @"A.Movie.2019.avi",
};
}
[Test]
public void should_delete_single_episode_file_once()
public void should_delete_single_movie_file_once()
{
GivenSingleEpisodeWithSingleEpisodeFile();
GivenSingleMovieWithSingleMovieFile();
Subject.UpgradeMovieFile(_movieFile, _localMovie);
@ -56,9 +66,9 @@ namespace NzbDrone.Core.Test.MediaFiles
}
[Test]
public void should_delete_episode_file_from_database()
public void should_delete_movie_file_from_database()
{
GivenSingleEpisodeWithSingleEpisodeFile();
GivenSingleMovieWithSingleMovieFile();
Subject.UpgradeMovieFile(_movieFile, _localMovie);
@ -68,7 +78,7 @@ namespace NzbDrone.Core.Test.MediaFiles
[Test]
public void should_delete_existing_file_fromdb_if_file_doesnt_exist()
{
GivenSingleEpisodeWithSingleEpisodeFile();
GivenSingleMovieWithSingleMovieFile();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
@ -82,7 +92,7 @@ namespace NzbDrone.Core.Test.MediaFiles
[Test]
public void should_not_try_to_recyclebin_existing_file_if_file_doesnt_exist()
{
GivenSingleEpisodeWithSingleEpisodeFile();
GivenSingleMovieWithSingleMovieFile();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FileExists(It.IsAny<string>()))
@ -94,11 +104,25 @@ namespace NzbDrone.Core.Test.MediaFiles
}
[Test]
public void should_return_old_episode_file_in_oldFiles()
public void should_return_old_movie_file_in_oldFiles()
{
GivenSingleEpisodeWithSingleEpisodeFile();
GivenSingleMovieWithSingleMovieFile();
Subject.UpgradeMovieFile(_movieFile, _localMovie).OldFiles.Count.Should().Be(1);
}
[Test]
public void should_throw_if_there_are_existing_movie_files_and_the_root_folder_is_missing()
{
GivenSingleMovieWithSingleMovieFile();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.FolderExists(Directory.GetParent(_localMovie.Movie.Path).FullName))
.Returns(false);
Assert.Throws<RootFolderNotFoundException>(() => Subject.UpgradeMovieFile(_movieFile, _localMovie));
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_localMovie.Movie.MovieFile, DeleteMediaFileReason.Upgrade), Times.Never());
}
}
}

@ -1,5 +1,6 @@
using System.Linq;
using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Events;
@ -7,6 +8,8 @@ namespace NzbDrone.Core.HealthCheck.Checks
{
[CheckOn(typeof(MovieDeletedEvent))]
[CheckOn(typeof(MovieMovedEvent))]
[CheckOn(typeof(MoviesImportedEvent), CheckOnCondition.FailedOnly)]
[CheckOn(typeof(MovieImportFailedEvent), CheckOnCondition.SuccessfulOnly)]
public class RootFolderCheck : HealthCheckBase
{
private readonly IMovieService _movieService;

@ -0,0 +1,29 @@
using System;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Download;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles.Events
{
public class MovieImportFailedEvent : IEvent
{
public Exception Exception { get; set; }
public LocalMovie MovieInfo { get; }
public bool NewDownload { get; }
public string DownloadClient { get; }
public string DownloadId { get; }
public MovieImportFailedEvent(Exception exception, LocalMovie movieInfo, bool newDownload, DownloadClientItem downloadClientItem)
{
Exception = exception;
MovieInfo = movieInfo;
NewDownload = newDownload;
if (downloadClientItem != null)
{
DownloadClient = downloadClientItem.DownloadClient;
DownloadId = downloadClientItem.DownloadId;
}
}
}
}

@ -7,6 +7,7 @@ using NzbDrone.Common.EnsureThat;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.Events;
using NzbDrone.Core.MediaFiles.MovieImport;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Organizer;
@ -188,7 +189,7 @@ namespace NzbDrone.Core.MediaFiles
if (!_diskProvider.FolderExists(rootFolder))
{
throw new DirectoryNotFoundException(string.Format("Root folder '{0}' was not found.", rootFolder));
throw new RootFolderNotFoundException(string.Format("Root folder '{0}' was not found.", rootFolder));
}
var changed = false;

@ -134,6 +134,18 @@ namespace NzbDrone.Core.MediaFiles.MovieImport
_eventAggregator.PublishEvent(new MovieDownloadedEvent(localMovie, movieFile, oldFiles, downloadClientItem));
}
}
catch (RootFolderNotFoundException e)
{
_logger.Warn(e, "Couldn't import movie " + localMovie);
_eventAggregator.PublishEvent(new MovieImportFailedEvent(e, localMovie, newDownload, downloadClientItem));
importResults.Add(new ImportResult(importDecision, "Failed to import movie, Root folder missing."));
}
catch (DestinationAlreadyExistsException e)
{
_logger.Warn(e, "Couldn't import movie " + localMovie);
importResults.Add(new ImportResult(importDecision, "Failed to import movie, Destination already exists."));
}
catch (Exception e)
{
_logger.Warn(e, "Couldn't import movie " + localMovie);

@ -0,0 +1,28 @@
using System;
using System.IO;
using System.Runtime.Serialization;
namespace NzbDrone.Core.MediaFiles.MovieImport
{
public class RootFolderNotFoundException : DirectoryNotFoundException
{
public RootFolderNotFoundException()
{
}
public RootFolderNotFoundException(string message)
: base(message)
{
}
public RootFolderNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}
protected RootFolderNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}

@ -1,6 +1,7 @@
using System.IO;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Core.MediaFiles.MovieImport;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles
@ -41,6 +42,14 @@ namespace NzbDrone.Core.MediaFiles
var existingFile = localMovie.Movie.MovieFile;
var rootFolder = _diskProvider.GetParentFolder(localMovie.Movie.Path);
// If there are existing movie files and the root folder is missing, throw, so the old file isn't left behind during the import process.
if (existingFile != null && !_diskProvider.FolderExists(rootFolder))
{
throw new RootFolderNotFoundException($"Root folder '{rootFolder}' was not found.");
}
if (existingFile != null)
{
var movieFilePath = Path.Combine(localMovie.Movie.Path, existingFile.RelativePath);

Loading…
Cancel
Save