Fixed: Don't import the same file again

Closes #929
This commit is contained in:
Mark McDowall 2017-05-02 22:52:59 -07:00
parent f852ca91c0
commit 53e51af9c7
No known key found for this signature in database
GPG Key ID: D4CEFA9A718052E0
4 changed files with 140 additions and 0 deletions

View File

@ -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.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<SameFileSpecification>
{
private LocalEpisode _localEpisode;
[SetUp]
public void Setup()
{
_localEpisode = Builder<LocalEpisode>.CreateNew()
.With(l => l.Size = 150.Megabytes())
.Build();
}
[Test]
public void should_be_accepted_if_no_existing_file()
{
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.TheFirst(1)
.With(e => e.EpisodeFileId = 0)
.BuildList();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_be_accepted_if_multiple_existing_files()
{
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(2)
.TheFirst(1)
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Size = _localEpisode.Size
}))
.TheNext(1)
.With(e => e.EpisodeFileId = 2)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Size = _localEpisode.Size
}))
.Build()
.ToList();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_be_accepted_if_file_size_is_different()
{
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.TheFirst(1)
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Size = _localEpisode.Size + 100.Megabytes()
}))
.Build()
.ToList();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_be_reject_if_file_size_is_the_same()
{
_localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
.TheFirst(1)
.With(e => e.EpisodeFileId = 1)
.With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
new EpisodeFile
{
Size = _localEpisode.Size
}))
.Build()
.ToList();
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse();
}
}
}

View File

@ -283,6 +283,7 @@
<Compile Include="MediaFiles\EpisodeImport\SampleServiceFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FreeSpaceSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FullSeasonSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\SameFileSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\GrabbedReleaseQualityFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\MatchesFolderSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\NotSampleSpecificationFixture.cs" />

View File

@ -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, DownloadClientItem downloadClientItem)
{
var episodeFiles = localEpisode.Episodes.Where(e => e.EpisodeFileId != 0).Select(e => e.EpisodeFile).ToList();
if (episodeFiles.Count == 0)
{
_logger.Debug("No existing episode file, skipping");
return Decision.Accept();
}
if (episodeFiles.Count > 1)
{
_logger.Debug("More than one existing episode file, skipping.");
return Decision.Accept();
}
if (episodeFiles.First().Value.Size == localEpisode.Size)
{
_logger.Debug("'{0}' Has the same filesize as existing file", localEpisode.Path);
return Decision.Reject("Has the same filesize as existing file");
}
return Decision.Accept();
}
}
}

View File

@ -754,6 +754,7 @@
<Compile Include="MediaFiles\EpisodeImport\DetectSample.cs" />
<Compile Include="MediaFiles\EpisodeImport\Manual\ManuallyImportedFile.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FreeSpaceSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\SameFileSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\GrabbedReleaseQualitySpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\MatchesFolderSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FullSeasonSpecification.cs" />