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

pull/4/head
Mark McDowall 11 years ago
parent 33db208bb6
commit e6b9cd7e60

@ -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);
}
}
}

@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using FizzWare.NBuilder;
using Marr.Data;
using Moq;
@ -122,7 +123,6 @@ namespace NzbDrone.Core.Test.MediaFileTests
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 @@ namespace NzbDrone.Core.Test.MediaFileTests
}
[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 @@ namespace NzbDrone.Core.Test.MediaFileTests
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());
}
}
}

@ -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" />

@ -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 @@ namespace NzbDrone.Core.MediaFiles
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))

@ -38,7 +38,7 @@ namespace NzbDrone.Core.MediaFiles
.Select(e => e.EpisodeFile.Value)
.GroupBy(e => e.Id);
episodeFile = _episodeFileMover.PreMoveEpisodeFile(episodeFile, localEpisode);
foreach (var existingFile in existingFiles)
{

Loading…
Cancel
Save