parent
d7ef6cc88b
commit
3383cc1744
@ -0,0 +1,140 @@
|
|||||||
|
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.Test.Framework;
|
||||||
|
using NzbDrone.Core.Music;
|
||||||
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.MediaFiles.MediaFileDeletionService
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class DeleteTrackFileFixture : CoreTest<Core.MediaFiles.MediaFileDeletionService>
|
||||||
|
{
|
||||||
|
private static readonly string RootFolder = @"C:\Test\Music";
|
||||||
|
private Artist _artist;
|
||||||
|
private TrackFile _trackFile;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
_artist = Builder<Artist>.CreateNew()
|
||||||
|
.With(s => s.Path = Path.Combine(RootFolder, "Artist Name"))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
_trackFile = Builder<TrackFile>.CreateNew()
|
||||||
|
.With(f => f.RelativePath = "Artist Name - Track01")
|
||||||
|
.With(f => f.Path = Path.Combine(_artist.Path, "Artist Name - Track01"))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Mocker.GetMock<IDiskProvider>()
|
||||||
|
.Setup(s => s.GetParentFolder(_artist.Path))
|
||||||
|
.Returns(RootFolder);
|
||||||
|
|
||||||
|
Mocker.GetMock<IDiskProvider>()
|
||||||
|
.Setup(s => s.GetParentFolder(_trackFile.Path))
|
||||||
|
.Returns(_artist.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[] { _artist.Path });
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GivenSeriesFolderExists()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IDiskProvider>()
|
||||||
|
.Setup(s => s.FolderExists(_artist.Path))
|
||||||
|
.Returns(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_throw_if_root_folder_does_not_exist()
|
||||||
|
{
|
||||||
|
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteTrackFile(_artist, _trackFile));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_should_throw_if_root_folder_is_empty()
|
||||||
|
{
|
||||||
|
GivenRootFolderExists();
|
||||||
|
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteTrackFile(_artist, _trackFile));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_delete_from_db_if_artist_folder_does_not_exist()
|
||||||
|
{
|
||||||
|
GivenRootFolderExists();
|
||||||
|
GivenRootFolderHasFolders();
|
||||||
|
|
||||||
|
Subject.DeleteTrackFile(_artist, _trackFile);
|
||||||
|
|
||||||
|
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_trackFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||||
|
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_trackFile.Path, It.IsAny<string>()), Times.Never());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_delete_from_db_if_track_file_does_not_exist()
|
||||||
|
{
|
||||||
|
GivenRootFolderExists();
|
||||||
|
GivenRootFolderHasFolders();
|
||||||
|
GivenSeriesFolderExists();
|
||||||
|
|
||||||
|
Subject.DeleteTrackFile(_artist, _trackFile);
|
||||||
|
|
||||||
|
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_trackFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||||
|
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_trackFile.Path, It.IsAny<string>()), Times.Never());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_delete_from_disk_and_db_if_track_file_exists()
|
||||||
|
{
|
||||||
|
GivenRootFolderExists();
|
||||||
|
GivenRootFolderHasFolders();
|
||||||
|
GivenSeriesFolderExists();
|
||||||
|
|
||||||
|
Mocker.GetMock<IDiskProvider>()
|
||||||
|
.Setup(s => s.FileExists(_trackFile.Path))
|
||||||
|
.Returns(true);
|
||||||
|
|
||||||
|
Subject.DeleteTrackFile(_artist, _trackFile);
|
||||||
|
|
||||||
|
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_trackFile.Path, "Series Title"), Times.Once());
|
||||||
|
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_trackFile, DeleteMediaFileReason.Manual), Times.Once());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_handle_error_deleting_track_file()
|
||||||
|
{
|
||||||
|
GivenRootFolderExists();
|
||||||
|
GivenRootFolderHasFolders();
|
||||||
|
GivenSeriesFolderExists();
|
||||||
|
|
||||||
|
Mocker.GetMock<IDiskProvider>()
|
||||||
|
.Setup(s => s.FileExists(_trackFile.Path))
|
||||||
|
.Returns(true);
|
||||||
|
|
||||||
|
Mocker.GetMock<IRecycleBinProvider>()
|
||||||
|
.Setup(s => s.DeleteFile(_trackFile.Path, "Artist Name"))
|
||||||
|
.Throws(new IOException());
|
||||||
|
|
||||||
|
Assert.Throws<NzbDroneClientException>(() => Subject.DeleteTrackFile(_artist, _trackFile));
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(_trackFile.Path, "Artist Name"), Times.Once());
|
||||||
|
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_trackFile, DeleteMediaFileReason.Manual), Times.Never());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Disk;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Core.Exceptions;
|
||||||
|
using NzbDrone.Core.Messaging.Events;
|
||||||
|
using NzbDrone.Core.Music;
|
||||||
|
using NzbDrone.Core.Music.Events;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.MediaFiles
|
||||||
|
{
|
||||||
|
public interface IDeleteMediaFiles
|
||||||
|
{
|
||||||
|
void DeleteTrackFile(Artist artist, TrackFile trackFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MediaFileDeletionService : IDeleteMediaFiles, IHandleAsync<ArtistDeletedEvent>
|
||||||
|
{
|
||||||
|
private readonly IDiskProvider _diskProvider;
|
||||||
|
private readonly IRecycleBinProvider _recycleBinProvider;
|
||||||
|
private readonly IMediaFileService _mediaFileService;
|
||||||
|
private readonly Logger _logger;
|
||||||
|
|
||||||
|
public MediaFileDeletionService(IDiskProvider diskProvider,
|
||||||
|
IRecycleBinProvider recycleBinProvider,
|
||||||
|
IMediaFileService mediaFileService,
|
||||||
|
Logger logger)
|
||||||
|
{
|
||||||
|
_diskProvider = diskProvider;
|
||||||
|
_recycleBinProvider = recycleBinProvider;
|
||||||
|
_mediaFileService = mediaFileService;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteTrackFile(Artist artist, TrackFile trackFile)
|
||||||
|
{
|
||||||
|
var fullPath = Path.Combine(artist.Path, trackFile.RelativePath);
|
||||||
|
var rootFolder = _diskProvider.GetParentFolder(artist.Path);
|
||||||
|
|
||||||
|
if (!_diskProvider.FolderExists(rootFolder))
|
||||||
|
{
|
||||||
|
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Artist's root folder ({0}) doesn't exist.", rootFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_diskProvider.GetDirectories(rootFolder).Empty())
|
||||||
|
{
|
||||||
|
throw new NzbDroneClientException(HttpStatusCode.Conflict, "Artist's root folder ({0}) is empty.", rootFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_diskProvider.FolderExists(artist.Path) && _diskProvider.FileExists(fullPath))
|
||||||
|
{
|
||||||
|
_logger.Info("Deleting track file: {0}", fullPath);
|
||||||
|
|
||||||
|
var subfolder = _diskProvider.GetParentFolder(artist.Path).GetRelativePath(_diskProvider.GetParentFolder(fullPath));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_recycleBinProvider.DeleteFile(fullPath, subfolder);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Error(e, "Unable to delete track file");
|
||||||
|
throw new NzbDroneClientException(HttpStatusCode.InternalServerError, "Unable to delete track file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the track file from the database to clean it up even if the file was already deleted
|
||||||
|
_mediaFileService.Delete(trackFile, DeleteMediaFileReason.Manual);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void HandleAsync(ArtistDeletedEvent message)
|
||||||
|
{
|
||||||
|
if (message.DeleteFiles)
|
||||||
|
{
|
||||||
|
if (_diskProvider.FolderExists(message.Artist.Path))
|
||||||
|
{
|
||||||
|
_recycleBinProvider.DeleteFolder(message.Artist.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue