parent
b50fdd637b
commit
5b6a6cc9dc
@ -0,0 +1,143 @@
|
||||
using System.IO;
|
||||
using FizzWare.NBuilder;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.Movies;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Test.Common;
|
||||
|
||||
namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService
|
||||
{
|
||||
[TestFixture]
|
||||
public class DeleteMovieFileFixture : CoreTest<Core.MediaFiles.MediaFileDeletionService>
|
||||
{
|
||||
private static readonly string RootFolder = @"C:\Test\Movies";
|
||||
private Movie _movie;
|
||||
private MovieFile _movieFile;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_movie = Builder<Movie>.CreateNew()
|
||||
.With(s => s.Path = Path.Combine(RootFolder, "Movie Title"))
|
||||
.Build();
|
||||
|
||||
_movieFile = Builder<MovieFile>.CreateNew()
|
||||
.With(f => f.RelativePath = "Some SubFolder")
|
||||
.With(f => f.Path = Path.Combine(_movie.Path, "Some SubFolder"))
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.GetParentFolder(_movie.Path))
|
||||
.Returns(RootFolder);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.GetParentFolder(_movieFile.Path))
|
||||
.Returns(_movie.Path);
|
||||
}
|
||||
|
||||
private void GivenRootFolderExists()
|
||||
{
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.FolderExists(RootFolder))
|
||||
.Returns(true);
|
||||
}
|
||||
|
||||
private void GivenRootFolderHasFolders()
|
||||
{
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.GetDirectories(RootFolder))
|
||||
.Returns(new[] { _movie.Path });
|
||||
}
|
||||
|
||||
private void GivenMovieFolderExists()
|
||||
{
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.FolderExists(_movie.Path))
|
||||
.Returns(true);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_throw_if_root_folder_does_not_exist()
|
||||
{
|
||||
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteMovieFile(_movie, _movieFile));
|
||||
ExceptionVerification.ExpectedWarns(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_should_throw_if_root_folder_is_empty()
|
||||
{
|
||||
GivenRootFolderExists();
|
||||
|
||||
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteMovieFile(_movie, _movieFile));
|
||||
ExceptionVerification.ExpectedWarns(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_delete_from_db_if_movie_folder_does_not_exist()
|
||||
{
|
||||
GivenRootFolderExists();
|
||||
GivenRootFolderHasFolders();
|
||||
|
||||
Subject.DeleteMovieFile(_movie, _movieFile);
|
||||
|
||||
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_movieFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_movieFile.Path, It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_delete_from_db_if_movie_file_does_not_exist()
|
||||
{
|
||||
GivenRootFolderExists();
|
||||
GivenRootFolderHasFolders();
|
||||
GivenMovieFolderExists();
|
||||
|
||||
Subject.DeleteMovieFile(_movie, _movieFile);
|
||||
|
||||
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_movieFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_movieFile.Path, It.IsAny<string>()), Times.Never());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_delete_from_disk_and_db_if_movie_file_exists()
|
||||
{
|
||||
GivenRootFolderExists();
|
||||
GivenRootFolderHasFolders();
|
||||
GivenMovieFolderExists();
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.FileExists(_movieFile.Path))
|
||||
.Returns(true);
|
||||
|
||||
Subject.DeleteMovieFile(_movie, _movieFile);
|
||||
|
||||
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_movieFile.Path, "Movie Title"), Times.Once());
|
||||
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_movieFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_handle_error_deleting_movie_file()
|
||||
{
|
||||
GivenRootFolderExists();
|
||||
GivenRootFolderHasFolders();
|
||||
GivenMovieFolderExists();
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.FileExists(_movieFile.Path))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IRecycleBinProvider>()
|
||||
.Setup(s => s.DeleteFile(_movieFile.Path, "Movie Title"))
|
||||
.Throws(new IOException());
|
||||
|
||||
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteMovieFile(_movie, _movieFile));
|
||||
|
||||
ExceptionVerification.ExpectedErrors(1);
|
||||
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_movieFile.Path, "Movie Title"), Times.Once());
|
||||
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_movieFile, DeleteMediaFileReason.Manual), Times.Never());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Disk;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Exceptions;
|
||||
using NzbDrone.Core.MediaFiles.Events;
|
||||
using NzbDrone.Core.Messaging;
|
||||
using NzbDrone.Core.Messaging.Events;
|
||||
using NzbDrone.Core.Movies;
|
||||
using NzbDrone.Core.Movies.Events;
|
||||
|
||||
namespace NzbDrone.Core.MediaFiles
|
||||
{
|
||||
public interface IDeleteMediaFiles
|
||||
{
|
||||
void DeleteMovieFile(Movie movie, MovieFile movieFile);
|
||||
}
|
||||
|
||||
public class MediaFileDeletionService : IDeleteMediaFiles,
|
||||
IHandleAsync<MovieDeletedEvent>,
|
||||
IHandle<MovieFileDeletedEvent>
|
||||
{
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IRecycleBinProvider _recycleBinProvider;
|
||||
private readonly IMediaFileService _mediaFileService;
|
||||
private readonly IMovieService _movieService;
|
||||
private readonly IConfigService _configService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public MediaFileDeletionService(IDiskProvider diskProvider,
|
||||
IRecycleBinProvider recycleBinProvider,
|
||||
IMediaFileService mediaFileService,
|
||||
IMovieService movieService,
|
||||
IConfigService configService,
|
||||
Logger logger)
|
||||
{
|
||||
_diskProvider = diskProvider;
|
||||
_recycleBinProvider = recycleBinProvider;
|
||||
_mediaFileService = mediaFileService;
|
||||
_movieService = movieService;
|
||||
_configService = configService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void DeleteMovieFile(Movie movie, MovieFile movieFile)
|
||||
{
|
||||
var fullPath = Path.Combine(movie.Path, movieFile.RelativePath);
|
||||
var rootFolder = _diskProvider.GetParentFolder(movie.Path);
|
||||
|
||||
if (!_diskProvider.FolderExists(rootFolder))
|
||||
{
|
||||
_logger.Warn("Movie's root folder ({0}) doesn't exist.", rootFolder);
|
||||
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Movie's root folder ({0}) doesn't exist.", rootFolder);
|
||||
}
|
||||
|
||||
if (_diskProvider.GetDirectories(rootFolder).Empty())
|
||||
{
|
||||
_logger.Warn("Movie's root folder ({0}) is empty.", rootFolder);
|
||||
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Movie's root folder ({0}) is empty.", rootFolder);
|
||||
}
|
||||
|
||||
if (_diskProvider.FolderExists(movie.Path) && _diskProvider.FileExists(fullPath))
|
||||
{
|
||||
_logger.Info("Deleting movie file: {0}", fullPath);
|
||||
|
||||
var subfolder = _diskProvider.GetParentFolder(movie.Path).GetRelativePath(_diskProvider.GetParentFolder(fullPath));
|
||||
|
||||
try
|
||||
{
|
||||
_recycleBinProvider.DeleteFile(fullPath, subfolder);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Unable to delete movie file");
|
||||
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete movie file");
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the movie file from the database to clean it up even if the file was already deleted
|
||||
_mediaFileService.Delete(movieFile, DeleteMediaFileReason.Manual);
|
||||
}
|
||||
|
||||
public void HandleAsync(MovieDeletedEvent message)
|
||||
{
|
||||
if (message.DeleteFiles)
|
||||
{
|
||||
var movie = message.Movie;
|
||||
var allMovies = _movieService.GetAllMovies();
|
||||
|
||||
foreach (var s in allMovies)
|
||||
{
|
||||
if (s.Id == movie.Id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (movie.Path.IsParentPath(s.Path))
|
||||
{
|
||||
_logger.Error("Movie path: '{0}' is a parent of another movie, not deleting files.", movie.Path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (movie.Path.PathEquals(s.Path))
|
||||
{
|
||||
_logger.Error("Movie path: '{0}' is the same as another movie, not deleting files.", movie.Path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (_diskProvider.FolderExists(message.Movie.Path))
|
||||
{
|
||||
_recycleBinProvider.DeleteFolder(message.Movie.Path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[EventHandleOrder(EventHandleOrder.Last)]
|
||||
public void Handle(MovieFileDeletedEvent message)
|
||||
{
|
||||
if (message.Reason == DeleteMediaFileReason.Upgrade)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_configService.DeleteEmptyFolders)
|
||||
{
|
||||
var movie = message.MovieFile.Movie;
|
||||
var movieFileFolder = message.MovieFile.Path.GetParentPath();
|
||||
|
||||
if (_diskProvider.GetFiles(movie.Path, SearchOption.AllDirectories).Empty())
|
||||
{
|
||||
_diskProvider.DeleteFolder(movie.Path, true);
|
||||
}
|
||||
else if (_diskProvider.GetFiles(movieFileFolder, SearchOption.AllDirectories).Empty())
|
||||
{
|
||||
_diskProvider.RemoveEmptySubfolders(movieFileFolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue