mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-04 10:02:40 +01:00
MediaFile cleanup will unlink removed episode files from episodes
This commit is contained in:
parent
21a3260bf2
commit
bbd0fd41a3
@ -14,22 +14,27 @@ namespace NzbDrone.Core.Test.MediaFileTests
|
||||
{
|
||||
public class MediaFileTableCleanupServiceFixture : CoreTest<MediaFileTableCleanupService>
|
||||
{
|
||||
private const string DeletedPath = "ANY FILE WITH THIS PATH IS CONSIDERED DELETED!";
|
||||
private const string DELETED_PATH = "ANY FILE WITH THIS PATH IS CONSIDERED DELETED!";
|
||||
private List<Episode> _episodes;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_episodes = Builder<Episode>.CreateListOfSize(10)
|
||||
.Build()
|
||||
.ToList();
|
||||
|
||||
Mocker.GetMock<ISeriesService>()
|
||||
.Setup(s => s.GetSeries(It.IsAny<Int32>()))
|
||||
.Returns(Builder<Series>.CreateNew().Build());
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(e => e.FileExists(It.Is<String>(c => c != DeletedPath)))
|
||||
.Setup(e => e.FileExists(It.Is<String>(c => c != DELETED_PATH)))
|
||||
.Returns(true);
|
||||
|
||||
Mocker.GetMock<IEpisodeService>()
|
||||
.Setup(c => c.GetEpisodesByFileId(It.IsAny<int>()))
|
||||
.Returns(new List<Episode> {new Episode()});
|
||||
.Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
|
||||
.Returns(_episodes);
|
||||
|
||||
Mocker.GetMock<IDiskProvider>()
|
||||
.Setup(s => s.IsParent(It.IsAny<String>(), It.IsAny<String>()))
|
||||
@ -45,9 +50,11 @@ private void GivenEpisodeFiles(IEnumerable<EpisodeFile> episodeFiles)
|
||||
|
||||
private void GivenFilesAreNotAttachedToEpisode()
|
||||
{
|
||||
_episodes.ForEach(e => e.EpisodeFileId = 0);
|
||||
|
||||
Mocker.GetMock<IEpisodeService>()
|
||||
.Setup(c => c.GetEpisodesByFileId(It.IsAny<int>()))
|
||||
.Returns(new List<Episode>());
|
||||
.Setup(c => c.GetEpisodeBySeries(It.IsAny<int>()))
|
||||
.Returns(_episodes);
|
||||
}
|
||||
|
||||
private void GivenFileIsNotInSeriesFolder()
|
||||
@ -75,14 +82,14 @@ public void should_delete_none_existing_files()
|
||||
{
|
||||
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10)
|
||||
.Random(2)
|
||||
.With(c => c.Path = DeletedPath)
|
||||
.With(c => c.Path = DELETED_PATH)
|
||||
.Build();
|
||||
|
||||
GivenEpisodeFiles(episodeFiles);
|
||||
|
||||
Subject.Execute(new CleanMediaFileDb(0));
|
||||
|
||||
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.Is<EpisodeFile>(e => e.Path == DeletedPath), false), Times.Exactly(2));
|
||||
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.Is<EpisodeFile>(e => e.Path == DELETED_PATH), false), Times.Exactly(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -116,5 +123,30 @@ public void should_delete_files_that_do_not_belong_to_the_series_path()
|
||||
|
||||
Mocker.GetMock<IMediaFileService>().Verify(c => c.Delete(It.IsAny<EpisodeFile>(), false), Times.Exactly(10));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_unlink_episode_when_episodeFile_does_not_exist()
|
||||
{
|
||||
GivenEpisodeFiles(new List<EpisodeFile>());
|
||||
|
||||
Subject.Execute(new CleanMediaFileDb(0));
|
||||
|
||||
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.Is<Episode>(e => e.EpisodeFileId == 0)), Times.Exactly(10));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_update_episode_when_episodeFile_exists()
|
||||
{
|
||||
var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(10)
|
||||
.Random(10)
|
||||
.With(c => c.Path = "ExistingPath")
|
||||
.Build();
|
||||
|
||||
GivenEpisodeFiles(episodeFiles);
|
||||
|
||||
Subject.Execute(new CleanMediaFileDb(0));
|
||||
|
||||
Mocker.GetMock<IEpisodeService>().Verify(c => c.UpdateEpisode(It.IsAny<Episode>()), Times.Never());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,21 +16,18 @@ public class MediaFileTableCleanupService : IExecute<CleanMediaFileDb>
|
||||
private readonly IDiskProvider _diskProvider;
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly ISeriesService _seriesService;
|
||||
private readonly IParsingService _parsingService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public MediaFileTableCleanupService(IMediaFileService mediaFileService,
|
||||
IDiskProvider diskProvider,
|
||||
IEpisodeService episodeService,
|
||||
ISeriesService seriesService,
|
||||
IParsingService parsingService,
|
||||
Logger logger)
|
||||
{
|
||||
_mediaFileService = mediaFileService;
|
||||
_diskProvider = diskProvider;
|
||||
_episodeService = episodeService;
|
||||
_seriesService = seriesService;
|
||||
_parsingService = parsingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@ -38,6 +35,7 @@ public void Execute(CleanMediaFileDb message)
|
||||
{
|
||||
var seriesFile = _mediaFileService.GetFilesBySeries(message.SeriesId);
|
||||
var series = _seriesService.GetSeries(message.SeriesId);
|
||||
var episodes = _episodeService.GetEpisodeBySeries(message.SeriesId);
|
||||
|
||||
foreach (var episodeFile in seriesFile)
|
||||
{
|
||||
@ -57,9 +55,7 @@ public void Execute(CleanMediaFileDb message)
|
||||
continue;
|
||||
}
|
||||
|
||||
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
|
||||
|
||||
if (!episodes.Any())
|
||||
if (!episodes.Any(e => e.EpisodeFileId == episodeFile.Id))
|
||||
{
|
||||
_logger.Trace("File [{0}] is not assigned to any episodes, removing from db", episodeFile.Path);
|
||||
_mediaFileService.Delete(episodeFile);
|
||||
@ -75,12 +71,22 @@ public void Execute(CleanMediaFileDb message)
|
||||
// continue;
|
||||
// }
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMessage = String.Format("Unable to cleanup EpisodeFile in DB: {0}", episodeFile.Id);
|
||||
_logger.ErrorException(errorMessage, ex);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var episode in episodes)
|
||||
{
|
||||
if (episode.EpisodeFileId > 0 && seriesFile.SingleOrDefault(f => f.Id == episode.EpisodeFileId) == null)
|
||||
{
|
||||
episode.EpisodeFileId = 0;
|
||||
_episodeService.UpdateEpisode(episode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user