1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Episode files are moved to final folder without renaming before deletion and final renaming

This commit is contained in:
Mark McDowall 2013-08-07 22:40:31 -07:00
parent 33db208bb6
commit e6b9cd7e60
5 changed files with 40 additions and 196 deletions

View File

@ -1,166 +0,0 @@
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Qualities;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFileTests
{
public class EpisodeFileMoverFixture : CoreTest<MoveEpisodeFiles>
{
[Test]
public void should_not_move_file_if_source_and_destination_are_the_same_path()
{
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Id = 5)
.With(s => s.Title = "30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.SeriesId = fakeSeries.Id)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 1)
.Build().ToList();
const string filename = @"30 Rock - S01E01 - TBD";
var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".avi").AsOsAgnostic();
var file = Builder<EpisodeFile>.CreateNew()
.With(f => f.SeriesId = fakeSeries.Id)
.With(f => f.Path = fi)
.Build();
Mocker.GetMock<ISeriesRepository>()
.Setup(e => e.Get(fakeSeries.Id))
.Returns(fakeSeries);
Mocker.GetMock<IEpisodeService>()
.Setup(e => e.GetEpisodesByFileId(file.Id))
.Returns(fakeEpisode);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny<EpisodeFile>()))
.Returns(filename);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilePath(It.IsAny<Series>(), fakeEpisode.First().SeasonNumber, filename, ".avi"))
.Returns(fi);
Mocker.GetMock<IDiskProvider>()
.Setup(s => s.FileExists(fi))
.Returns(true);
var result = Subject.MoveEpisodeFile(file);
result.Should().BeNull();
}
[Test]
public void should_use_EpisodeFiles_quality()
{
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Id = 5)
.With(s => s.Title = "30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.SeriesId = fakeSeries.Id)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 1)
.Build().ToList();
const string filename = @"30 Rock - S01E01 - TBD";
var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".mkv").AsOsAgnostic();
var currentFilename = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", "30.Rock.S01E01.Test.WED-DL.mkv").AsOsAgnostic();
const string message = "30 Rock - 1x01 - [WEBDL]";
var file = Builder<EpisodeFile>.CreateNew()
.With(f => f.SeriesId = fakeSeries.Id)
.With(f => f.Path = currentFilename)
.With(f => f.Quality = new QualityModel(Quality.WEBDL720p))
.Build();
Mocker.GetMock<ISeriesRepository>()
.Setup(e => e.Get(fakeSeries.Id))
.Returns(fakeSeries);
Mocker.GetMock<IEpisodeService>()
.Setup(e => e.GetEpisodesByFileId(file.Id))
.Returns(fakeEpisode);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny<EpisodeFile>()))
.Returns(filename);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilePath(It.IsAny<Series>(), fakeEpisode.First().SeasonNumber, filename, ".mkv"))
.Returns(fi);
Mocker.GetMock<IDiskProvider>()
.Setup(s => s.FileExists(currentFilename))
.Returns(true);
var result = Subject.MoveEpisodeFile(file);
}
[Test]
public void should_log_error_and_return_null_when_source_file_does_not_exists()
{
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Id = 5)
.With(s => s.Title = "30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.SeriesId = fakeSeries.Id)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 1)
.Build().ToList();
const string filename = @"30 Rock - S01E01 - TBD";
var fi = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", filename + ".mkv").AsOsAgnostic();
var currentFilename = Path.Combine(@"C:\Test\TV\30 Rock\Season 01\", "30.Rock.S01E01.Test.WED-DL.mkv").AsOsAgnostic();
const string message = "30 Rock - 1x01 - [WEBDL]";
var file = Builder<EpisodeFile>.CreateNew()
.With(f => f.SeriesId = fakeSeries.Id)
.With(f => f.Path = currentFilename)
.With(f => f.Quality = new QualityModel(Quality.WEBDL720p))
.Build();
Mocker.GetMock<ISeriesRepository>()
.Setup(e => e.Get(fakeSeries.Id))
.Returns(fakeSeries);
Mocker.GetMock<IEpisodeService>()
.Setup(e => e.GetEpisodesByFileId(file.Id))
.Returns(fakeEpisode);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilename(fakeEpisode, fakeSeries, It.IsAny<EpisodeFile>()))
.Returns(filename);
Mocker.GetMock<IBuildFileNames>()
.Setup(e => e.BuildFilePath(It.IsAny<Series>(), fakeEpisode.First().SeasonNumber, filename, ".mkv"))
.Returns(fi);
var result = Subject.MoveEpisodeFile(file);
result.Should().BeNull();
ExceptionVerification.ExpectedErrors(1);
}
}
}

View File

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using FizzWare.NBuilder;
using Marr.Data;
using Moq;
@ -122,7 +123,6 @@ public void should_delete_episode_file_from_database()
Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(It.IsAny<EpisodeFile>(), true), Times.Once());
}
[Test]
public void should_delete_existing_file_fromdb_if_file_doesnt_exist()
{
@ -138,7 +138,7 @@ public void should_delete_existing_file_fromdb_if_file_doesnt_exist()
}
[Test]
public void should_not_try_to_recyclebin_existing_file_fromdb_if_file_doesnt_exist()
public void should_not_try_to_recyclebin_existing_file_if_file_doesnt_exist()
{
GivenSingleEpisodeWithSingleEpisodeFile();
@ -150,5 +150,30 @@ public void should_not_try_to_recyclebin_existing_file_fromdb_if_file_doesnt_exi
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>()), Times.Never());
}
[Test]
public void should_not_delete_file_if_pre_move_fails()
{
GivenSingleEpisodeWithSingleEpisodeFile();
Mocker.GetMock<IMoveEpisodeFiles>()
.Setup(s => s.PreMoveEpisodeFile(It.IsAny<EpisodeFile>(), It.IsAny<LocalEpisode>()))
.Throws<InvalidOperationException>();
Assert.Throws<InvalidOperationException>(() => Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode));
Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>()), Times.Never());
}
[Test]
public void should_move_after_premove()
{
GivenSingleEpisodeWithSingleEpisodeFile();
Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
Mocker.GetMock<IMoveEpisodeFiles>().Verify(v => v.PreMoveEpisodeFile(It.IsAny<EpisodeFile>(), It.IsAny<LocalEpisode>()), Times.Once());
Mocker.GetMock<IMoveEpisodeFiles>().Verify(v => v.MoveEpisodeFile(It.IsAny<EpisodeFile>(), It.IsAny<LocalEpisode>()), Times.Once());
}
}
}

View File

@ -138,7 +138,6 @@
<Compile Include="MediaFileTests\EpisodeImportTests\ImportDecisionMakerFixture.cs" />
<Compile Include="MediaFileTests\MediaFileTableCleanupServiceFixture.cs" />
<Compile Include="MediaFileTests\MediaFileRepositoryFixture.cs" />
<Compile Include="MediaFileTests\EpisodeFileMoverFixture.cs" />
<Compile Include="MetadataSourceTests\TraktProxyFixture.cs" />
<Compile Include="NotificationTests\NotificationServiceFixture.cs" />
<Compile Include="NotificationTests\Xbmc\GetJsonVersionFixture.cs" />

View File

@ -13,55 +13,32 @@ namespace NzbDrone.Core.MediaFiles
{
public interface IMoveEpisodeFiles
{
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile);
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series);
EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode);
}
public class MoveEpisodeFiles : IMoveEpisodeFiles
{
private readonly ISeriesRepository _seriesRepository;
private readonly IEpisodeService _episodeService;
private readonly IBuildFileNames _buildFileNames;
private readonly IMediaFileService _mediaFileService;
private readonly IMessageAggregator _messageAggregator;
private readonly IDiskProvider _diskProvider;
private readonly Logger _logger;
public MoveEpisodeFiles(ISeriesRepository seriesRepository,
IEpisodeService episodeService,
public MoveEpisodeFiles(IEpisodeService episodeService,
IBuildFileNames buildFileNames,
IMediaFileService mediaFileService,
IMessageAggregator messageAggregator,
IDiskProvider diskProvider,
Logger logger)
{
_seriesRepository = seriesRepository;
_episodeService = episodeService;
_buildFileNames = buildFileNames;
_mediaFileService = mediaFileService;
_messageAggregator = messageAggregator;
_diskProvider = diskProvider;
_logger = logger;
}
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile)
{
if (episodeFile == null)
throw new ArgumentNullException("episodeFile");
var series = _seriesRepository.Get(episodeFile.SeriesId);
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
var newFileName = _buildFileNames.BuildFilename(episodes, series, episodeFile);
var destinationFilename = _buildFileNames.BuildFilePath(series, episodes.First().SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
episodeFile = MoveFile(episodeFile, destinationFilename);
_mediaFileService.Update(episodeFile);
return episodeFile;
}
public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, Series series)
{
var episodes = _episodeService.GetEpisodesByFileId(episodeFile.Id);
@ -82,6 +59,15 @@ public EpisodeFile MoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEp
return episodeFile;
}
public EpisodeFile PreMoveEpisodeFile(EpisodeFile episodeFile, LocalEpisode localEpisode)
{
var newFileName = Path.GetFileNameWithoutExtension(episodeFile.Path);
var destinationFilename = _buildFileNames.BuildFilePath(localEpisode.Series, localEpisode.SeasonNumber, newFileName, Path.GetExtension(episodeFile.Path));
episodeFile = MoveFile(episodeFile, destinationFilename);
return episodeFile;
}
private EpisodeFile MoveFile(EpisodeFile episodeFile, string destinationFilename)
{
if (!_diskProvider.FileExists(episodeFile.Path))

View File

@ -38,7 +38,7 @@ public EpisodeFile UpgradeEpisodeFile(EpisodeFile episodeFile, LocalEpisode loca
.Select(e => e.EpisodeFile.Value)
.GroupBy(e => e.Id);
episodeFile = _episodeFileMover.PreMoveEpisodeFile(episodeFile, localEpisode);
foreach (var existingFile in existingFiles)
{