diff --git a/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/SameFileSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/SameFileSpecificationFixture.cs new file mode 100644 index 000000000..073ce9cd5 --- /dev/null +++ b/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Specifications/SameFileSpecificationFixture.cs @@ -0,0 +1,96 @@ +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Marr.Data; +using NUnit.Framework; +using NzbDrone.Core.MediaFiles; +using NzbDrone.Core.MediaFiles.TrackImport.Specifications; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Music; + +namespace NzbDrone.Core.Test.MediaFiles.TrackImport.Specifications +{ + [TestFixture] + public class SameFileSpecificationFixture : CoreTest + { + private LocalTrack _localTrack; + + [SetUp] + public void Setup() + { + _localTrack = Builder.CreateNew() + .With(l => l.Size = 150.Megabytes()) + .Build(); + } + + [Test] + public void should_be_accepted_if_no_existing_file() + { + _localTrack.Tracks = Builder.CreateListOfSize(1) + .TheFirst(1) + .With(e => e.TrackFileId = 0) + .BuildList(); + + Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue(); + } + + [Test] + public void should_be_accepted_if_multiple_existing_files() + { + _localTrack.Tracks = Builder.CreateListOfSize(2) + .TheFirst(1) + .With(e => e.TrackFileId = 1) + .With(e => e.TrackFile = new LazyLoaded( + new TrackFile + { + Size = _localTrack.Size + })) + .TheNext(1) + .With(e => e.TrackFileId = 2) + .With(e => e.TrackFile = new LazyLoaded( + new TrackFile + { + Size = _localTrack.Size + })) + .Build() + .ToList(); + + Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue(); + } + + [Test] + public void should_be_accepted_if_file_size_is_different() + { + _localTrack.Tracks = Builder.CreateListOfSize(1) + .TheFirst(1) + .With(e => e.TrackFileId = 1) + .With(e => e.TrackFile = new LazyLoaded( + new TrackFile + { + Size = _localTrack.Size + 100.Megabytes() + })) + .Build() + .ToList(); + + Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeTrue(); + } + + [Test] + public void should_be_reject_if_file_size_is_the_same() + { + _localTrack.Tracks = Builder.CreateListOfSize(1) + .TheFirst(1) + .With(e => e.TrackFileId = 1) + .With(e => e.TrackFile = new LazyLoaded( + new TrackFile + { + Size = _localTrack.Size + })) + .Build() + .ToList(); + + Subject.IsSatisfiedBy(_localTrack).Accepted.Should().BeFalse(); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index d5a0c106e..a65554526 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -254,6 +254,7 @@ + diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/SameFileSpecification.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/SameFileSpecification.cs new file mode 100644 index 000000000..1ef2f8df6 --- /dev/null +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/Specifications/SameFileSpecification.cs @@ -0,0 +1,43 @@ +using System.Linq; +using NLog; +using NzbDrone.Core.DecisionEngine; +using NzbDrone.Core.Download; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.MediaFiles.TrackImport.Specifications +{ + public class SameFileSpecification : IImportDecisionEngineSpecification + { + private readonly Logger _logger; + + public SameFileSpecification(Logger logger) + { + _logger = logger; + } + + public Decision IsSatisfiedBy(LocalTrack localTrack) + { + var trackFiles = localTrack.Tracks.Where(e => e.TrackFileId != 0).Select(e => e.TrackFile).ToList(); + + if (trackFiles.Count == 0) + { + _logger.Debug("No existing track file, skipping"); + return Decision.Accept(); + } + + if (trackFiles.Count > 1) + { + _logger.Debug("More than one existing track file, skipping."); + return Decision.Accept(); + } + + if (trackFiles.First().Value.Size == localTrack.Size) + { + _logger.Debug("'{0}' Has the same filesize as existing file", localTrack.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 06c7af236..f39bac565 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -633,6 +633,7 @@ +