From 0c23511b7ad7b6ca0f35416f512bdea8dff2d757 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 18 Feb 2018 17:29:41 +0100 Subject: [PATCH] Added: Same File Size MediaFile Specification (#2532) --- .../SameFileSpecificationFixture.cs | 67 +++++++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../Specifications/SameFileSpecification.cs | 42 ++++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 4 files changed, 111 insertions(+) create mode 100644 src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/SameFileSpecificationFixture.cs create mode 100644 src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/SameFileSpecification.cs diff --git a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/SameFileSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/SameFileSpecificationFixture.cs new file mode 100644 index 000000000..41b36d971 --- /dev/null +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/SameFileSpecificationFixture.cs @@ -0,0 +1,67 @@ +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Marr.Data; +using NUnit.Framework; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications +{ + [TestFixture] + public class SameFileSpecificationFixture : CoreTest + { + private LocalMovie _localMovie; + + [SetUp] + public void Setup() + { + _localMovie = Builder.CreateNew() + .With(l => l.Size = 150.Megabytes()) + .Build(); + } + + [Test] + public void should_be_accepted_if_no_existing_file() + { + _localMovie.Movie = Builder.CreateNew() + .With(e => e.MovieFileId = 0) + .Build(); + + Subject.IsSatisfiedBy(_localMovie, null).Accepted.Should().BeTrue(); + } + + [Test] + public void should_be_accepted_if_file_size_is_different() + { + _localMovie.Movie = Builder.CreateNew() + .With(e => e.MovieFileId = 1) + .With(e => e.MovieFile = new LazyLoaded( + new MovieFile + { + Size = _localMovie.Size + 100.Megabytes() + })) + .Build(); + + Subject.IsSatisfiedBy(_localMovie, null).Accepted.Should().BeTrue(); + } + + [Test] + public void should_be_reject_if_file_size_is_the_same() + { + _localMovie.Movie = Builder.CreateNew() + .With(e => e.MovieFileId = 1) + .With(e => e.MovieFile = new LazyLoaded( + new MovieFile + { + Size = _localMovie.Size + })) + .Build(); + + Subject.IsSatisfiedBy(_localMovie, null).Accepted.Should().BeFalse(); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index a56079e96..e2f3ade60 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -283,6 +283,7 @@ + diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/SameFileSpecification.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/SameFileSpecification.cs new file mode 100644 index 000000000..7eeb93684 --- /dev/null +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/SameFileSpecification.cs @@ -0,0 +1,42 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.DecisionEngine; +using NzbDrone.Core.Download; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications +{ + public class SameFileSpecification : IImportDecisionEngineSpecification + { + private readonly Logger _logger; + + public SameFileSpecification(Logger logger) + { + _logger = logger; + } + + public Decision IsSatisfiedBy(LocalEpisode localEpisode) + { + return Decision.Accept(); + } + + public Decision IsSatisfiedBy(LocalMovie localMovie, DownloadClientItem downloadClientItem) + { + var movieFile = localMovie.Movie.MovieFile; + + if (localMovie.Movie.MovieFileId == 0) + { + _logger.Debug("No existing movie file, skipping"); + return Decision.Accept(); + } + + if (movieFile.Size == localMovie.Size) + { + _logger.Debug("'{0}' Has the same filesize as existing file", localMovie.Path); + return Decision.Reject("Has the same filesize as existing file"); + } + + return Decision.Accept(); + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index bf3bb5215..7d6e50944 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -131,6 +131,7 @@ +