Fixed Renaming of episodes to include extension.

PostDownload Importing setup, need to setup job for period scan.
pull/3113/head
Mark McDowall 14 years ago
parent 96670d0567
commit 560706ae8f

@ -1,6 +1,7 @@
// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Linq;
using AutoMoq;
@ -8,6 +9,7 @@ using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
@ -132,7 +134,6 @@ namespace NzbDrone.Core.Test
Assert.AreNotEqual(new DateTime(), result.DateAdded);
}
[Test]
[Description("Verifies that a new file imported properly")]
public void import_existing_season_file()
@ -165,7 +166,6 @@ namespace NzbDrone.Core.Test
result.Should().BeNull();
}
[Test]
[Description("Verifies that a un-parsable file isn't imported")]
public void import_unparsable_file()
@ -251,7 +251,6 @@ namespace NzbDrone.Core.Test
var fakeSeries = Builder<Series>.CreateNew().Build();
//Mocks
var mocker = new AutoMoqer();
mocker.GetMock<IRepository>(MockBehavior.Strict)
.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(true).Verifiable();
@ -361,5 +360,120 @@ namespace NzbDrone.Core.Test
result.Should().HaveSameCount(firstSeriesFiles);
}
[Test]
[Description("Verifies that a new download will import successfully")]
public void import_new_download_success()
{
//Fakes
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.Path = @"C:\Test\TV\30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = fakeSeries.SeriesId)
.With(e => e.EpisodeFileId = 0)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 5)
.Build();
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".avi");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> {fakeEpisode});
episodeProvider.Setup(e => e.GetEpisode(fakeSeries.SeriesId, 1, 5)).Returns(fakeEpisode);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
[Test]
[Description("Verifies that a new download will import successfully, deletes previous episode")]
public void import_new_download_success_delete_equal_quality()
{
//Fakes
var fakeSeries = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.Path = @"C:\Test\TV\30 Rock")
.Build();
var fakeEpisode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = fakeSeries.SeriesId)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 5)
.With(e => e.EpisodeFileId = 1)
.Build();
var fakeEpisodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.SeriesId = fakeSeries.SeriesId)
.With(e => e.EpisodeFileId = 1)
.With(e => e.Quality = QualityTypes.SDTV)
.With(e => e.Episodes = new List<Episode>{ fakeEpisode })
.Build();
fakeEpisode.EpisodeFile = fakeEpisodeFile;
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".avi");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { fakeEpisode });
episodeProvider.Setup(e => e.GetEpisode(fakeSeries.SeriesId, 1, 5)).Returns(fakeEpisode);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", fakeSeries);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
}
}

@ -18,7 +18,7 @@ namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class RenameProviderTest : TestBase
public class MediaFileProvider_GetNewFilenameTest : TestBase
{
[Test]
public void GetNewFilename_Series_Episode_Quality_S01E05_Dash()
@ -34,23 +34,14 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(2);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> {episode});
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("South Park - S15E06 - City Sushi [HDTV]", result);
@ -70,23 +61,14 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(0);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("15x06 - City Sushi [HDTV]", result);
@ -106,23 +88,14 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(1);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("South Park 05x06 [HDTV]", result);
@ -142,23 +115,15 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(3);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("South Park s05e06", result);
@ -178,23 +143,14 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(3);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(true);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("South.Park.s05e06.City.Sushi", result);
@ -214,25 +170,14 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(3);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(true);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 5)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
var fakeSeriesProvider = mocker.GetMock<SeriesProvider>();
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("South.Park.-.s05e06.-.City.Sushi.[HDTV]", result);
@ -252,23 +197,15 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.NumberStyle).Returns(2);
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episode = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episode });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "South Park");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episode }, "South Park", QualityTypes.HDTV);
//Assert
Assert.AreEqual("S15E06", result);
@ -289,30 +226,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(3);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("The Mentalist - S03E23-E24 - Strawberries and Cream (1) + Strawberries and Cream (2) [HDTV]", result);
@ -333,30 +260,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(2);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("3x23x24 - Strawberries and Cream (1) + Strawberries and Cream (2) [HDTV]", result);
@ -377,30 +294,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(false);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(2);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("3x23x24 Strawberries and Cream (1) + Strawberries and Cream (2) [HDTV]", result);
@ -421,30 +328,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(true);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(1);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("The.Mentalist.s03e23.s03e24.Strawberries.and.Cream.(1).+.Strawberries.and.Cream.(2)", result);
@ -465,31 +362,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(true);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(0);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("The.Mentalist.-.S03E23-24", result);
@ -510,30 +396,20 @@ namespace NzbDrone.Core.Test
fakeConfig.SetupGet(c => c.ReplaceSpaces).Returns(true);
fakeConfig.SetupGet(c => c.MultiEpisodeStyle).Returns(2);
var episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.EpisodeFileId = 12345)
.With(e => e.Quality = QualityTypes.HDTV)
.Build();
var episodeOne = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (1)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 23)
.Build();
var episodeTwo = Builder<Episode>.CreateNew()
.With(e => e.EpisodeFileId = episodeFile.EpisodeFileId)
.With(e => e.Title = "Strawberries and Cream (2)")
.With(e => e.SeasonNumber = 3)
.With(e => e.EpisodeNumber = 24)
.Build();
var fakeEpisodeProvider = mocker.GetMock<EpisodeProvider>();
fakeEpisodeProvider.Setup(m => m.EpisodesByFileId(12345)).Returns(new List<Episode> { episodeOne, episodeTwo });
//Act
string result = mocker.Resolve<RenameProvider>().GetNewFilename(episodeFile, "The Mentalist");
string result = mocker.Resolve<MediaFileProvider>().GetNewFilename(new List<Episode> { episodeOne, episodeTwo }, "The Mentalist", QualityTypes.HDTV);
//Assert
Assert.AreEqual("3x23x24", result);

@ -0,0 +1,346 @@
// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Linq;
using AutoMoq;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Providers.Jobs;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using NzbDrone.Core.Test.Framework;
using SubSonic.Repository;
namespace NzbDrone.Core.Test
{
[TestFixture]
// ReSharper disable InconsistentNaming
public class MediaFileProvider_ImportNewDownloadTest : TestBase
{
private Episode episode;
private Episode episode2;
private EpisodeFile episodeFile;
private EpisodeFile episodeFile2;
private Series series;
[SetUp]
public new void Setup()
{
series = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.Path = @"C:\Test\TV\30 Rock")
.Build();
episode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = series.SeriesId)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 5)
.With(e => e.EpisodeFileId = 1)
.With(e => e.Title = "Episode One Title")
.Build();
episode2 = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = series.SeriesId)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 6)
.With(e => e.EpisodeFileId = 1)
.With(e => e.Title = "Episode Two Title")
.Build();
episodeFile = Builder<EpisodeFile>.CreateNew()
.With(e => e.SeriesId = series.SeriesId)
.With(e => e.EpisodeFileId = 1)
.With(e => e.Quality = QualityTypes.SDTV)
.With(e => e.Episodes = new List<Episode> { episode })
.Build();
episodeFile2 = Builder<EpisodeFile>.CreateNew()
.With(e => e.SeriesId = series.SeriesId)
.With(e => e.EpisodeFileId = 1)
.With(e => e.Quality = QualityTypes.SDTV)
.With(e => e.Episodes = new List<Episode> { episode })
.Build();
episode.EpisodeFile = episodeFile;
base.Setup();
}
[Test]
[Description("Verifies that a new download will import successfully")]
public void import_new_download_imported()
{
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".avi");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> {episode});
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 5)).Returns(episode);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
[Test]
[Description("Verifies that a new download will import successfully, deletes previous episode")]
public void import_new_download_imported_delete_equal_quality()
{
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".avi");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { episode });
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 5)).Returns(episode);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
[Test]
[Description("Verifies that a new download will not import successfully, because existing episode is better")]
public void import_new_download_not_imported_greater_quality()
{
//Alternate Setups
episodeFile.Quality = QualityTypes.DVD;
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".avi");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { episode });
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(0, result.Count);
}
[Test]
[Description("Verifies that a new download will not import successfully, because of invalid episode in new file")]
public void import_new_download_not_imported_non_existant_episode()
{
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05 - Episode Title\30.Rock.S01E05.Gibberish.XviD.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode>());
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(0, result.Count);
ExceptionVerification.ExcpectedErrors(1);
}
[Test]
[Description("Verifies that a new download will import successfully, deletes previous episode")]
public void import_new_download_imported_delete_lesser_quality_multi_episodes()
{
//Alternate Setup
episodeFile.Episodes.Add(episode2);
episode2.EpisodeFile = episodeFile;
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05x06 - Episode Title\30.Rock.S01E05E06.Gibberish.x264.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".mkv");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { episode, episode2 });
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 5)).Returns(episode);
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 6)).Returns(episode2);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05x06 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
[Test]
[Description("Verifies that a new download will import successfully, deletes previous episode")]
public void import_new_download_imported_delete_lesser_quality_multi_episode_files()
{
//Alternate Setup
episodeFile2.Episodes.Add(episode2);
episode2.EpisodeFile = episodeFile2;
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05x06 - Episode Title\30.Rock.S01E05E06.Gibberish.x264.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.RenameFile(It.IsAny<string>(), It.IsAny<string>()));
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".mkv");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { episode, episode2 });
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 5)).Returns(episode);
episodeProvider.Setup(e => e.GetEpisode(series.SeriesId, 1, 6)).Returns(episode2);
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
var repo = mocker.GetMock<IRepository>();
repo.Setup(r => r.Exists(It.IsAny<Expression<Func<EpisodeFile, Boolean>>>())).Returns(false).Verifiable();
repo.Setup(r => r.Add<EpisodeFile>(It.IsAny<EpisodeFile>())).Returns(1);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05x06 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(1, result.Count);
}
[Test]
[Description("Verifies that a new download will not import successfully, previous episode is better quality")]
public void import_new_download_not_imported_multi_episode_files()
{
//Alternate Setup
episodeFile2.Episodes.Add(episode2);
episode2.EpisodeFile = episodeFile2;
episodeFile2.Quality = QualityTypes.Bluray720p;
//Mocks
var mocker = new AutoMoqer();
var diskProvider = mocker.GetMock<DiskProvider>();
diskProvider.Setup(d => d.GetFiles(It.IsAny<string>(), "*.*", SearchOption.AllDirectories)).Returns(new string[] { @"C:\Test\30 Rock - 1x05x06 - Episode Title\30.Rock.S01E05E06.Gibberish.x264.avi" });
diskProvider.Setup(d => d.GetSize(It.IsAny<string>())).Returns(90000000000);
diskProvider.Setup(d => d.CreateDirectory(It.IsAny<string>())).Returns("ok");
diskProvider.Setup(d => d.GetExtension(It.IsAny<string>())).Returns(".mkv");
var episodeProvider = mocker.GetMock<EpisodeProvider>();
episodeProvider.Setup(e => e.GetEpisodes(It.IsAny<EpisodeParseResult>())).Returns(new List<Episode> { episode, episode2 });
var configProvider = mocker.GetMock<ConfigProvider>();
configProvider.SetupGet(c => c.UseSeasonFolder).Returns(true);
configProvider.SetupGet(c => c.SeasonFolderFormat).Returns(@"Season %0s");
configProvider.SetupGet(c => c.SeriesName).Returns(true);
configProvider.SetupGet(c => c.EpisodeName).Returns(true);
configProvider.SetupGet(c => c.AppendQuality).Returns(true);
configProvider.SetupGet(c => c.SeparatorStyle).Returns(0);
configProvider.SetupGet(c => c.NumberStyle).Returns(2);
configProvider.SetupGet(c => c.ReplaceSpaces).Returns(false);
//Act
var result = mocker.Resolve<MediaFileProvider>().ImportNewFiles(@"C:\Test\30 Rock - 1x05x06 - Episode Title", series);
//Assert
mocker.VerifyAllMocks();
Assert.AreEqual(0, result.Count);
}
}
}

@ -90,7 +90,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="RenameProviderTest.cs" />
<Compile Include="MediaFileProvider_ImportNewDownloadTest.cs" />
<Compile Include="MediaFileProvider_GetNewFilenameTest.cs" />
<Compile Include="dbBenchmark.cs" />
<Compile Include="Framework\TestBase.cs" />
<Compile Include="InventoryProvider_IsMonitoredTest.cs" />

@ -87,7 +87,6 @@ namespace NzbDrone.Core
_kernel.Bind<XbmcProvider>().ToSelf().InSingletonScope();
_kernel.Bind<ConfigProvider>().To<ConfigProvider>().InSingletonScope();
_kernel.Bind<SyncProvider>().ToSelf().InSingletonScope();
_kernel.Bind<RenameProvider>().ToSelf().InSingletonScope();
_kernel.Bind<NotificationProvider>().ToSelf().InSingletonScope();
_kernel.Bind<LogProvider>().ToSelf().InSingletonScope();
_kernel.Bind<MediaFileProvider>().ToSelf().InSingletonScope();

@ -195,6 +195,7 @@
<Compile Include="Providers\Jobs\IJob.cs" />
<Compile Include="Providers\Jobs\RssSyncJob.cs" />
<Compile Include="Providers\Jobs\UpdateInfoJob.cs" />
<Compile Include="Providers\PostProcessingProvider.cs" />
<Compile Include="Providers\StatsProvider.cs" />
<Compile Include="Repository\ExternalNotificationSetting.cs" />
<Compile Include="Repository\JobSetting.cs" />
@ -214,7 +215,6 @@
<Compile Include="Providers\BacklogProvider.cs" />
<Compile Include="Providers\IndexerProvider.cs" />
<Compile Include="Providers\QualityProvider.cs" />
<Compile Include="Providers\RenameProvider.cs" />
<Compile Include="Providers\RootDirProvider.cs" />
<Compile Include="Providers\UpcomingEpisodesProvider.cs" />
<Compile Include="Providers\XbmcProvider.cs" />

@ -174,6 +174,13 @@ namespace NzbDrone.Core.Providers.Core
set { SetValue("SabTvPriority", (int)value); }
}
public virtual String SabDropDirectory
{
get { return GetValue("SabTvDropDirectory", "", false); }
set { SetValue("SabTvDropDirectory", value); }
}
public virtual Boolean UseBlackhole
{
get { return GetValueBoolean("UseBlackhole"); }

@ -46,5 +46,10 @@ namespace NzbDrone.Core.Providers.Core
{
File.Move(sourcePath, destinationPath);
}
public virtual string GetExtension(string path)
{
return Path.GetExtension(path);
}
}
}

@ -3,18 +3,26 @@ using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
namespace NzbDrone.Core.Providers.Jobs
{
public class RenameEpisodeJob : IJob
{
private readonly RenameProvider _renameProvider;
private readonly DiskProvider _diskProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly MediaFileProvider _mediaFileProvider;
private readonly SeriesProvider _seriesProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public RenameEpisodeJob(RenameProvider renameProvider)
public RenameEpisodeJob(DiskProvider diskProvider, EpisodeProvider episodeProvider,
MediaFileProvider mediaFileProvider, SeriesProvider seriesProvider)
{
_renameProvider = renameProvider;
_diskProvider = diskProvider;
_episodeProvider = episodeProvider;
_mediaFileProvider = mediaFileProvider;
_seriesProvider = seriesProvider;
}
public string Name
@ -29,7 +37,7 @@ namespace NzbDrone.Core.Providers.Jobs
public void Start(ProgressNotification notification, int targetId)
{
_renameProvider.RenameEpisodeFile(targetId, notification);
_mediaFileProvider.RenameEpisodeFile(targetId, notification);
}
}
}

@ -3,8 +3,11 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Repository.Quality;
using SubSonic.Repository;
namespace NzbDrone.Core.Providers
@ -17,14 +20,17 @@ namespace NzbDrone.Core.Providers
private readonly EpisodeProvider _episodeProvider;
private readonly SeriesProvider _seriesProvider;
private readonly IRepository _repository;
private readonly ConfigProvider _configProvider;
public MediaFileProvider(IRepository repository, DiskProvider diskProvider,
EpisodeProvider episodeProvider, SeriesProvider seriesProvider)
EpisodeProvider episodeProvider, SeriesProvider seriesProvider,
ConfigProvider configProvider)
{
_repository = repository;
_diskProvider = diskProvider;
_episodeProvider = episodeProvider;
_seriesProvider = seriesProvider;
_configProvider = configProvider;
}
public MediaFileProvider() { }
@ -71,7 +77,6 @@ namespace NzbDrone.Core.Providers
{
var parseResult = Parser.ParseEpisodeInfo(filePath);
if (parseResult == null)
return null;
@ -162,6 +167,15 @@ namespace NzbDrone.Core.Providers
if (!_diskProvider.FileExists(episodeFile.Path))
{
Logger.Trace("File {0} no longer exists on disk. removing from database.", episodeFile.Path);
//Set the EpisodeFileId for each episode attached to this file to 0
foreach (var episode in episodeFile.Episodes)
{
episode.EpisodeFileId = 0;
_episodeProvider.UpdateEpisode(episode);
}
//Delete it from the DB
_repository.Delete<EpisodeFile>(episodeFile.EpisodeFileId);
}
}
@ -208,5 +222,204 @@ namespace NzbDrone.Core.Providers
Logger.Debug("{0} media files were found in {1}", mediaFileList.Count, path);
return mediaFileList;
}
public virtual List<EpisodeFile> ImportNewFiles(string path, Series series)
{
var result = new List<EpisodeFile>();
var files = GetMediaFileList(path);
foreach (var file in files)
{
//If Size is less than 40MB and contains sample. Check for Size to ensure its not an episode with sample in the title
if (_diskProvider.GetSize(file) < 40000000 && file.ToLower().Contains("sample"))
{
Logger.Trace("[{0}] appears to be a sample. Skipping.", file);
continue;
}
//Parse the filename
var parseResult = Parser.ParseEpisodeInfo(Path.GetFileName(file));
parseResult.Series = series;
parseResult.Episodes = _episodeProvider.GetEpisodes(parseResult);
if (parseResult.Episodes.Count == 0)
{
Logger.Error("File '{0}' contains invalid episode information, skipping import", file);
continue;
}
var ext = _diskProvider.GetExtension(file);
var filename = GetNewFilename(parseResult.Episodes, series.Title, parseResult.Quality.QualityType) + ext;
var folder = series.Path + Path.DirectorySeparatorChar;
if (_configProvider.UseSeasonFolder)
folder += _configProvider.SeasonFolderFormat
.Replace("%0s", parseResult.SeasonNumber.ToString("00"))
.Replace("%s", parseResult.SeasonNumber.ToString())
+ Path.DirectorySeparatorChar;
_diskProvider.CreateDirectory(folder);
//Get a list of episodeFiles that we need to delete and cleanup
var episodeFilesToClean = new List<EpisodeFile>();
foreach (var episode in parseResult.Episodes)
{
if (episode.EpisodeFileId > 0)
episodeFilesToClean.Add(episode.EpisodeFile);
}
if (episodeFilesToClean.Count != episodeFilesToClean.Where(e => parseResult.Quality.QualityType >= e.Quality).Count())
{
Logger.Debug("Episode isn't an upgrade for all episodes in file: [{0}]. Skipping.", file);
continue;
}
//Delete the files and then cleanup!
episodeFilesToClean.ForEach(e => _diskProvider.DeleteFile(e.Path));
CleanUp(episodeFilesToClean);
//Move the file
_diskProvider.RenameFile(file, folder + filename);
//Import into DB
result.Add(ImportFile(series, folder + filename));
}
return result;
}
public virtual string GetNewFilename(IList<Episode> episodes, string seriesName, QualityTypes quality)
{
var separatorStyle = EpisodeSortingHelper.GetSeparatorStyle(_configProvider.SeparatorStyle);
var numberStyle = EpisodeSortingHelper.GetNumberStyle(_configProvider.NumberStyle);
var useSeriesName = _configProvider.SeriesName;
var useEpisodeName = _configProvider.EpisodeName;
var replaceSpaces = _configProvider.ReplaceSpaces;
var appendQuality = _configProvider.AppendQuality;
var title = String.Empty;
if (episodes.Count == 1)
{
if (useSeriesName)
{
title += seriesName;
title += separatorStyle.Pattern;
}
title += numberStyle.Pattern.Replace("%s", String.Format("{0}", episodes[0].SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episodes[0].SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
if (useEpisodeName)
{
title += separatorStyle.Pattern;
title += episodes[0].Title;
}
if (appendQuality)
title += String.Format(" [{0}]", quality);
if (replaceSpaces)
title = title.Replace(' ', '.');
Logger.Debug("New File Name is: {0}", title);
return title;
}
var multiEpisodeStyle = EpisodeSortingHelper.GetMultiEpisodeStyle(_configProvider.MultiEpisodeStyle);
if (useSeriesName)
{
title += seriesName;
title += separatorStyle.Pattern;
}
title += numberStyle.Pattern.Replace("%s", String.Format("{0}", episodes[0].SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episodes[0].SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
var numbers = String.Empty;
var episodeNames = episodes[0].Title;
for (int i = 1; i < episodes.Count; i++)
{
var episode = episodes[i];
if (multiEpisodeStyle.Name == "Duplicate")
{
numbers += separatorStyle.Pattern + numberStyle.Pattern.Replace("%s", String.Format("{0}", episode.SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episode.SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber));
}
else
{
numbers += multiEpisodeStyle.Pattern.Replace("%s", String.Format("{0}", episode.SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episode.SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber))
.Replace("%x", numberStyle.EpisodeSeparator)
.Replace("%p", separatorStyle.Pattern);
}
episodeNames += String.Format(" + {0}", episode.Title);
}
title += numbers;
if (useEpisodeName)
{
episodeNames = episodeNames.TrimEnd(' ', '+');
title += separatorStyle.Pattern;
title += episodeNames;
}
if (appendQuality)
title += String.Format(" [{0}]", quality);
if (replaceSpaces)
title = title.Replace(' ', '.');
Logger.Debug("New File Name is: {0}", title);
return title;
}
public virtual bool RenameEpisodeFile(int episodeFileId, ProgressNotification notification)
{
var episodeFile = GetEpisodeFile(episodeFileId);
if (episodeFile == null)
return false;
try
{
notification.CurrentMessage = String.Format("Renaming '{0}'", episodeFile.Path);
var series = _seriesProvider.GetSeries(episodeFile.SeriesId);
var folder = new FileInfo(episodeFile.Path).DirectoryName;
var episodes = _episodeProvider.EpisodesByFileId(episodeFileId);
var ext = _diskProvider.GetExtension(episodeFile.Path);
var newFileName = GetNewFilename(episodes, series.Title, episodeFile.Quality);
var newFile = folder + Path.DirectorySeparatorChar + newFileName + ext;
//Do the rename
_diskProvider.RenameFile(episodeFile.Path, newFile);
//Update the filename in the DB
episodeFile.Path = newFile;
Update(episodeFile);
notification.CurrentMessage = String.Format("Finished Renaming '{0}'", newFile);
}
catch (Exception e)
{
notification.CurrentMessage = String.Format("Failed to Rename '{0}'", episodeFile.Path);
Logger.ErrorException("An error has occurred while renaming episode: " + episodeFile.Path, e);
throw;
}
return true;
}
}
}

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class PostProcessingProvider
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
private readonly DiskProvider _diskProvider;
private readonly SeriesProvider _seriesProvider;
private readonly MediaFileProvider _mediaFileProvider;
private readonly EpisodeProvider _episodeProvider;
public PostProcessingProvider(ConfigProvider configProvider, DiskProvider diskProvider,
SeriesProvider seriesProvider, MediaFileProvider mediaFileProvider,
EpisodeProvider episodeProvider)
{
_configProvider = configProvider;
_diskProvider = diskProvider;
_seriesProvider = seriesProvider;
_mediaFileProvider = mediaFileProvider;
_episodeProvider = episodeProvider;
}
//Scan folder
//Delete Existing episode(s)
//Move file(s)
//Import file(s)
public virtual void Scan()
{
var dropFolder = _configProvider.SabDropDirectory;
if (String.IsNullOrEmpty(dropFolder))
{
Logger.Warn("Unable to Scan for New Downloads - Folder Name is Empty");
return;
}
if (!_diskProvider.FolderExists(dropFolder))
{
Logger.Warn("Unable to Scan for New Downloads - Folder Doesn't Exist");
return;
}
var subfolders = _diskProvider.GetDirectories(dropFolder);
foreach (var subfolder in subfolders)
{
var di = new DirectoryInfo(subfolder);
if (di.Name.StartsWith("_UNPACK_"))
{
Logger.Info("Folder [{0}] is still being unpacked", subfolder);
continue;
}
if (di.Name.StartsWith("_FAILED_"))
{
Logger.Info("Folder [{0}] is marked as failed", subfolder);
continue;
}
//Parse the Folder name
var seriesName = Parser.ParseSeriesName(di.Name);
var series = _seriesProvider.FindSeries(seriesName);
if (series == null)
{
Logger.Warn("Unable to Import new download, series is not being watched");
return;
}
_mediaFileProvider.ImportNewFiles(subfolder, series);
}
}
}
}

@ -1,157 +0,0 @@
using System;
using System.IO;
using System.Linq;
using NLog;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Providers
{
public class RenameProvider
{
//TODO: Remove some of these dependencies. we shouldn't have a single class with dependency on the whole app!
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ConfigProvider _configProvider;
private readonly DiskProvider _diskProvider;
private readonly EpisodeProvider _episodeProvider;
private readonly MediaFileProvider _mediaFileProvider;
private readonly SeriesProvider _seriesProvider;
public RenameProvider(SeriesProvider seriesProvider,EpisodeProvider episodeProvider,
MediaFileProvider mediaFileProvider, DiskProvider diskProvider,
ConfigProvider configProvider)
{
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_mediaFileProvider = mediaFileProvider;
_diskProvider = diskProvider;
_configProvider = configProvider;
}
public virtual void RenameEpisodeFile(int episodeFileId, ProgressNotification notification)
{
var episodeFile = _mediaFileProvider.GetEpisodeFile(episodeFileId);
try
{
notification.CurrentMessage = String.Format("Renaming '{0}'", episodeFile.Path);
var series = _seriesProvider.GetSeries(episodeFile.SeriesId);
var folder = new FileInfo(episodeFile.Path).DirectoryName;
var newFileName = GetNewFilename(episodeFile, series.Title);
var newFile = folder + Path.DirectorySeparatorChar + newFileName;
_diskProvider.RenameFile(episodeFile.Path, newFile);
notification.CurrentMessage = String.Format("Finished Renaming '{0}'", newFile);
}
catch (Exception e)
{
notification.CurrentMessage = String.Format("Failed to Rename '{0}'", episodeFile.Path);
Logger.ErrorException("An error has occurred while renaming episode: " + episodeFile.Path, e);
throw;
}
}
public string GetNewFilename(EpisodeFile episodeFile, string seriesName)
{
var episodes = _episodeProvider.EpisodesByFileId(episodeFile.EpisodeFileId);
//var series = _seriesProvider.GetSeries(episodeFile.SeriesId);
var separatorStyle = EpisodeSortingHelper.GetSeparatorStyle(_configProvider.SeparatorStyle);
var numberStyle = EpisodeSortingHelper.GetNumberStyle(_configProvider.NumberStyle);
var useSeriesName = _configProvider.SeriesName;
var useEpisodeName = _configProvider.EpisodeName;
var replaceSpaces = _configProvider.ReplaceSpaces;
var appendQuality = _configProvider.AppendQuality;
var title = String.Empty;
if (episodes.Count == 1)
{
if (useSeriesName)
{
title += seriesName;
title += separatorStyle.Pattern;
}
title += numberStyle.Pattern.Replace("%s", String.Format("{0}", episodes[0].SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episodes[0].SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
if (useEpisodeName)
{
title += separatorStyle.Pattern;
title += episodes[0].Title;
}
if (appendQuality)
title += String.Format(" [{0}]", episodeFile.Quality);
if (replaceSpaces)
title = title.Replace(' ', '.');
Logger.Debug("New File Name is: {0}", title);
return title;
}
var multiEpisodeStyle = EpisodeSortingHelper.GetMultiEpisodeStyle(_configProvider.MultiEpisodeStyle);
if (useSeriesName)
{
title += seriesName;
title += separatorStyle.Pattern;
}
title += numberStyle.Pattern.Replace("%s", String.Format("{0}", episodes[0].SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episodes[0].SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episodes[0].EpisodeNumber));
var numbers = String.Empty;
var episodeNames = episodes[0].Title;
for (int i = 1; i < episodes.Count; i++)
{
var episode = episodes[i];
if (multiEpisodeStyle.Name == "Duplicate")
{
numbers += separatorStyle.Pattern + numberStyle.Pattern.Replace("%s", String.Format("{0}", episode.SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episode.SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber));
}
else
{
numbers += multiEpisodeStyle.Pattern.Replace("%s", String.Format("{0}", episode.SeasonNumber))
.Replace("%0s", String.Format("{0:00}", episode.SeasonNumber))
.Replace("%0e", String.Format("{0:00}", episode.EpisodeNumber))
.Replace("%x", numberStyle.EpisodeSeparator)
.Replace("%p", separatorStyle.Pattern);
}
episodeNames += String.Format(" + {0}", episode.Title);
}
title += numbers;
if (useEpisodeName)
{
episodeNames = episodeNames.TrimEnd(' ', '+');
title += separatorStyle.Pattern;
title += episodeNames;
}
if (appendQuality)
title += String.Format(" [{0}]", episodeFile.Quality);
if (replaceSpaces)
title = title.Replace(' ', '.');
Logger.Debug("New File Name is: {0}", title);
return title;
}
}
}

@ -19,9 +19,9 @@ namespace NzbDrone.Core.Repository
public DateTime DateAdded { get; set; }
[SubSonicToManyRelation]
public virtual IList<Episode> Episodes { get; private set; }
public virtual IList<Episode> Episodes { get; set; }
[SubSonicToOneRelation(ThisClassContainsJoinKey = true)]
public virtual Series Series { get; private set; }
public virtual Series Series { get; set; }
}
}

@ -16,7 +16,6 @@ namespace NzbDrone.Web.Controllers
{
private readonly EpisodeProvider _episodeProvider;
private readonly QualityProvider _qualityProvider;
private readonly RenameProvider _renameProvider;
private readonly SeriesProvider _seriesProvider;
private readonly TvDbProvider _tvDbProvider;
private readonly JobProvider _jobProvider;
@ -27,7 +26,6 @@ namespace NzbDrone.Web.Controllers
public SeriesController(SeriesProvider seriesProvider,
EpisodeProvider episodeProvider,
QualityProvider qualityProvider,
RenameProvider renameProvider,
TvDbProvider tvDbProvider,
JobProvider jobProvider,
MediaFileProvider mediaFileProvider)
@ -35,7 +33,6 @@ namespace NzbDrone.Web.Controllers
_seriesProvider = seriesProvider;
_episodeProvider = episodeProvider;
_qualityProvider = qualityProvider;
_renameProvider = renameProvider;
_tvDbProvider = tvDbProvider;
_jobProvider = jobProvider;
_mediaFileProvider = mediaFileProvider;

Loading…
Cancel
Save