NotUnpacking check added to episode import

Checks last write time when in working folder, should help with moving while unpacking
This commit is contained in:
Mark McDowall 2013-08-22 08:34:51 -07:00
parent 227e13d858
commit 7771899e29
7 changed files with 134 additions and 1 deletions

View File

@ -93,7 +93,6 @@ namespace NzbDrone.Common
{
Ensure.That(() => path).IsValidPath();
if (!FileExists(path))
throw new FileNotFoundException("File doesn't exist: " + path);

View File

@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using Marr.Data;
using Moq;
using Newtonsoft.Json.Serialization;
using NUnit.Framework;
using NzbDrone.Common;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFileTests.EpisodeImportTests
{
[TestFixture]
public class NotUnpackingSpecificationFixture : CoreTest<NotUnpackingSpecification>
{
private LocalEpisode _localEpisode;
[SetUp]
public void Setup()
{
Mocker.GetMock<IConfigService>()
.SetupGet(s => s.DownloadClientWorkingFolders)
.Returns("_UNPACK_|_FAILED_");
_localEpisode = new LocalEpisode
{
Path = @"C:\Test\Unsorted TV\30.rock\30.rock.s01e01.avi".AsOsAgnostic(),
Size = 100,
Series = Builder<Series>.CreateNew().Build()
};
}
private void GivenInWorkingFolder()
{
_localEpisode.Path = @"C:\Test\Unsorted TV\_UNPACK_30.rock\30.rock.s01e01.avi".AsOsAgnostic();
}
private void GivenLastWriteTimeUtc(DateTime time)
{
Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetLastFileWrite(It.IsAny<string>()))
.Returns(time);
}
[Test]
public void should_return_true_if_not_in_working_folder()
{
Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue();
}
[Test]
public void should_return_true_when_in_old_working_folder()
{
GivenInWorkingFolder();
GivenLastWriteTimeUtc(DateTime.UtcNow.AddHours(-1));
Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue();
}
[Test]
public void should_return_false_if_in_working_folder_and_last_write_time_was_recent()
{
GivenInWorkingFolder();
GivenLastWriteTimeUtc(DateTime.UtcNow);
Subject.IsSatisfiedBy(_localEpisode).Should().BeFalse();
}
}
}

View File

@ -129,6 +129,7 @@
<Compile Include="JobTests\TestJobs.cs" />
<Compile Include="MediaCoverTests\CoverExistsSpecificationFixture.cs" />
<Compile Include="MediaCoverTests\MediaCoverServiceFixture.cs" />
<Compile Include="MediaFileTests\EpisodeImportTests\NotUnpackingSpecificationFixture.cs" />
<Compile Include="MediaFileTests\EpisodeImportTests\NotInUseSpecificationFixture.cs" />
<Compile Include="MediaFileTests\EpisodeImportTests\FreeSpaceSpecificationFixture.cs" />
<Compile Include="MediaFileTests\RenameEpisodeFileServiceFixture.cs" />

View File

@ -258,6 +258,12 @@ namespace NzbDrone.Core.Configuration
set { SetValue("AutoDownloadPropers", value); }
}
public string DownloadClientWorkingFolders
{
get { return GetValue("DownloadClientWorkingFolders", "_UNPACK_|_FAILED_"); }
set { SetValue("DownloadClientWorkingFolders", value); }
}
private string GetValue(string key)
{
return GetValue(key, String.Empty);

View File

@ -38,6 +38,7 @@ namespace NzbDrone.Core.Configuration
string ReleaseRestrictions { get; set; }
Int32 RssSyncInterval { get; set; }
Boolean AutoDownloadPropers { get; set; }
String DownloadClientWorkingFolders { get; set; }
void SaveValues(Dictionary<string, object> configValues);
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.IO;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
{
public class NotUnpackingSpecification : IImportDecisionEngineSpecification
{
private readonly IDiskProvider _diskProvider;
private readonly IConfigService _configService;
private readonly Logger _logger;
public NotUnpackingSpecification(IDiskProvider diskProvider, IConfigService configService, Logger logger)
{
_diskProvider = diskProvider;
_configService = configService;
_logger = logger;
}
public string RejectionReason { get { return "File is still being unpacked"; } }
public bool IsSatisfiedBy(LocalEpisode localEpisode)
{
if (_diskProvider.IsParent(localEpisode.Series.Path, localEpisode.Path))
{
_logger.Trace("{0} is in series folder, unpacking check", localEpisode.Path);
return true;
}
foreach (var workingFolder in _configService.DownloadClientWorkingFolders.Split('|'))
{
if (Directory.GetParent(localEpisode.Path).Name.StartsWith(workingFolder))
{
if (_diskProvider.GetLastFileWrite(localEpisode.Path) > DateTime.UtcNow.AddMinutes(-5))
{
_logger.Trace("{0} appears to be unpacking still", localEpisode.Path);
return false;
}
}
}
return true;
}
}
}

View File

@ -225,6 +225,7 @@
<Compile Include="MediaFiles\EpisodeImport\IImportDecisionEngineSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\ImportDecisionMaker.cs" />
<Compile Include="MediaFiles\EpisodeImport\ImportApprovedEpisodes.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\NotUnpackingSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\NotInUseSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FreeSpaceSpecification.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\UpgradeSpecification.cs" />